gpt4 book ai didi

javascript - Android 上基于密码的 AES 加密和使用 CryptoJS 解密

转载 作者:行者123 更新时间:2023-11-28 14:49:36 32 4
gpt4 key购买 nike

在 Android 上,我可以毫无问题地加密消息并获取 iv。

String Test = "Lorem ipsum dolor sit amet, ...";
String password = "test";

KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(password.getBytes("UTF8"));
kgen.init(256, sr);
SecretKey skey = kgen.generateKey();

Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] decrypted = c.doFinal(Test.getBytes());
decrypted = Base64.encodeBase64(decrypted);
byte[] iv = Base64.encodeBase64(c.getIV());
Log.d("encryptString", new String(decrypted));
Log.d("encryptString iv", new String(iv));

输出示例:

encryptString: 2NVoJzMkPphwUJc2h/4LfsmAwyJlejbWKGLG2ACNbaI=
encryptString iv: YX5SF+cFwzv1I4OiGrJk3A==

当我转到 JavaScript 端时,我首先将 base64 编码转换为字节。然后我通过 CryptoJS AES Decrypt 函数运行它。

var decrypt = CryptoJS.enc.Base64.parse("2NVoJzMkPphwUJc2h/4LfsmAwyJlejbWKGLG2ACNbaI=");
var iv = CryptoJS.enc.Base64.parse("YX5SF+cFwzv1I4OiGrJk3A==");
var password = "test";

var encrypted = CryptoJS.AES.decrypt(decrypt.toString(), password, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});

输出始终为空。 Android 上是否还有其他我还需要传递给 CryptoJS 的东西?

最佳答案

发现问题了,并不是padding的问题。

正如其他人所说,这与我使用 SecureRandom.getInstance("SHA1PRNG") 的事实有关。

我更正了代码中的问题,即在 Android 上使用 PBEKeySpec 和在 CryptoJS 上使用 PBKDF2 生成 key 。然后只需执行相同的步骤即可:

String Test = "Lorem ipsum dolor sit amet, ...";
String password = "test";

byte[] salt = new String("12345678").getBytes("Utf8");
int iterationCount = 2048;
int keyStrength = 256;

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyStrength);
SecretKey tmp = factory.generateSecret(spec);

Log.d("encryptString Key: ", new String(Base64.encodeBase64(tmp.getEncoded())));

Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, tmp);
byte[] decrypted = c.doFinal(Test.getBytes());
decrypted = Base64.encodeBase64(decrypted);
byte[] iv = c.getIV();

Log.d("encryptString: ", new String(decrypted));
Log.d("encryptString iv:", new String(Base64.encodeBase64(iv)));

Android 上运行代码的示例输出:

encryptString Key:: ueTU6u4PXbm86zy+UtlQfeh55xZorA58W3fKKBypheM=
encryptString:: ii8UNoi4xG1zGC8RyzHKu6JMkxixkK7LTPxGMaCHGNk=
encryptString iv:: nwy2VHctPnXOd/rahPFiWg==

现在我们在 JavaScript 中生成相同的 PBKDF2 key ,并将上面的输出输入到下面的示例代码中:

var salt = CryptoJS.enc.Utf8.parse("12345678");
var password = "test";
var keyBits = CryptoJS.PBKDF2(password, salt, {
hasher: CryptoJS.algo.SHA1,
keySize: 8,
iterations: 2048
});

var iv = CryptoJS.enc.Base64.parse("nwy2VHctPnXOd/rahPFiWg==");
var message = CryptoJS.enc.Base64.parse("ii8UNoi4xG1zGC8RyzHKu6JMkxixkK7LTPxGMaCHGNk=");

var encrypted = CryptoJS.AES.decrypt("ii8UNoi4xG1zGC8RyzHKu6JMkxixkK7LTPxGMaCHGNk=", keyBits, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});

console.log(encrypted.toString(CryptoJS.enc.Utf8));
<!doctype html>

<html>

<head>
</head>

<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/pbkdf2.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/aes.min.js"></script>
</body>

</html>

JavaScript 中的输出:

"Lorem ipsum dolor sit amet, ..."

关于javascript - Android 上基于密码的 AES 加密和使用 CryptoJS 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034471/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com