Search code examples
javascriptcryptojs

crypto.js what does "ciphertext" mean


I am reading this article about crypto.js.

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: ';' 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.

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.

I want ABCDE back again do you know how to do that?


Solution

  • 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.


    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>