Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Error in crypto deciphering

Error in crypto deciphering

Problem

Why does the following code throw up a DecipherFinal error in crypto -

  var crypto = require('crypto');
  c=new Date;
  x= (c.getTime()+"."+c.getMilliseconds()).toString()+".uIn";
  key = 'sevsolut'
        , plaintext = x
        , cipher = crypto.createCipher('aes-256-cbc', key)
        , decipher = crypto.createDecipher('aes-256-cbc', key);
  cipher.update(plaintext, 'utf8', 'base64');
  var encryptedPassword = cipher.final('base64')

  decipher.update(encryptedPassword, 'base64', 'utf8');
  var decryptedPassword = decipher.final('utf8');

  console.log('encrypted :', encryptedPassword);
  console.log('decrypted :', decryptedPassword);
Problem courtesy of: digster

Solution

You need to take the output from update:

var crypto = require('crypto');
c=new Date();
x= (c.getTime()+"."+c.getMilliseconds()).toString()+".uIn";
key = "sevsolut"
      , plaintext = x
      , cipher = crypto.createCipher('aes-256-cbc', key)
      , decipher = crypto.createDecipher('aes-256-cbc', key);
var encryptedPassword = cipher.update(plaintext, 'utf8', 'base64');
encryptedPassword += cipher.final('base64')

var decryptedPassword = decipher.update(encryptedPassword, 'base64', 'utf8');
decryptedPassword += decipher.final('utf8');

console.log('encrypted :', encryptedPassword);
console.log('decrypted :', decryptedPassword);
Solution courtesy of: laktak

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Error in crypto deciphering

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×