0
產生crypto.createCipheriv論據我使用下面的代碼來加密在我的Node.js代碼串。如何從靜態源
我想了解如何從靜態源生成KEY
和HMAC_KEY
。在我的程序中,它現在是隨機生成的。由於它是隨機生成的,因此我無法使用以下算法來加密我的數據庫密碼。
crypto = require('crypto');
ALGORITHM = "AES-256-CBC";
HMAC_ALGORITHM = "SHA256";
KEY = crypto.randomBytes(32);
HMAC_KEY = crypto.randomBytes(32);
function (plain_text) {
var IV = new Buffer(crypto.randomBytes(16)); // ensure that the IV (initialization vector) is random
var cipher_text;
var hmac;
var encryptor;
encryptor = crypto.createCipheriv(ALGORITHM, KEY, IV);
encryptor.setEncoding('hex');
encryptor.write(plain_text);
encryptor.end();
cipher_text = encryptor.read();
hmac = crypto.createHmac(HMAC_ALGORITHM, HMAC_KEY);
hmac.update(cipher_text);
hmac.update(IV.toString('hex')); // ensure that both the IV and the cipher-text is protected by the HMAC
// The IV isn't a secret so it can be stored along side everything else
return cipher_text + "$" + IV.toString('hex') + "$" + hmac.digest('hex')
};
感謝。不幸的是我得到一個錯誤「錯誤:無效的密鑰長度」使用KEY = Buffer.from(「爲abcdefghijklmnopqrstuvwxyzabcdef」,「六角」)之後;正如你所建議的那樣。我哪裏錯了?你能指導我嗎? –
它改變後開始工作KEY = Buffer.from('some key string','hex');到KEY = Buffer.from('一些關鍵字符串','二進制'); –
您應該使用第1步中顯示的相同密鑰字符串 –