【发布时间】:2019-11-06 06:15:05
【问题描述】:
好的,所以当我加密任何东西时,让我们说在这种情况下是一个文件,用我的电脑。它可以很好地加密和解密所述文件。但是,如果用我的计算机加密文件,然后将它发送给我的 android 并运行我编写的相同解密代码,它不会正确解密文件。它没有显示任何错误,但是文件显然没有正确解密。
所以我了解到 Bouncy Castle 与 JVM/JDK 标准加密库完全不同,所以我不能 100% 确定这是否是问题所在。然而,这将是有道理的。问题是,如果是我不知道如何在我的 android 上使用 JVM/JDK 库进行加密。我不想在我的电脑上使用 Bouncy Castle 库。
public static void encrypt(File source, File dest, String password){
try{
FileInputStream inFile = new FileInputStream(source.getPath());
FileOutputStream outFile = new FileOutputStream(dest.getPath());
byte[] salt = new byte[8], iv = new byte[16];
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
SecretKey secretKey = factory.generateSecret(keySpec);
SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));
byte[] input = new byte[64];
int bytesRead;
while((bytesRead = inFile.read(input)) != -1){
byte[] output = cipher.update(input, 0, bytesRead);
if(output != null){
outFile.write(output);
}
}
byte[] output = cipher.doFinal();
if(output != null){
outFile.write(output);
}
inFile.close();
outFile.flush();
outFile.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void decrypt(File source, File dest, String password){
try{
byte[] salt = new byte[8], iv = new byte[16];
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(keySpec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
FileInputStream fis = new FileInputStream(source.getPath());
FileOutputStream fos = new FileOutputStream(dest.getPath());
byte[] in = new byte[64];
int read;
while((read = fis.read(in)) != -1){
byte[] output = cipher.update(in, 0, read);
if(output != null){
fos.write(output);
}
}
byte[] output = cipher.doFinal();
if(output != null){
fos.write(output);
}
fis.close();
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
结果也将能够使用我的计算机使用标准 JVM/JDK 加密库加密任何文件,并且也能够使用我的 android 解密相同的文件。
但是,如果除了 Bouncy Castle 之外还有其他可以与 android 一起使用的库,我愿意提供想法。
【问题讨论】:
-
您可能有兴趣查看
CipherInputStream/CipherOutputStream类和Files类的copy(...)方法。它们一起可以用来消除代码中的读写 I/O 循环。结果更短、更清晰、更不容易出错。从 API 26 开始,Files类在 Android 中可用。
标签: java android file encryption