user1845266
user1845266

Reputation: 41

Java Signature can not be verified cross platform

I have a problem and i'm not able to solve it. I create a signature object on linux, and try to verify it on windows and it fails. The same the other way around. If i stay on one platform everythings fine.

First i thought about encoding, so i started some tests like setting -Dfile.encoding to different standards. But even if i create the signature using UTF-8 and verify it using windows-1215, if i stay on the same platform everythings fine.

The code is very basic, and i just can't find the problem:

Creating the signature:

public void signData(File fileToSign, String outPutFileName)...
{
    Signature dsa = Signature.getInstance("DSA");
    dsa.initSign(privateKey);

    byte[] bytesToSign = FileUtils.readByteArrayFromFile(fileToSign);
    dsa.update(bytesToSign);

    byte[] sigData = dsa.sign();
    FileUtils.saveByteArrayToFile(outPutFileName, sigData);
}

public static void saveByteArrayToFile(String outPutFileName, byte[] bytesToSave)...
{
    FileOutputStream fos = new FileOutputStream(outPutFileName);
    fos.write(Base64.encodeBase64(bytesToSave));
    fos.close();
}

verifying it:

public boolean isVerified(File fileToVerify, File signatureFile)...
{
    byte[] sigData = FileUtils.readByteArrayFromFile(signatureFile);

    Signature signature = Signature.getInstance("DSA");
    signature.initVerify(publicKey);

    byte[] byteToVerify = FileUtils.readByteArrayFromFile(fileToVerify);
    signature.update(byteToVerify);

    return signature.verify(sigData);
}

public static byte[] readByteArrayFromFile(File file)...
{
    FileInputStream fis = new FileInputStream(file);
    byte[] byteArray = new byte[fis.available()];
    fis.read(byteArray);
    fis.close();

    return Base64.decodeBase64(byteArray);
}

I hope someone can point me in the right direction.
Thanks.

With kind regards,

Upvotes: 2

Views: 261

Answers (1)

user1845266
user1845266

Reputation: 41

I finally found a solution. The problem was indeed the encoding. Now I just de- and encode my data and everything's fine. I updated the code accordingly.

Upvotes: 1

Related Questions