我正在尝试使用 crypto-js 来管理来自 Salesforce 的数据的加密和解密。 我在网上检查了库文档和几个“指南”。 lib 文档没有讨论如何使用 IV,而其他指南则非常令人困惑。 我最初尝试使用简单的加密解密流程,但我似乎无法做到正确。解密后的值始终为空。
现在多亏了 Topaco,我将密钥更改为 12 个字符长,现在我可以加密/解密了。
我还必须稍微调整解密部分,传递一个密码对象。
IV 与 Salesforce 上的 IV 类似(长度相同),我猜他们是这样做的
Blob.valueOf("11111111");
我需要与他们核实他们使用参数的准确程度,以及他们是否进行了多次加密才能使用他们的示例数据。我会更新问题。
这是我的代码。
const key = enc.Utf8.parse('Harold%Finch');
const iv = enc.Utf8.parse('11111111');
const secret = enc.Utf8.parse('[email protected]');
const encrypted = AES.encrypt(secret, key, {
iv: iv,
mode: mode.CBC,
padding: pad.Pkcs7,
});
const encryptedString = encrypted.toString();
// "2VxmNeqlTolVpgHrk+tdEO3+xS8XOjClikUjB+zAGis="
// Convert the base64-encoded string to a CipherParams object
const ciphertext = enc.Base64.parse(encryptedString);
const cipherParams = lib.CipherParams.create({ ciphertext });
const decrypted = AES.decrypt(cipherParams, key, {
iv: iv,
mode: mode.CBC,
padding: pad.Pkcs7,
});
// Convert the decrypted data to a string (UTF-8)
const decryptedString = decrypted.toString(enc.Utf8);
console.info('decrypted', decryptedString);
// "[email protected]"
闪电战这里