在Android上加密文件AES [英] Encrypting files with AES on Android
问题描述
所以我工作的一个个人项目,我自己,我想我的手机上的文件进行加密。这些文件可以是任何东西,如文档,照片等。现在,我试图让这个工作正常。当过我运行加密它似乎正常工作和加密文件。当我运行解密,有时它工作和其他时候也不能。当它失败我一般得到一个错误而最终确定的密码,垫块损坏错误。我也没有使用不同的测试文件,因此它不像一些文件工作和别人不一样。这是同样的两个文件我尝试每一次。
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();
}
目前的盐和密码是静态的,不用于测试目的改变。仍然得到大约一半的时间错误。
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:
<一个href="http://stackoverflow.com/questions/26426151/android-decryption-error-while-finalizing-cipher">Android解密:错误而最终确定密码
<一个href="http://stackoverflow.com/questions/12799726/last-block-incomplete-with-cipherinputstream-cipheroutputstream-even-with-paddi">last不完全阻塞与CipherInputStream / CipherOutputStream,即使填充AES / CBC / PKCS5Padding
<一个href="http://stackoverflow.com/questions/11503157/decrypting-error-no-iv-set-when-one-expected">Decrypting错误:&QUOT;当一个预期&QUOT没有四集;
<一个href="http://stackoverflow.com/questions/4598873/how-to-handle-last-block-incomplete-in-decryption">How处理&QUOT;最后一个数据块不完整的解密&QUOT;
<一个href="http://stackoverflow.com/questions/12136558/encryption-and-decryption-of-image-file">Encryption和图像文件解密
<一个href="http://stackoverflow.com/questions/13527521/tips-on-encryption-decryption-of-images-in-java-using-aes">Tips使用AES 在java中的图像加密/解密
Tips on encryption/decryption of images in java using AES
任何帮助是极大AP preciated!我想,我只是简单的东西...
Any help is greatly appreciated! I think I am just missing something simple...
更新!
人们都权当它是盐。当我删除了盐,问题解决了......难道一点挖,原来盐+通行证是问题,但由于盐是一个byte []和通是一个字符串。我改变盐的字符串,然后用salt.concat(通行证),问题解决了!
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!