将解密的.net代码转换为Java时出现异常
Exception in thread "main" java.lang.IllegalArgumentException: Missing argument
at javax.crypto.spec.SecretKeySpec.<init>(DashoA13*..)
at com.motorola.gst.DecryptTest3.Decrypt(DecryptTest3.java:90)
at com.motorola.gst.DecryptTest3.main(DecryptTest3.java:36)
嗯,我是第一次尝试解密并将.net代码转换为Java
这是我要转换的.net代码
private static string Decrypt(string encryptedText, string completeEncodedKey, int keySize)
{
RijndaelManaged aesEncryption = new RijndaelManaged();
aesEncryption.KeySize = keySize;
aesEncryption.BlockSize = 128;
aesEncryption.Mode = CipherMode.CBC;
aesEncryption.Padding = PaddingMode.PKCS7;
aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[1]);
ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);
return ASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length));
}
[我浏览了许多帖子,发现Decrypting bytes encrypted by .NET's RijndaelManaged using Java与我的案子更相关。
我遵循了这些,并将解密函数写为:::>
private static String Decrypt(String encryptedText, String completeEncodedKey,int keySize) { //get completeEncodedKey in bytes and then to string String decodedcompleteEncodedKey = StringUtils.newStringUtf8(Base64.decodeBase64(completeEncodedKey)); System.out.println("Decoded completeEncodedKey Key :: "+decodedcompleteEncodedKey); int indexComma = decodedcompleteEncodedKey.indexOf(','); System.out.println("COmma Index :: "+indexComma); String IV = decodedcompleteEncodedKey.substring(0, indexComma); String Key = decodedcompleteEncodedKey.substring(indexComma+1,decodedcompleteEncodedKey.length()); System.out.println("IV::: "+IV); System.out.println("Key::: "+Key); byte[] sessionKey = null; byte[] iv = null ; byte[] plaintext = encryptedText.getBytes(); Cipher cipher = null; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv)); byte[] ciphertext = cipher.doFinal(plaintext); } catch (IllegalBlockSizeException e) { System.out.println("IllegalBlockSizeException"); e.printStackTrace(); } catch (BadPaddingException e) { System.out.println("BadPaddingException"); e.printStackTrace(); } catch (InvalidKeyException e) { System.out.println("InvalidKeyException"); e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { System.out.println("InvalidAlgorithmParameterException"); e.printStackTrace(); } catch (NoSuchAlgorithmException e) { System.out.println("NoSuchAlgorithmException"); e.printStackTrace(); } catch (NoSuchPaddingException e) { System.out.println("NoSuchPaddingException"); e.printStackTrace(); } return null; }
但是现在我在线程“ main” java.lang.IllegalArgumentException中缺少异常:缺少参数。
任何人都可以帮助我解决这些错误。任何帮助,将不胜感激。谢谢!!
在将解密的.net代码转换为Java时,我在线程“主” java.lang.IllegalArgumentException中得到了异常Exception:javax.crypto.spec.SecretKeySpec缺少参数。
new SecretKeySpec(sessionKey, "AES")
调用了sessionKey = null
。