来自前端(角度)的加密和来自Java的解密

问题描述 投票:0回答:1

这是我使用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 angular encryption cryptography cryptojs
1个回答
0
投票

我已经检查了,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服务。

© www.soinside.com 2019 - 2024. All rights reserved.