在 Android 上使用 AES 加密文件
Encrypting files with AES on Android
所以我正在为自己做一个个人项目,我正在尝试加密 phone 上的文件。这些文件可以是任何文件,即文档、照片等。现在我正在努力让它正常工作。每当我 运行 加密时,它似乎都能正常工作并加密文件。当我 运行 解密时,有时它可以工作,有时则不能。当它失败时,我通常会收到 "Error while finalizing cipher, pad block corrupted" 错误。我也没有使用不同的测试文件,所以它不像某些文件有效而其他文件无效。这是我每次尝试的两个相同的文件。
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();
}
目前盐和密码是静态的,不会为了测试目的而改变。大约一半的时间仍然会出错。
有人知道为什么会这样吗?我一直在四处搜索,发现了一些可以尝试的方法,none 其中有效。我已经查看了以下一些问题以寻求解决方案:
Android decryption: Error while finalizing cipher
last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding
Encryption error on Android 4.2
Decrypting error : "no iv set when one expected"
How to handle "last block incomplete in decryption"
Encryption and decryption of image file
Tips on encryption/decryption of images in java using AES
非常感谢任何帮助!我想我只是缺少一些简单的东西...
更新!
人们是对的,当它是盐时。当我移除盐时,问题就解决了......再挖掘一点,结果是 salt+Pass 是问题所在,但因为盐是一个字节 [] 而 Pass 是一个字符串。我把salt改成了String,然后用salt.concat(Pass),问题就解决了!
也许我遗漏了一些东西,但在我这边它没有问题。
您可以尝试以下 class 只需更改 fileToBeCrypted、fileToBeDecrypted 和 fileDecryptedOutput 变量吗?
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 兼容模式。
希望对您有所帮助。
再见
皮耶罗
所以我正在为自己做一个个人项目,我正在尝试加密 phone 上的文件。这些文件可以是任何文件,即文档、照片等。现在我正在努力让它正常工作。每当我 运行 加密时,它似乎都能正常工作并加密文件。当我 运行 解密时,有时它可以工作,有时则不能。当它失败时,我通常会收到 "Error while finalizing cipher, pad block corrupted" 错误。我也没有使用不同的测试文件,所以它不像某些文件有效而其他文件无效。这是我每次尝试的两个相同的文件。
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();
}
目前盐和密码是静态的,不会为了测试目的而改变。大约一半的时间仍然会出错。
有人知道为什么会这样吗?我一直在四处搜索,发现了一些可以尝试的方法,none 其中有效。我已经查看了以下一些问题以寻求解决方案:
Android decryption: Error while finalizing cipher
last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding
Encryption error on Android 4.2
Decrypting error : "no iv set when one expected"
How to handle "last block incomplete in decryption"
Encryption and decryption of image file
Tips on encryption/decryption of images in java using AES
非常感谢任何帮助!我想我只是缺少一些简单的东西...
更新!
人们是对的,当它是盐时。当我移除盐时,问题就解决了......再挖掘一点,结果是 salt+Pass 是问题所在,但因为盐是一个字节 [] 而 Pass 是一个字符串。我把salt改成了String,然后用salt.concat(Pass),问题就解决了!
也许我遗漏了一些东西,但在我这边它没有问题。 您可以尝试以下 class 只需更改 fileToBeCrypted、fileToBeDecrypted 和 fileDecryptedOutput 变量吗?
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 兼容模式。
希望对您有所帮助。
再见 皮耶罗