2016-12-08 4 views
2

Ich bin total verwirrt, warum dies nicht funktioniert, ich bin Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decryptSymmetrische Verschlüsselung mit NodeJS

var crypto = require('crypto'); 
var key = "ciw7p02f70000ysjon7gztjn7"; 
var pt = "72721827b4b4ee493ac09c635827c15ce014c3c3"; 

var encrypt = crypto.createCipher('aes256', key); 
encrypt.update(pt, 'utf8', 'hex'); 
var encrypted = encrypt.final('hex') 

var decrypt = crypto.createDecipher('aes256', key); 
decrypt.update(encrypted, 'hex', 'utf8') 
decrypt.final() 

Arbeitslösung bekommen:https://runkit.com/fredyc/symmetric-encryption-with-nodejs

Antwort

4

Lösung über https://github.com/nodejs/node-v0.x-archive/issues/6386

// https://github.com/nodejs/node-v0.x-archive/issues/6386#issuecomment-31817919 
var assert = require('assert'); 
var crypto = require('crypto'); 

var algorithm = 'aes256'; 
var inputEncoding = 'utf8'; 
var outputEncoding = 'hex'; 

var key = 'ciw7p02f70000ysjon7gztjn7'; 
var text = '72721827b4b4ee493ac09c635827c15ce014c3c3'; 

console.log('Ciphering "%s" with key "%s" using %s', text, key, algorithm); 

var cipher = crypto.createCipher(algorithm, key); 
var ciphered = cipher.update(text, inputEncoding, outputEncoding); 
ciphered += cipher.final(outputEncoding); 

console.log('Result in %s is "%s"', outputEncoding, ciphered); 

var decipher = crypto.createDecipher(algorithm, key); 
var deciphered = decipher.update(ciphered, outputEncoding, inputEncoding); 
deciphered += decipher.final(inputEncoding); 

console.log(deciphered); 
assert.equal(deciphered, text, 'Deciphered text does not match!'); 

die Verwendungsfehler ist hier:

// yours (incorrect) 
var encrypt = crypto.createCipher('aes256', key); 
encrypt.update(pt, 'utf8', 'hex'); 
var encrypted = encrypt.final('hex') 

// correct 
var encrypt = crypto.createCipher('aes256', key); 
var encrypted = encrypt.update(pt, 'utf8', 'hex'); 
encrypted += encrypt.final('hex') 



// yours (incorrect) 
var decrypt = crypto.createDecipher('aes256', key); 
decrypt.update(encrypted, 'hex', 'utf8') 
decrypt.final() 

// correct 
var decrypt = crypto.createDecipher('aes256', key); 
var decrypted = decrypt.update(encrypted, 'hex', 'utf8') 
decrypted += decrypt.final() 
+0

Danke, habe nicht erwartet, Antwort in Nodejs Archiv zu finden: D – FredyC

+0

Ich hasse es zu sagen, aber ... Google, erster Schlag. :) – Tomalak

+0

Ich denke, ich fand es auch, aber ich traute einfach nicht, es könnte relevant sein, da es 3 Jahre alt ist :) – FredyC

Verwandte Themen