0

I'm using below code to encrypt data using CryptoJS

export const encryptData = (data) => {
    let key = CryptoJS.enc.Utf8.parse(api.encryption.Key);
    let iv = {
        keySize: 128 / 8,
        iv: CryptoJS.enc.Utf8.parse(api.encryption.IV),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    };
    return encodeURIComponent(CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(data), key, iv));
}

And for decrypting the data i used below code. But data is not getting decrypted. However encrypt is working fine.

export const decryptData = (data) => {
    let key = CryptoJS.enc.Utf8.parse(api.encryption.Key);
    let iv = {
        keySize: 128 / 8,
        iv: CryptoJS.enc.Utf8.parse(api.encryption.IV),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    };
    return decodeURIComponent(CryptoJS.AES.decrypt(CryptoJS.enc.Utf8.parse(data), key, iv));
}

Any idea what I am doing wrong while decrypting?

1 Answer 1

1

I think the issue is that when you encrypt you return encodeURIComponent string, but when you decrypt you decode such string too late.

export const decryptData = (data) => {
  let key = CryptoJS.enc.Utf8.parse(api.encryption.Key);
  let iv = {
      keySize: 128 / 8,
      iv: CryptoJS.enc.Utf8.parse(api.encryption.IV),
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
  };
  return CryptoJS.AES.decrypt(decodeURIComponent(data), key, iv).toString(CryptoJS.enc.Utf8);
}

this version, instead, decode data before passing it to decrypt, otherwise you decrypt something that has been encoded, hence it's not usable for decrypt.

Sign up to request clarification or add additional context in comments.

8 Comments

If i try with above code snippet, i'm getting output as - decrypt -> WordArray.int {words: Array(0),sigBytes:0}
not sure about that, but do you understand why you need to decode data bevore passing it around? the encrypted data returns an encoded string, and that is the string you need to decode before decrypting.
i'm also not understanding. Is there any other alternative approach?
if all you need is to encrypt/decrypt with a specific key you can try a webcrypto based library such as secretly (search on github) ... it gives you obj.encrypt(clean) and obj.decrypt(encrypted) out of the box
CryptoJS.AES.decrypt() expects the ciphertext as a CipherParams object or by default as a Base64 encoded string (which is implicitly converted), i.e. the data must not be parsed with the UTF8 encoder, see the documentation cryptojs.gitbook.io/docs/#the-cipher-input. Instead, the decrypted data must be UTF-8 decoded: return CryptoJS.AES.decrypt(decodeURIComponent(data), key, iv).toString(CryptoJS.enc.Utf8); - @SnehaBoopal
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.