Java解密提供了与原始不同的结果

时间:2016-06-02 16:41:32

标签: java encryption aes

使用CipherInputStream和CipherOutputStream加密和解密文件时,解密的文件与原始文件不同。在执行期间没有抛出任何错误,但原始和结果具有不同的哈希值。原始和结果也是完全相同的大小。没有任何文件冲突/覆盖正在进行。到目前为止,我最好的猜测是字符编码。

public void fileTest(File source, File output, String key) throws Exception {
    Log.write("Starting file encryption/decryption test!");
    Util.charset = CharsetToolkit.guessEncoding(source, 4096, StandardCharsets.UTF_8);
    Log.write("Using charset " + Util.charset.name());
    Log.write("Using key: " + key);
    String oHash = Util.checksum(source);
    Log.write("Original hash: " + oHash);

    //Cipher setup

    SecretKeySpec sks = Util.padKey(key);
    Cipher eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    Cipher dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, sks, new IvParameterSpec(new byte[16]));
    dCipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(new byte[16]));

    //IO setup
    File tmpEncrypt = new File(source.getParent() + "/" + source.getName() + "-tmp");
    tmpEncrypt.createNewFile();
    output.createNewFile();
    Log.write("Encrypting to: " + tmpEncrypt.getAbsolutePath());
    InputStream fis = new FileInputStream(source);
    InputStream enIn = new FileInputStream(tmpEncrypt);
    OutputStream fos = new FileOutputStream(tmpEncrypt);
    OutputStream clearOut = new FileOutputStream(output);
    CipherInputStream cis = new CipherInputStream(enIn, dCipher);
    CipherOutputStream cos = new CipherOutputStream(fos, eCipher);

    //Encrypt
    Log.write("Starting encryption process");
    int numRead = 0;
    byte[] buffer = new byte[1024];
    while ((numRead = fis.read(buffer)) >= 0) {
        cos.write(buffer, 0, numRead);
    }

    cos.close();
    fos.close();
    Log.write("Done!");

    Log.write("Encrypted hash: " + Util.checksum(output));

    //Decrypt
    Log.write("Starting decryption process");
    int nr = 0;
    byte[] b = new byte[1024];
    while ((nr = cis.read(b)) >= 0) {
        clearOut.write(buffer, 0, nr);
    }
    clearOut.close();
    cis.close();
    fis.close();
    Log.write("Done!");

    String fHash = Util.checksum(output);
    Log.write("Final hash: " + fHash);

    if(fHash.equals(oHash)) {
        Log.write("Success! The hashes are equal!");
    } else {
        Log.write("Failure! Hashes are different!");
    }
}

编辑: 我接受了@zaph的建议,现在写入/读取文件似乎有问题。该文件正好是40个字节长。这是十六进制转储:

Key hex: 000074657374696e676b65797364617767313233
IV hex: 0000000000000000000000000000000000000000
Data IN: Lorem ipsum dolor sit amet orci aliquam.
Data OUT: Lorem ipsum dolor sit amet orci Lorem ip.

奇怪的是,它似乎是重复前8个字节重写最后8个字节。我尝试将缓冲区大小从1024字节调整为8字节,它甚至更奇怪。 8字节测试的十六进制转储:

Key hex: 000074657374696e676b65797364617767313233
IV hex: 0000000000000000000000000000000000000000
Data IN: Lorem ipsum dolor sit amet orci aliquam.
Data OUT: aliquam.aliquam.aliquam.aliquam.aliquam.

第一个被读/写的方式肯定是错的,但我不知道发生了什么。 提前谢谢!

1 个答案:

答案 0 :(得分:2)

解决了!我的while循环出现问题。像这样:

while ((nr = cis.read(b)) >= 0) {
    clearOut.write(buffer, 0, nr);
}

所以我用for循环代替它,一切正常。

int segs = (int) (source.length() / 4);
for (int i = 0; i < segs; i++) {
    fis.read(buffer);
    cos.write(buffer, 0, buffer.length);
}
相关问题