il seguente codice per cifrare e decifrare le richieste al nostro servizio gateway di pagamento funziona correttamente con Nodo Js 5.7.0valido Lunghezza chiave dopo l'aggiornamento a NodeJS 6
function Encrypt(plainText, workingKey) {
var m = crypto.createHash('md5');
m.update(workingKey);
var key = m.digest('binary');
var iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f';
var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
var encoded = cipher.update(plainText, 'utf8', 'hex');
encoded += cipher.final('hex');
return encoded;
};
function Decrypt(encText, workingKey) {
var m = crypto.createHash('md5');
m.update(workingKey)
var key = m.digest('binary');
var iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f';
var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
var decoded = decipher.update(encText, 'hex', 'utf8');
decoded += decipher.final('utf8');
return decoded;
};
Tuttavia dopo l'aggiornamento a NodeJS 6.0 (anche provato 6.1) otteniamo il seguente errore.
Debug: internal, implementation, error
Error: Uncaught error: Invalid key length
at Error (native)
at new Cipheriv (crypto.js:184:16)
at Object.Cipheriv (crypto.js:182:12)
nostra lunghezza della chiave è sempre stata di 16 caratteri (cioè 128 bit) e stava lavorando prima dell'aggiornamento. Perché questo problema si verificherà ora?
6.x è lungi dall'essere stabile, è necessario usarlo? Stiamo usando 5.5.0 nella nostra compagnia, ho provato anche 6.x, ma in un'ora sono stato costretto a tornare indietro. – libik
Perché usi le stringhe "binarie"? Usa solo un Buffer e fallo con esso; rimuovi il ''binario'' da' m.digest (' binary ') '. –
@ Artjom-b. Grazie mille. Quello che hai suggerito ha funzionato. Saresti così gentile da spiegare perché ha funzionato? – Anomaly211