简体   繁体   English

crypto.js “密文”是什么意思

[英]crypto.js what does "ciphertext" mean

I am reading this article about crypto.js.我正在阅读有关 crypto.js 的这篇文章。

https://hibara.org/blog/2016/02/15/cryptojs/ https://hibara.org/blog/2016/02/15/cryptojs/

text = "ABCDE"
password = "pass"

var secret_passphrase = crypto.enc.Utf8.parse(this.password);
    var salt = crypto.lib.WordArray.random(128 / 8);
    var key128Bits500Iterations = crypto.PBKDF2(secret_passphrase, salt, {keySize: 128 / 8, iterations: 500 });
    var iv = crypto.lib.WordArray.random(128 / 8);
    var options = {iv: iv, mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7};
    var message_text = crypto.enc.Utf8.parse(this.text);
    var encrypted = crypto.AES.encrypt(message_text, key128Bits500Iterations, options);
    var binary_data = crypto.enc.Hex.stringify(salt);
    binary_data += (',' + crypto.enc.Hex.stringify(iv));
    binary_data += (',' + encrypted);

    console.log(binary_data)

    var array_rawData = binary_data.split(',');
    var salt = crypto.enc.Hex.parse(array_rawData[0]);
    var iv = crypto.enc.Hex.parse(array_rawData[1]);
    var encrypted_data = crypto.enc.Base64.parse(array_rawData[2]);
    var secret_passphrase = crypto.enc.Utf8.parse(this.password);
    var key128Bits500Iterations = crypto.PBKDF2(secret_passphrase, salt, {keySize: 128 / 8, iterations: 500 });
    var options = {iv: iv, mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7};
    var decrypted = crypto.AES.decrypt({"ciphertext":encrypted_data}, key128Bits500Iterations, options);

    console.log(decrypted)

But I am getting an error.但我收到一个错误。

Cannot find name 'ciphertext'.找不到名称“密文”。

and

TS1005: ';' TS1005: ';' expected.预期的。

What is ciphertext ?什么是密文? Does anyone know the way to solve this problem?有谁知道解决这个问题的方法?


Thank you so much for answering.非常感谢您的回答。

But it doesn't work properly.但它不能正常工作。

I think console.log(decrypted) should be "ABCDE" , but I got WordArray object.我认为 console.log(decrypted) 应该是 "ABCDE" ,但我得到了 WordArray 对象。

Do you know why?你知道为什么吗?


text = "ABCDE"
  password = "pass"

var secret_passphrase = CryptoJS.enc.Utf8.parse(this.password);
      //alert(secret_passphrase.toString(CryptoJS.enc.Utf8));
      var salt = CryptoJS.lib.WordArray.random(128 / 8);
      var key128Bits500Iterations =
          CryptoJS.PBKDF2(secret_passphrase, salt, {keySize: 128 / 8, iterations: 500 });
      //初期化ベクトル(ブロック長と同じ)
      var iv = CryptoJS.lib.WordArray.random(128 / 8);
      //暗号化オプション(IV:初期化ベクトル, CBCモード, パディングモード:PKCS7
      var options = {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7};
      //暗号化内容のエンコーディングは「UTF-8」
      var message_text = CryptoJS.enc.Utf8.parse(this.text);

      //----------------------------------------------------------------------
      //暗号化
      var encrypted = CryptoJS.AES.encrypt(message_text, key128Bits500Iterations, options);
      //----------------------------------------------------------------------

      //暗号結果データをカンマ(",")で結合してまとめる(復号時にわかるように)
      //(salt + iv + ciphertext)
      var binary_data = CryptoJS.enc.Hex.stringify(salt);
      binary_data += (',' + CryptoJS.enc.Hex.stringify(iv));
      binary_data += (',' + encrypted);

      var array_rawData = binary_data.split(',');

      var salt = CryptoJS.enc.Hex.parse(array_rawData[0]);  // パスワードSalt
      var iv = CryptoJS.enc.Hex.parse(array_rawData[1]);    // 初期化ベクトル(IV)
      var encrypted_data = CryptoJS.enc.Base64.parse(array_rawData[2]); //暗号化データ本体

      //パスワード(鍵空間の定義)
      var secret_passphrase = CryptoJS.enc.Utf8.parse(this.password);
      var key128Bits500Iterations =
          CryptoJS.PBKDF2(secret_passphrase, salt, {keySize: 128 / 8, iterations: 500 });

      //復号オプション(暗号化と同様)
      var options = {iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7};

      //復号
      var decrypted = CryptoJS.AES.decrypt({"ciphertext":encrypted_data}, key128Bits500Iterations, options);
      // 文字コードをUTF-8にする
      console.log(decrypted)

I changed my code.我改变了我的代码。

I thought inside of the decrypted would be "ABCDE" but inside of it there was WordArray object.我认为解密的内部将是“ABCDE”,但内部有 WordArray 对象。

I want ABCDE back again do you know how to do that?我想再次回到 ABCDE 你知道怎么做吗?

Ciphertext refers to the encrypted text.密文是指加密后的文本。

As for the error you're getting: there's an encoding error in your code (and in the article you've copied it from).至于您收到的错误:您的代码中存在编码错误(以及您从中复制的文章)。

This line:这一行:

var decrypted = CryptoJS.AES.decrypt({"ciphertext":encrypted_data}, key128Bits500Iterations, options);

Should be:应该:

var decrypted = CryptoJS.AES.decrypt({"ciphertext":encrypted_data}, key128Bits500Iterations, options);

Or even just:或者甚至只是:

var decrypted = CryptoJS.AES.decrypt({ciphertext:encrypted_data}, key128Bits500Iterations, options);

The JSFiddle that the article links to has the correct code.文章链接到的JSFiddle具有正确的代码。


Complete snippet:完整片段:

 const crypto = CryptoJS; const text = "ABCDE" const password = "pass" var secret_passphrase = crypto.enc.Utf8.parse(password); var salt = crypto.lib.WordArray.random(128 / 8); var key128Bits500Iterations = crypto.PBKDF2(secret_passphrase, salt, {keySize: 128 / 8, iterations: 500 }); var iv = crypto.lib.WordArray.random(128 / 8); var options = {iv: iv, mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7}; var message_text = crypto.enc.Utf8.parse(text); var encrypted = crypto.AES.encrypt(message_text, key128Bits500Iterations, options); var binary_data = crypto.enc.Hex.stringify(salt); binary_data += (',' + crypto.enc.Hex.stringify(iv)); binary_data += (',' + encrypted); console.log(binary_data); var array_rawData = binary_data.split(','); var salt = crypto.enc.Hex.parse(array_rawData[0]); var iv = crypto.enc.Hex.parse(array_rawData[1]); var encrypted_data = crypto.enc.Base64.parse(array_rawData[2]); var secret_passphrase = crypto.enc.Utf8.parse(password); var key128Bits500Iterations = crypto.PBKDF2(secret_passphrase, salt, {keySize: 128 / 8, iterations: 500 }); var options = {iv: iv, mode: crypto.mode.CBC, padding: crypto.pad.Pkcs7}; var decrypted = crypto.AES.decrypt({ciphertext:encrypted_data}, key128Bits500Iterations, options); console.log(decrypted.toString(CryptoJS.enc.Utf8));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM