Cryptojs AES解密十六进制字符串?

发布于 2025-01-28 15:17:09 字数 542 浏览 1 评论 0原文

这是问题的第二部分,问题试图用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2025-02-04 15:17:09

cryptojs使用WordArray内置数据类型,并提供 encoders 用于转换。如果要将十六进制的字符串转换为WordArray,则可以使用十六进制编码器,如下所示:

CryptoJS.enc.Hex.parse(<your hex encoded string>)

相反的方式

<your WordArray>.toString(CryptoJS.enc.Hex)

或相反的

<your WordArray>.toString()

crage> cryptojs.aes.cens.cens.encrypt()返回cipherparams封装Ciphertext和Salt等的对象。此cipherparams对象将传递给cryptojs.aes.decrypt()进行解密。

因此,您想要的内容可以如下实现:

function EncryptAES(text, key) {
    var encrypted = CryptoJS.AES.encrypt(text, key);
    return {ciphertext: encrypted.ciphertext.toString(), salt: encrypted.salt.toString()}
}

function DecryptAES(ciphertext, salt, key){
    var decrypted = CryptoJS.AES.decrypt({ciphertext:CryptoJS.enc.Hex.parse(ciphertext), salt:CryptoJS.enc.Hex.parse(salt)}, key); 
    return decrypted;
}

var {ciphertext, salt} = EncryptAES('The quick brown fox jumps over the lazy dog', 'my passphrase');
console.log('Ciphertext (hex): ', ciphertext);
console.log('Salt (hex): ', salt);

var decrypted = DecryptAES(ciphertext, salt, 'my passphrase');
console.log('Decrypted: ', decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>


但是,有一种更简单的方法来处理盐和密文。对于cipherparams对象,.toString()返回基本64编码的openSSL格式中的数据,该格式由salted> salted ___的十六进制编码和8字节盐和实际的密文。 cryptojs.aes.decrypt()隐式将这种base64字符串转换为cipherparams对象:

var encrypted = CryptoJS.AES.encrypt('The quick brown fox jumps over the lazy dog', 'my passphrase').toString();
console.log("Salt, ciphertext (OpenSSL format, Base64): ", encrypted);

var decrypted = CryptoJS.AES.decrypt(encrypted, 'my passphrase');
console.log("Decrypted: ", decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

CryptoJS uses the WordArray data type internally and provides encoders for conversion. If you want to convert a hex encoded string into a WordArray, this is possible with the Hex encoder as follows:

CryptoJS.enc.Hex.parse(<your hex encoded string>)

or the other way around

<your WordArray>.toString(CryptoJS.enc.Hex)

or for short

<your WordArray>.toString()

Moreover, CryptoJS.AES.encrypt() returns a CipherParams object that encapsulates ciphertext and salt, among other things. This CipherParams object is to be passed to CryptoJS.AES.decrypt() for decryption.

With this, what you want can be implemented as follows:

function EncryptAES(text, key) {
    var encrypted = CryptoJS.AES.encrypt(text, key);
    return {ciphertext: encrypted.ciphertext.toString(), salt: encrypted.salt.toString()}
}

function DecryptAES(ciphertext, salt, key){
    var decrypted = CryptoJS.AES.decrypt({ciphertext:CryptoJS.enc.Hex.parse(ciphertext), salt:CryptoJS.enc.Hex.parse(salt)}, key); 
    return decrypted;
}

var {ciphertext, salt} = EncryptAES('The quick brown fox jumps over the lazy dog', 'my passphrase');
console.log('Ciphertext (hex): ', ciphertext);
console.log('Salt (hex): ', salt);

var decrypted = DecryptAES(ciphertext, salt, 'my passphrase');
console.log('Decrypted: ', decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>


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 of Salted__ followed by the 8 bytes salt and the actual ciphertext. CryptoJS.AES.decrypt() implicitly converts such a Base64 string into a CipherParams object:

var encrypted = CryptoJS.AES.encrypt('The quick brown fox jumps over the lazy dog', 'my passphrase').toString();
console.log("Salt, ciphertext (OpenSSL format, Base64): ", encrypted);

var decrypted = CryptoJS.AES.decrypt(encrypted, 'my passphrase');
console.log("Decrypted: ", decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

街角迷惘 2025-02-04 15:17:09

Cryptojs期望其加密数据具有一定的格式。如果您仅提供密文的十六进制字符串,则需要在解密时重建此格式化数据。这是一个示例:

function EncryptAES(text, key) {
  var encrypted = CryptoJS.AES.encrypt(text, key);
  return encrypted.toString();  // this will be a string includes salt, iv and ciphertext
}

function DecryptAES(ciphertext, key) {
  var decrypted = CryptoJS.AES.decrypt(ciphertext, key);
  return decrypted.toString(CryptoJS.enc.Utf8); // decode to original text
}

在Encryptaes函数中,通过调用eNcrypted.toString(),它将生成一个包括盐,iv和ciphertext的字符串。当我们解密它时,我们不需要分别处理盐和IV。

但是,如果您仅处理密文(十六进制字符串),则可以使用以下方式解密:

function DecryptFromHex(ciphertextHex, key, iv) {
  var cipherParams = CryptoJS.lib.CipherParams.create({
    ciphertext: CryptoJS.enc.Hex.parse(ciphertextHex)
  });
  var decrypted = CryptoJS.AES.decrypt(cipherParams, CryptoJS.enc.Utf8.parse(key), { iv: CryptoJS.enc.Utf8.parse(iv) });
  return decrypted.toString(CryptoJS.enc.Utf8);
}

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:

function EncryptAES(text, key) {
  var encrypted = CryptoJS.AES.encrypt(text, key);
  return encrypted.toString();  // this will be a string includes salt, iv and ciphertext
}

function DecryptAES(ciphertext, key) {
  var decrypted = CryptoJS.AES.decrypt(ciphertext, key);
  return decrypted.toString(CryptoJS.enc.Utf8); // decode to original text
}

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:

function DecryptFromHex(ciphertextHex, key, iv) {
  var cipherParams = CryptoJS.lib.CipherParams.create({
    ciphertext: CryptoJS.enc.Hex.parse(ciphertextHex)
  });
  var decrypted = CryptoJS.AES.decrypt(cipherParams, CryptoJS.enc.Utf8.parse(key), { iv: CryptoJS.enc.Utf8.parse(iv) });
  return decrypted.toString(CryptoJS.enc.Utf8);
}

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 for encryption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文