Export to GitHub

crypto-js - issue #91

AES Decryption not working


Posted on Aug 7, 2013 by Happy Dog

Below is my javascript code

var key64 = "avIPFtMr6mYhQ2OD3vyHrg=="; // Base64 encoded key generated via aes.random_key ruby

var data = "xE1E9BKoUM00zxcjns8eBQ=="; //actual data : SOME DATA

var decrypted = CryptoJS.AES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(data) }, CryptoJS.enc.Base64.parse(key64)); console.log('Text: ', CryptoJS.enc.Latin1.stringify(decrypted));

Expected output is "SOME DATA"

I m using latest aes build

This key and data decrypts in PHP but for some reason not working in javascript console. Is their something wrong that i m doing here.

Comment #1

Posted on Aug 7, 2013 by Happy Horse

Where's the IV? What mode were you expecting to use? CryptoJS's default is CBC. What padding scheme were you expecting to use? CryptoJS's default is PKCS7.

Comment #2

Posted on Aug 7, 2013 by Happy Dog

Thanks for quick reply

Frankly i m quite naive. Below is the ruby code used to generate those

def getkey

aes = OpenSSL::Cipher::Cipher.new('AES-128-CBC') aes.encrypt key = aes.random_key

session[:key] = key

render :json => {:mkey => Base64.encode64(key).gsub(/\n/, '')} end

def getdata js = "SOME DATA"

aes = OpenSSL::Cipher::Cipher.new('AES-128-CBC')
aes.encrypt
aes.key = session[:key]
encrypted = aes.update(js) + aes.final

encrypted = Base64.encode64(encrypted).gsub(/\n/, '')

render :json => {:data => encrypted}

end

getkey generates key and getdata encrypts data

By default im using CBC mode and 128 bit

Comment #3

Posted on Aug 7, 2013 by Happy Dog

I think ruby OpenSSL::Cipher applies PKCS#5 padding by default.

Comment #4

Posted on Aug 7, 2013 by Happy Horse

The Ruby documentation seems to indicate that when an IV isn't provided, then it uses an all-zero IV. That's probably not the behavior you want, but nonetheless if you wanted to replicate it in JS...

var decrypted = CryptoJS.AES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(data) }, CryptoJS.enc.Base64.parse(key64), { iv: CryptoJS.enc.Hex.parse('00000000000000000000000000000000') });

Comment #5

Posted on Aug 8, 2013 by Happy Dog

Thank you for helping me out, it worked.

Greatly appreciated, where you could have easily disregarded this as not cryptojs bug you helped me sort it.

Comment #6

Posted on Aug 13, 2013 by Happy Horse

(No comment was entered for this change.)

Status: Invalid

Labels:
Type-Defect Priority-Medium