试图用加密js打印六角字符串
我正在尝试使用cryptojs加密某些内容,然后生成加密文本的十六进制字符串。
function EncryptAES(text, key) {
var encrypted = CryptoJS.AES.encrypt(text, key);
return CryptoJS.enc.Hex.stringify(encrypted);
}
var encrypted = EncryptAES("Hello, World!", "SuperSecretPassword");
console.log(encrypted);
但是,将空白行打印到控制台上,而不是十六进制的字符串。我在做什么错?
I am trying to use CryptoJS to encrypt something and then generate a hexadecimal string of the encrypted text.
function EncryptAES(text, key) {
var encrypted = CryptoJS.AES.encrypt(text, key);
return CryptoJS.enc.Hex.stringify(encrypted);
}
var encrypted = EncryptAES("Hello, World!", "SuperSecretPassword");
console.log(encrypted);
However, instead of a hexadecimal string, a blank line is printed to the console. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cryptojs.aes.encrypt()
返回cipherparams
对象将几个数据封装,包括ciphertext aswordarray
(s。在这里)。默认情况下,.toString()
返回WordArray的十六进制数据
:请注意,在您的示例中,关键材料以字符串的形式传递,因此被解释为密码(s。在这里),通过键推导函数推断键和IV,并与a andan> Random 8字节盐结合使用,这就是为什么Ciphertext每次在相同的输入数据中每次更改的原因。
因此,解密不仅需要密文,还需要盐,这也封装在
cipherparams
对象中。对于
cipherparams
对象,.toString()
返回基本64编码的openssl格式中的数据,该格式由ASCII编码salted> salted __
的编码,然后是8字节盐和实际的密文,因此包含解密所需的所有信息。CryptoJS.AES.encrypt()
returns aCipherParams
object that encapsulates several data, including the ciphertext asWordArray
(s. here). By default,.toString()
returns the hex encoded data for aWordArray
:Note that in your example the key material is passed as string and therefore interpreted as passphrase (s. here), inferring key and IV via a key derivation function in conjunction with a random 8 bytes salt, which is why the ciphertext changes each time for the same input data.
Therefore, decryption requires not only the ciphertext but also the salt, which is also encapsulated in the
CipherParams
object.For a
CipherParams
object,.toString()
returns the data in the Base64 encoded OpenSSL format consisting of the ASCII encoding ofSalted__
followed by the 8 bytes salt and the actual ciphertext, and thus contains all the information needed for decryption.