Cryptojs AES解密十六进制字符串?
这是问题的第二部分,问题试图用cryptojs < /a>
基本上,我有一个使用Cryptojs加密消息并将Cypher文本作为十六进制字符串吐出的函数。
function EncryptAES(text, key) {
var encrypted = CryptoJS.AES.encrypt(text, key);
return encrypted.ciphertext.toString()
}
我想编写一个函数解密,该功能将六角字符串,钥匙和盐(显然是我需要)返回原始明文。问题是我不知道如何将十六进制字符串重新回到cryptojs.aes.decrypt
的表单中,并且我找不到文档中的任何地方,它实际上可以解释如何说明做到这一点。也许我忽略了一些东西。请帮忙。
This is sort of a part two to the question Trying to print hex string with CryptoJS
Basically, I have a function that uses CryptoJS to encrypt a message and spit out the cypher text as a hex string.
function EncryptAES(text, key) {
var encrypted = CryptoJS.AES.encrypt(text, key);
return encrypted.ciphertext.toString()
}
I want to write a function DecryptAES that takes the hex string, key, and salt (which I apparently need) and returns the original plaintext. The problem is I can't figure out how to get the hex string back into a form that CryptoJS.AES.decrypt
will accept, and I can't find anywhere in the documentation where it actually explains how to do it. Maybe I overlooked something. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cryptojs使用
WordArray
内置数据类型,并提供 encoders 用于转换。如果要将十六进制的字符串转换为WordArray
,则可以使用十六进制编码器,如下所示:相反的方式
或相反的
,
crage> cryptojs.aes.cens.cens.encrypt()
返回cipherparams
封装Ciphertext和Salt等的对象。此cipherparams
对象将传递给cryptojs.aes.decrypt()
进行解密。因此,您想要的内容可以如下实现:
但是,有一种更简单的方法来处理盐和密文。对于
cipherparams
对象,.toString()
返回基本64编码的openSSL格式中的数据,该格式由salted> salted ___
的十六进制编码和8字节盐和实际的密文。cryptojs.aes.decrypt()
隐式将这种base64字符串转换为cipherparams
对象:CryptoJS uses the
WordArray
data type internally and provides encoders for conversion. If you want to convert a hex encoded string into aWordArray
, this is possible with the Hex encoder as follows:or the other way around
or for short
Moreover,
CryptoJS.AES.encrypt()
returns aCipherParams
object that encapsulates ciphertext and salt, among other things. ThisCipherParams
object is to be passed toCryptoJS.AES.decrypt()
for decryption.With this, what you want can be implemented as follows:
However, there is an easier way to handle salt and ciphertext. For a
CipherParams
object,.toString()
returns the data in the Base64 encoded OpenSSL format consisting of the hex encoding ofSalted__
followed by the 8 bytes salt and the actual ciphertext.CryptoJS.AES.decrypt()
implicitly converts such a Base64 string into aCipherParams
object:Cryptojs期望其加密数据具有一定的格式。如果您仅提供密文的十六进制字符串,则需要在解密时重建此格式化数据。这是一个示例:
在Encryptaes函数中,通过调用eNcrypted.toString(),它将生成一个包括盐,iv和ciphertext的字符串。当我们解密它时,我们不需要分别处理盐和IV。
但是,如果您仅处理密文(十六进制字符串),则可以使用以下方式解密:
Decryptfromhex采用十六进制的密文字符串,将其解析为Cryptojs可以理解的格式,然后解密它。请注意,键和IV也从UTF8解析为CryptoJS使用的WordArray格式。
确保键和IV用于
解密
匹配用于加密
的键。CryptoJS expects a certain format for its encrypted data. If you're supplying only the hexadecimal string of the ciphertext, you'll need to reconstruct this formatted data when decrypting. Here's an example:
In the EncryptAES function, by calling encrypted.toString(), it will generate a string which includes salt, iv, and ciphertext. When we decrypt it, we don't need to handle salt and iv separately.
However, if you are only dealing with the ciphertext (hexadecimal string), you can decrypt it using:
DecryptFromHex takes the hexadecimal ciphertext string, parses it back into a format that CryptoJS can understand, and then decrypts it. Note that the key and the iv are also parsed from UTF8 to the WordArray format that CryptoJS uses.
Ensure the key and iv you use for the
decryption
match those used forencryption
.