decrypt using cryptojs not working
Thanks owlstead, I already figured that out it last couple of days.
I raised this query in crypto-js thread, the problem was I was not specifying IV in crypto-js decryption, as in Ruby if you dont specify any IV it adds \0 as IV.
Below is working code
var encrypted = {};
encrypted.ciphertext = CryptoJS.enc.Base64.parse(data.data);
var decrypted = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Base64.parse(BASE64_ENCODED_KEY),
{ iv: CryptoJS.enc.Hex.parse('00000000000000000000000000000000') });;
console.log(decrypted.toString(CryptoJS.enc.Utf8));
This worked for me, this solution was provided to me yesterday by Jeff.Mott.OR (current cryptojs lead dev)
Sudesh
I m just an enthusiast who enjoys solving various problems using any technology i can understand. I basically hail from web development world using PHP mainly and now off lately python. I have never got anybody to guide me on various technology or open source world. I believe i m good but an introvert kinda so always stands in a corner in crowd always sorting my problems myself because never got anybody. but hey i m not cribbing about it i enjoy calling myself self made. I would definitely like to get a mentor who can guide me would be great. That's me. Sudesh
Updated on June 04, 2022Comments
-
Sudesh almost 2 years
I m trying to send a encrypted data via json to client and decrypt it in client using cryptojs
My ROR code
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
My Javascript code
var key = btoa(BASE64_ENCODED_KEY); $http({method: 'GET', url: '/appi/getdata/', params: {SOME_PARAMS}}) .success(function(data, status, headers, config) { var dat = btoa(data.data); var decrypted = CryptoJS.AES.decrypt(dat, key); console.log(decrypted.toString(CryptoJS.enc.Utf8)); });
Getting javascript error "Error: Malformed UTF-8 data." in chrome
Below is a url for simplified jsfiddle for above query
-
Amit Kumar over 10 yearsdecrypted.toString(CryptoJS.enc.Utf8) works for me. I was using decrypted.toString(). Anyway thanks....
-
Catmandu over 10 years@sudesh it seems to be a problem..Think CryptoJs cant encrypt JSON objects..Can u please help me..I am getting the same issue
-
Catmandu over 10 years@sudesh where should i post it ..its quite large..can u come in chat
-
Catmandu over 10 yearsvar data = angular.fromJson(strData); var encrypted = {}; encrypted.ciphertext = CryptoJS.enc.Base64.parse(data.data); var decrypted = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Base64.parse(pki),{ iv: CryptoJS.enc.Base64.parse(data.pki) }); var mainData = decrypted.toString(CryptoJS.enc.Utf8); return angular.fromJson(mainData); }].concat($http.defaults.transformResponse), params: default_params, headers: headers }).success(function (data) { var endApi = new Date(); callback(data); });
-
Catmandu over 10 years@Sudesh Can you please guide me with this..Am stuck for a long time now
-
Sudesh over 10 years@PeeVee after seeing the code you posted, it is same script I coded for a website, seems your bosses know me.. I m just a call away
-
Catmandu over 10 years@Sudesh Thanks for the reply..We got to know the issue and have worked on the solution..The issue was chrome specific as the page was reloading as one of the image path specified was not proper. Actually I am not aware that this was the same code you worked on..Have you worked on the whole application