简体   繁体   English

当我试图解密获得以下错误

[英]When I am trying to decrypt getting following error

I am getting following error while decrypting. 我在解密时遇到以下错误。

Once it was working fine but suddenly I am getting this error. 一旦它工作正常但突然我得到这个错误。

How can I solve this problem? 我怎么解决这个问题? I have read many articles but couldn't get help. 我看了很多文章,却无法得到帮助。

java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:388)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:186)
at javax.crypto.Cipher.implInit(Cipher.java:786)
at javax.crypto.Cipher.chooseProvider(Cipher.java:848)
at javax.crypto.Cipher.init(Cipher.java:1212)
at javax.crypto.Cipher.init(Cipher.java:1152)
at 
com.test.security.TestEncryptDecrypt.decrypt(TestEncryptDecrypt.java:92)
at com.test.security.TestEncryptDecrypt.main(TestEncryptDecrypt.java:59)

The code is give below: 代码如下:

public static void main(String []args){

try {

String encString =  encrypt("PID=0000000003|ITC=NA|PRN=MNKB0701511135|AMT=1.00|CRN=INR|RU=https://www.testsite.com/testsk/servlet/TestResponseHandler?");

System.out.println("Enc  : " + encString);

System.out.println("Dec  : "+ decrypt(encString));

} catch (Exception e) {

e.printStackTrace();
}

}

Encrypt method 加密方法

public static String encrypt(String data) throws Exception {
String keyFile = "E:\\testpath\\Keys\\0000000003.key";
byte[] keyb = Files.readAllBytes(Paths.get(keyFile));

SecretKeySpec skey = new SecretKeySpec(keyb, "AES");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, skey);
byte[] encVal = c.doFinal(data.getBytes());
return new BASE64Encoder().encode(encVal);
}

Decrypt method 解密方法

public static String decrypt(String encryptedData)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException, IOException {

String keyFile = "E:\\testpath\\Keys\\0000000003.key";

byte[] keyb = Files.readAllBytes(Paths.get(keyFile));

SecretKeySpec skey = new SecretKeySpec(keyb, "AES");    
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, skey);

byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
byte[] decValue = c.doFinal(decordedValue);
return new String(decValue);
}

As the error message says, you are missing a parameter in the init methods. 正如错误消息所示,您在init方法中缺少一个参数。 If you're using CBC you should also specify the AlgorithmParameterSpec . 如果您正在使用CBC,则还应指定AlgorithmParameterSpec

Could you perhaps try the following: 您可以尝试以下方法:

byte[] paramSpecBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec paramSpec = new IvParameterSpec(paramSpecBytes);

And in your encrypt and decrypt methods: 在您的加密和解密方法中:

c.init(Cipher.ENCRYPT_MODE, skey, paramSpec);
c.init(Cipher.DECRYPT_MODE, skey, paramSpec);

You could fill the bytes with any value you like though, doesn't need to be zero's. 您可以使用您喜欢的任何值填充字节,但不需要为零。 Best would be some random value. 最好的是一些随机值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试 setOnClickListener 时出现错误 - I am getting error when trying to setOnClickListener 即时通讯尝试编译以下Java代码时出现错误“找不到javax.servlet包” - I am getting an error “javax.servlet package not found” when i m trying to compile the following java code 我正在尝试在tomcat 6.0中运行一个jsp项目。 但是我收到以下错误 - I am trying to run a jsp project in tomcat 6.0. But I am getting the following error 可以告诉我为什么我尝试在地图中添加浮点数时出现以下错误,即使我的浮点数是从Number继承的 - Could any on tell why am I getting following error when I am trying to add float number in the map even though my float is inherited from Number 我正在尝试使用JSON格式显示来自MySQL数据库的数据,但我一直收到以下错误 - I am trying to display data from MySQL database using JSON Format but i keep getting the following error 我一直在尝试在 intelliJ 中运行 selenium 基本测试,但出现以下错误 - I have been trying to run selenium basic test in intelliJ and I am getting the following error 在Hibernate中,我在运行时遇到以下错误 - In Hibernate,I am getting following error at runtime 嗨,我正在尝试在 tomcat 10 中部署 spring mvc 项目,但出现以下错误。 我搜索了很多但没有找到解决方案 - Hi I am trying to deploy spring mvc project in tomcat 10 but I am getting following error . I search a lot but not find the solution 尝试在jmeter 4.0上运行Http测试脚本记录时出现以下错误 - I am getting the following error while trying to run Http Test Script Record on jmeter 4.0 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM