大家好,我是用于加密的AES,我所做的是将数据加密到文本文件中并存储到给定的位置,如果在同一个类文件中给出解密,则可以正常工作,我创建了一个不同的Java类来解密在文件中,我正在使用带有用户名和密码的Javakeystore来存储密钥并检索它,并使用存储的密钥进行解密,但是我遇到了以上错误。帮帮我。这是解密代码。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import de.flexiprovider.core.FlexiCoreProvider;
public class Decrypto {
public static void main(String[] args) throws Exception {
Security.addProvider(new FlexiCoreProvider());
/*
* Cipher cipher1 = Cipher.getInstance("AES128_CBC", "FlexiCore");
* KeyGenerator keyGen = KeyGenerator.getInstance("AES", "FlexiCore");
* SecretKey secKey = keyGen.generateKey();
* System.out.println(secKey);
*/
Cipher cipher1 = Cipher.getInstance("AES128_CBC", "FlexiCore");
KeyStore keyStore = KeyStore.getInstance("JCEKS");
FileInputStream fis = new FileInputStream("C:\\mykey.keystore"); // here
// i am
// uploading
keyStore.load(fis, "javaci123".toCharArray());
fis.close();
Key secKey = (Key) keyStore.getKey("mySecretKey",
"javaci123".toCharArray()); // line 35
System.out.println("Found Key: " + (secKey));
String cleartextFile = "C:\\cleartext.txt";
String ciphertextFile = "C:\\ciphertextSymm.txt";
// FileInputStream fis = new FileInputStream(cleartextFile);
FileOutputStream fos = new FileOutputStream(ciphertextFile);
String cleartextAgainFile = "C:\\cleartextAgainSymm.txt";
cipher1.init(Cipher.DECRYPT_MODE, secKey);
fis = new FileInputStream(ciphertextFile);
// fis = new FileInputStream(ciphertextFile);
CipherInputStream cis = new CipherInputStream(fis, cipher1);
fos = new FileOutputStream(cleartextAgainFile);
byte[] block = new byte[8];
int i;
while ((i = fis.read(block)) != -1) {
cis.read(block, 0, i);
}
cis.close();
}
}
错误
Exception in thread "main" java.security.UnrecoverableKeyException: Given final block not properly padded
at com.sun.crypto.provider.KeyProtector.unseal(KeyProtector.java:360)
at com.sun.crypto.provider.JceKeyStore.engineGetKey(JceKeyStore.java:133)
at java.security.KeyStore.getKey(Unknown Source)
at darm.code.com.Decrypto.main(Decrypto.java:35)
最佳答案
UnrecoverableKeyException
可以识别问题,尤其是如果根本原因是"Given final block not properly padded"
。这基本上意味着您的密码不正确。 KeyStore
将首先根据给定的密码生成一个密钥,然后他们使用该密钥解密存储的密钥。如果解密失败,则希望出现MAC身份验证错误,但是在这种情况下,您会得到填充错误(这基本上意味着有人忘记为包含包装好的私钥的容器添加完整性保护)。