问题描述
所以我正在为自己做一个个人项目,我正在尝试加密手机上的文件.这些文件可以是任何东西,即文档、照片等.现在我正试图让它正常工作.当我运行加密时,它似乎可以正常工作并加密文件.当我运行解密时,有时它会起作用,有时则不起作用.当它失败时,我通常会收到完成密码时出错,填充块损坏"错误.我也没有使用不同的测试文件,所以它不像某些文件工作而其他文件不工作.这是我每次尝试的相同的两个文件.
So I am working on a personal project for myself, and I am trying to encrypt files on my phone. These files can be anything i.e Documents, photos, etc. Right now I am trying to get this working properly. When ever I run the encryption it seems to work properly and encrypt the files. When I run the decrypt, sometimes it works and other times it doesn't. When it fails I generally get a "Error while finalizing cipher, pad block corrupted" error. I'm also not using different test files, so it isn't like some files work and others don't. It's the same two files I try each time.
public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static void decrypt(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.replace(".crypt",""));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
目前,Salt 和密码是静态的,不会出于测试目的而更改.大约有一半的时间仍然会出错.
Currently the Salt and Password are static and do not change for testing purposes. Still get errors about half the time.
有没有人知道为什么会发生这种情况?我一直在四处寻找,我找到了一些可以尝试的方法,但都没有奏效.我查看了以下一些问题以寻求解决方案:
Does anyone have any ideas on why this happens? I have been searching around and I have found a couple things to try, none of which worked. I have looked through some of the following questions for solutions:
使用 CipherInputStream/CipherOutputStream 不完整的最后一个块,即使使用填充AES/CBC/PKCS5Padding
非常感谢任何帮助!我想我只是缺少一些简单的东西...
Any help is greatly appreciated! I think I am just missing something simple...
更新!
人们是对的,因为它是盐.当我去除盐时,问题就解决了......做了更多的挖掘,结果是盐+传递是问题,但是因为盐是一个字节[]而传递是一个字符串.我把salt改成String,然后用salt.concat(Pass),问题解决了!
People were right when it was the salt. When I removed the salt, the problem was solved... Did a little more digging, turns out salt+Pass was the issues, but because the salt was a byte[] and Pass was a string. I changed salt to String and then used salt.concat(Pass) and the problem was solved!
推荐答案
也许我遗漏了一些东西,但在我这边它可以正常工作.您可以尝试简单地更改 fileToBeCrypted、fileToBeDecrypted 和 fileDecryptedOutput 变量的以下类吗?
maybe I'm missing something but on my side it works without a problem.Can you try the following class simply changing the fileToBeCrypted, fileToBeDecrypted and fileDecryptedOutput variables?
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class TestCrypt{
private static final String salt = "t784";
private static final String cryptPassword = "873147cbn9x5'2 79'79314";
private static final String fileToBeCrypted = "c:\Temp\sampleFile.conf";
private static final String fileToBeDecrypted = "c:\Temp\sampleFile.conf.crypt";
private static final String fileDecryptedOutput = "c:\Temp\sampleFile.conf.decrypted";
public static void main(String[] args) throws Exception
{
for (int i=0; i<100; i++)
{
encryptfile(fileToBeCrypted, cryptPassword);
decrypt(fileToBeDecrypted, cryptPassword, fileDecryptedOutput);
System.out.println(i);
}
}
public static void encryptfile(String path,String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static void decrypt(String path,String password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(outPath);
byte[] key = (salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
}
我可以迭代很多次而没有错误!我使用的是 Oracle JDK 1.8,但在 1.7 兼容模式下运行.
I can iterate many times without an error!I'm using Oracle JDK 1.8 but running in 1.7 compatibility mode.
希望对你有所帮助.
再见皮耶罗
这篇关于在 Android 上使用 AES 加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!