node.js - Getting error "wrong final block length" when decrypting AES256 cipher -


i'm facing same problem mentioned in this thread while encrypting , decrypting using aes.

crypto.js:202
var ret = this._handle.final(); ^ error: error:0606506d:digital envelope routines:evp_decryptfinal_ex:wrong final block length
@ error (native)
@ decipher.cipher.final (crypto.js:202:26)

these encrypt , decrypt functions:

var config = { cryptkey: crypto.createhash('sha256').update('nixnogen').digest(), iv: "a2xhcgaaaaaaaaaa" };  function encrypttext(text){     console.log(config.cryptkey);         var cipher = crypto.createcipheriv('aes-256-cbc', config.cryptkey, config.iv);         var crypted = cipher.update(text,'utf8','binary');         crypted += cipher.final('binary');     crypted = new buffer(crypted, 'binary').tostring('base64');         return crypted; }  function decrypttext(text){     console.log(config.cryptkey);         if (text === null || typeof text === 'undefined' || text === '') {return text;};     text = new buffer(text, 'base64').tostring('binary');         var decipher = crypto.createdecipheriv('aes-256-cbc', config.cryptkey, config.iv);         var dec = decipher.update(text,'binary','utf8');         dec += decipher.final('utf8');         return dec; } 

i've set "node": ">=0.10.0" in package.json.

can tell me how fix it? have tried solutions mentioned in thread none of them working me.

updates:

i've tried solutions mentioned in thread none of them working me. think there might different solution , hence, rather polluting existing thread, decided create new one. also, if continue on existing thread might confuse future candidates right solution.

update 2:

for second solution mentioned in thread, have following code, giving me same error:

function encrypttext(text){     console.log(config.cryptkey);         var cipher = crypto.createcipheriv('aes-256-cbc', config.cryptkey, config.iv);     return buffer.concat([         cipher.update(text),         cipher.final()     ]); }  function decrypttext(text){     console.log(config.cryptkey);         if (text === null || typeof text === 'undefined' || text === '') {return text;};         var decipher = crypto.createdecipheriv('aes-256-cbc', config.cryptkey, config.iv);     return buffer.concat([         decipher.update(text),         decipher.final()     ]); } 

also, i'm using these functions getters , setters field in mongodb database using mongoose. don't think doing cause issues, still thought idea mention this.

update 3:

i'm trying encrypt facebook access token , decrypt encrypted facebook access token.

to reproduce error, can try encrypting token:

asdfghjklo0cbacjdjoc25hkzazcofxhtbvphrva4hnflyewahshzci2qmihyxps2fidgsqacalfhllo273owimzafo9tmyzcbuzabjkzpoo4hza8hrjvcrace6iunmbsmaeggvv8kclkibl6gf7hjy9ppleni2ij06vozbv0fkxuvkp1k7gwymva1zaybswidikchjq3yh1zcdwwedrfgthyj

encryption work fine , encrypted string.

now try decrypting encrypted string got in previous step. error.

this question 2 years old @ time of writing, has quite few views, hope answer still prove useful might come across it.

the problem here encrypttext works fine, it's not returning string. it's returning buffer. decrypttext expecting string, not buffer, tries read though buffer , error received.

this simple fix. need serialise buffer string when encrypt text, , deserialise encrypted string receive when decrypt text.

in example, use base64 encoding because compact when representing binary data.

var config = {   cryptkey: crypto.createhash('sha256').update('nixnogen').digest(),   iv: 'a2xhcgaaaaaaaaaa' }  function encrypttext (text) {   console.log(config.cryptkey)   var cipher = crypto.createcipheriv('aes-256-cbc', config.cryptkey, config.iv)   return buffer.concat([     cipher.update(text),     cipher.final()   ]).tostring('base64') // output base64 string }  function decrypttext (text) {   console.log(config.cryptkey)   if (text === null || typeof text === 'undefined' || text === '') {     return text   }   var decipher = crypto.createdecipheriv('aes-256-cbc', config.cryptkey, config.iv)   return buffer.concat([     decipher.update(text, 'base64'), // expect `text` base64 string     decipher.final()   ]).tostring() }  var encrypted = encrypttext('text') // 8xxus7jlg6crqjfhhb5j5a== var decrypted = decrypttext(encrypted) // text 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -