On Sun, Jun 2, 2013 at 7:04 PM, kousik kumar <[email protected]> wrote:
> I have this following piece of code which stores data in an encrypted format
> and decrypts when given the passwd. When i pass in the text which is less
> than 8 bytes, say for example (a b c), it gets encrypted and decrypted
> without any problem. If i use a larger string (hello there), am getting bad
> decrypt error while decrypting. What am is missing?
>
>
> function storeContent(postData, store, response) {
>
>
> var hash = crypto.createHash('sha1');
>
> hash.update(postData, 'utf8');
>
> var passwd = hash.digest('hex');
>
> console.log(passwd);
>
>
> var cypher = crypto.createCipher('aes192', passwd);
>
> cypher.update(postData, 'utf8', 'hex');
>
> var encrypt = cypher.final('hex');
>
> store[passwd] = encrypt;
>
> console.log(encrypt);
>
>
> response.writeHead(200, {"Content-Type": "text/plain"});
>
> response.write("Your content is securely stored, use " + passwd +
>
> " to retrieve it back");
>
> response.end();
>
>
> }
>
>
> function retrieveContent(passwd, store, response) {
>
>
> encrypt = store[passwd];
>
>
> console.log(store);
>
> if (!encrypt) {
>
>
> response.writeHead(200, {"Content-Type": "text/plain"});
>
> response.write("Incorrect passwd");
>
> response.end();
>
>
> } else {
>
> var decypher = crypto.createDecipher('aes192', passwd);
>
> console.log(encrypt);
>
> decypher.update(encrypt, 'hex', 'utf8');
>
>
> response.writeHead(200, {"Content-Type": "text/plain"});
>
> response.write("Stored content: " + decypher.final('utf8'));
>
> response.end();
>
> }
>
> }
>
>
>
> crypto.js:286
>
>   var ret = this._binding.final();
>
>                           ^
>
> TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad
> decrypt
>
>     at Decipher.Cipher.final (crypto.js:286:27)
>
>     at Object.retrieveContent
> (/Users/kousik/Documents/workspace/EasyCopy/routes/secure.js:45:48)
>
>     at Object.respond [as /respond]
> (/Users/kousik/Documents/workspace/EasyCopy/routes/requestHandlers.js:53:9)
>
>     at route
> (/Users/kousik/Documents/workspace/EasyCopy/routes/router.js:8:19)
>
>     at IncomingMessage.<anonymous>
> (/Users/kousik/Documents/workspace/EasyCopy/routes/server.js:20:4)
>
>     at IncomingMessage.EventEmitter.emit (events.js:92:17)
>
>     at _stream_readable.js:910:16
>
>     at process._tickCallback (node.js:415:13)
>
>
>
> Thanks
>
> Kousik

You forgot to concatenate the output of Cipher#update() and
Cipher#final().  Same for Decipher.

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to