Rohan Sharma
Rohan Sharma

Reputation: 1566

javascript 'hex' unknown behavior

while trying to pass a hex value to the crypto cipher in node js i am getting a blank return on ciphertext while proper return on ciphertext2. Can't explain what is the difference between the two cases.

var secret = 'YmcNFa37DrT+0p10pnSpQSytWxlqNCyU';
var cipher  = crypto.createCipher('des3', secret);
var plaintext = '3b9ac9ff';
var ciphertext = cipher.update(plaintext, 'hex', 'hex');
var ciphertext2 = cipher.update('3b9ac9ff', 'hex', 'hex');
console.log(plaintext + ' , ' +ciphertext + ' , '+ ciphertext2);

gives me an output

3b9ac9ff ,  , 0472620ba5ddf690

Upvotes: 2

Views: 106

Answers (1)

Rohan Sharma
Rohan Sharma

Reputation: 1566

Problem found. Pointed by @Andreas.

var ciphertext = cipher.update(plaintext, 'hex', 'hex');

should be

var ciphertext = cipher.update(plaintext, 'ascii', 'hex');

Two reasons which led to the confusion (although no justification for being stupid) 1. Node js doesn't give you a run time exception. Instead just behaves badly as pointed out above. 2. output is allowed as hex leading me to believe even input would be allowed.

Upvotes: 1

Related Questions