背景:我正在处理的应用程序应该脱机工作。我有一个HTML5页面,用户输入的数据使用密码-js库加密。我希望加密的消息被发送到,然后在服务器端解密它。
我可以使用Crypto-js加密消息
<code>
var message = "my message text";
var password = "user password";
var encrypted = CryptoJS.AES.encrypt( message ,password );
console.log(encrypted.toString());
// this prints an encrypted text "D0GBMGzxKXU757RKI8hDuQ=="
</code>我想要做的是将加密的文本"D0GBMGzxKXU757RKI8hDuQ==“传递给java服务器端代码,并对加密的消息进行解密。
我在java服务器端尝试了许多方法来解密加密消息。请在服务器端找到我的代码,以便对加密的文本进行解密。
<code>
public static String decrypt(String keyText,String encryptedText) 
{
// generate key 
Key key = new SecretKeySpec(keyText.getBytes(), "AES");
Cipher chiper = Cipher.getInstance("AES");
chiper.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
byte[] decValue = chiper.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}  
</code>我从下面的代码调用java方法解密。
<code>
// performs decryption 
public static void main(String[] args) throws Exception 
{
String decryptedText = CrypterUtil.decrypt("user password","D0GBMGzxKXU757RKI8hDuQ==");
}
</code>但是,当我运行java解密代码时,我会得到以下异常
<code>
Exception in thread "main" java.security.InvalidKeyException: Invalid AES key length: 13 bytes
at com.sun.crypto.provider.AESCipher.engineGetKeySize(AESCipher.java:372)
at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1052)
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1010)
at javax.crypto.Cipher.implInit(Cipher.java:786)
at javax.crypto.Cipher.chooseProvider(Cipher.java:849)
at javax.crypto.Cipher.init(Cipher.java:1213)
at javax.crypto.Cipher.init(Cipher.java:1153)
at au.gov.daff.pems.model.utils.CrypterUtil.decrypt(CrypterUtil.java:34)
at au.gov.daff.pems.model.utils.CrypterUtil.main(CrypterUtil.java:47)
Process exited with exit code 1.
</code>我不知道我做错了什么?使用密码-js库对消息进行加密的最佳方法是什么,以便在使用用户输入密码的地方对其进行解密。
https://stackoverflow.com/questions/27179685
复制相似问题