Scrungepipes
Scrungepipes

Reputation: 37590

CryptoJS: Converting encryption result to hex, then back, then can no longer decrypt the data

I'm encoding some text using CryptoES (identical API/functionality to CryptoJS), then decoding it as a check/test as I go along. The following code successfully encodes and then decodes some input:

const dataToEncrypt = "something";    
const encryptionKeyAsHexString = "2EE1F10212ADD4BEB0E9FED2FEA24733";
const ivAsHexString = "B8C67C19F6E3AE23D4E11E5B376FA891"; 
  
const encryptedData = CryptoES.AES.encrypt(dataToEncrypt, CryptoES.enc.Hex.parse(encryptionKeyAsHexString), { 
      mode: CryptoES.mode.CBC,
      padding: CryptoES.pad.Pkcs7, 
      iv: CryptoES.enc.Hex.parse(ivAsHexString)
  }
);

const decryptedCheck1 = CryptoES.AES.decrypt( encryptedData,   CryptoES.enc.Hex.parse(encryptionKeyAsHexString), {
  mode: CryptoES.mode.CBC, 
  padding: CryptoES.pad.Pkcs7,
  iv: CryptoES.enc.Hex.parse(ivAsHexString) 
} );
const decryptedCheck1Text = decryptedCheck1.toString( CryptoES.enc.Utf8 );
console.log("decryptedCheck1Text:  ", decryptedCheck1Text);

However if I add an extra step to the above code to hex encode the encrypted data, then I can no longer decrypt it using the following code:

const encryptedData = CryptoES.AES.encrypt(dataToEncrypt, CryptoES.enc.Hex.parse(encryptionKeyAsHexString), { 
          mode: CryptoES.mode.CBC,
          padding: CryptoES.pad.Pkcs7, 
          iv: CryptoES.enc.Hex.parse(ivAsHexString)
      }
    );
    
// Convert the encrypted data to hex
const encryptedDataAsHexString =  CryptoES.enc.Hex.stringify(encryptedData.ciphertext);
    
const decryptedCheck2 = CryptoES.AES.decrypt( CryptoES.enc.Hex.parse(encryptedDataAsHexString), CryptoES.enc.Hex.parse(encryptionKeyAsHexString), {
      mode: CryptoES.mode.CBC, 
      padding: CryptoES.pad.Pkcs7,
          iv: CryptoES.enc.Hex.parse(ivAsHexString) 
      } 
);
        
const decryptedCheck2Text = decryptedCheck2.toString( CryptoES.enc.Utf8 );
console.log("decryptedCheck2Text: ", decryptedCheck2Text);

In the second block of code encryptedData.ciphertext is converted to Hex the result of which is passed to the decrypt() function as CryptoES.enc.Hex.parse(encryptedDataAsHexString) which ought to reverse the hexing?

But something is incorrect and there is nothing in decryptedCheck2Text. Why is the second chunk of code no longer working to decrypt the data?

I've experimented with several variant (such as using CryptoES.enc.Hex.stringify(encryptedData) instead of just the .cipherText part) but I can't get anything to work.

Upvotes: 1

Views: 751

Answers (1)

Scrungepipes
Scrungepipes

Reputation: 37590

I found it was necessary to base64 encode the hex before passing it as a parameter, like this:

const tempWordArray = CryptoES.enc.Hex.parse(encryptedDataAsHexString);
const tempBase64 = CryptoES.enc.Base64.stringify(tempWordArray)
  const decryptedCheck2 = CryptoES.AES.decrypt( tempBase64, CryptoES.enc.Hex.parse(encryptionKeyAsHexString)
  ...

Upvotes: 1

Related Questions