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,b); } cos.flush(); cos.close(); fis.close();}public static voID decrypt(String path,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,"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,b); } fos.flush(); fos.close(); cis.close();}
目前,Salt和Password是静态的,为了测试目的不要改变.一半的时间仍然会出错.
有没有人有什么想法为什么会发生这种情况?我一直在寻找,我已经找到了几件事情尝试,没有一个工作.我已经考虑了以下解决方案的以下问题:
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改成了String,然后用salt.concat(pass),问题解决了!
解决方法 也许我错过了一些东西,但在我身边没有任何问题.可以尝试以下类简单的更改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,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,b); } cos.flush(); cos.close(); fis.close(); } public static voID decrypt(String path,String password,String outPath) throws IOException,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,"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,b); } fos.flush(); fos.close(); cis.close(); }}
我可以迭代多次没有错误!
我正在使用Oracle JDK 1.8,但是以1.7兼容模式运行.
希望这能帮助你.
再见皮耶罗
总结以上是内存溢出为你收集整理的java – 在Android上使用AES加密文件全部内容,希望文章能够帮你解决java – 在Android上使用AES加密文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)