Java 解密提供了与原始结果不同的结果
Java decryption provides a different result from the original
使用CipherInputStream和CipherOutputStream对文件进行加密和解密时,解密后的文件与原始文件不同。在执行过程中没有抛出错误,但原始和结果具有不同的哈希值。原件和结果的尺寸也完全相同。没有任何文件 collisions/overwriting 正在进行。到目前为止,我最好的猜测是字符编码。
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,现在 writing/read 文件似乎有问题。该文件恰好有 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.
第一个 read/wrote 的方式肯定有问题,但我不知道发生了什么。
提前致谢!
解决了!我的 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);
}
使用CipherInputStream和CipherOutputStream对文件进行加密和解密时,解密后的文件与原始文件不同。在执行过程中没有抛出错误,但原始和结果具有不同的哈希值。原件和结果的尺寸也完全相同。没有任何文件 collisions/overwriting 正在进行。到目前为止,我最好的猜测是字符编码。
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,现在 writing/read 文件似乎有问题。该文件恰好有 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.
第一个 read/wrote 的方式肯定有问题,但我不知道发生了什么。 提前致谢!
解决了!我的 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);
}