【发布时间】:2017-10-27 20:06:42
【问题描述】:
我正在解密一些加密数据。说数据是高度机密的信息。信息用tripledes_cbc算法加密。用于加密信息的秘钥采用rsa算法加密。
但是当我尝试使用DESede/CBC/NoPadding 算法解密信息时,我得到了原始数据以及一些在前面和结尾填充的垃圾字符。
当我尝试使用 DES/CBC/PKCS5Padding 而不是 DESede/CBC/NoPadding 时,出现“未正确填充最终块”异常。
下面是代码sn-p。请让我知道我可以做些什么来获得没有额外填充的正确文本:
KeyStore ks = KeyStore.getInstance("jks");
FileInputStream fis = new FileInputStream(ksFile);
ks.load(fis, "testing".toCharArray());
PrivateKey privateKey = (PrivateKey) ks.getKey("keys", "1234".toCharArray());
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] kekBytes = cipher.doFinal(Base64.decodeBase64(encKey.getBytes("UTF-8")));
SecureRandom random = new SecureRandom();
byte[] iv = new byte[8];
random.nextBytes(iv);
SecretKey key = new SecretKeySpec(kekBytes, "DESede");
Cipher cipher1 = Cipher.getInstance("DESede/CBC/NoPadding");
cipher1.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] out = cipher1.doFinal(Base64.decodeBase64(data.getBytes("UTF-8")));
System.out.println("Data Length: " + out.length);
String result = new String(out, "UTF-8");
【问题讨论】:
-
1. 3DES 不应该用于新工作,AES 是当前使用的对称方法。但我猜,这是一个学校项目,学校比现实世界落后了十年左右。 2.提供十六进制格式的预期和解密数据示例。
-
显然你需要找出加密端使用了什么填充。另外,您使用的是 DES 还是 DESede?因为
DES/CBC/PKCS5Padding不是DESede/CBC/NoPadding的明智替代品。也许你的意思是DESede/CBC/PKCS5Padding?
标签: java encryption cryptography rsa tripledes