我遇到解密通过openssl命令行加密的文本文件的问题:
Key Geanaration:
# openssl enc -aes-256-cbc -k secret -P -md sha1
salt=770AB8C12EF73A4E
key=7EF9E708C05E1767C433E097A66A84AC1971E124E42BDB9D4F0967FF77642AA4
iv =EFFA2B9E2990BC6641E1E5D859D3CF37
File Encryption:
# openssl enc -aes-256-cbc -K 7EF9E708C05E1767C433E097A66A84AC1971E124E42BDB9D4F0967FF77642AA4 -iv EFFA2B9E2990BC6641E1E5D859D3CF37 -in text.txt -out text22.enc
但是我有一个代码,我无法将其解密为:
public void decryptAES() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{
FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()+"/encrypted.txt");
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/decrypted.txt");
SecretKeySpec sks = new SecretKeySpec("your_key".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC");
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();
}
答案 0 :(得分:1)
您忘记实现IV功能,因此前16个字节将不同。您确实需要使用相同的密钥进行对称加密,例如AES,并且应指定填充模式,例如"AES/CBC/PKCS5Padding"
。