这是我使用crypto-js
通过角度应用加密的内容。如何用Java解密?
return CryptoJS.AES.encrypt(JSON.stringify('my-data-as-string'), 'my-secret').toString();
Java代码不起作用:
String secret = "my-secret"; // 128 bit key
Key aesKey = new SecretKeySpec(secret.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encryptedStringFromAngular.getBytes()));
我已经检查了,Java部分工作正常。在我的情况下,加密的字符串为Base64
格式。
String secret = "1122334455667788"; // 128 bit key
Key aesKey = new SecretKeySpec(secret.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String encryptedStringFromAngular = "OSPNg+tRAAZzsTAGZiZ0mQ=="; //String encrypted in base64
byte[] bytes = Base64.getDecoder().decode(encryptedStringFromAngular);
String decrypted = new String(cipher.doFinal(bytes));
System.out.println(decrypted);
输出:
test data
为了加密数据,我使用了this服务。