如何在NodeJS中创buildencryption和解密函数?

我需要在我的NodeJS应用程序中创buildencryption和解密function。 任何人都可以帮助,并指出我在正确的方向吗?

经过一番挖掘,我能够回答我自己的问题…对不起,缺乏清晰度和细节。 将在下一个问题上工作。

我已经包含了encryption和解密的函数,以及“帮助”函数来生成密钥并生成初始化向量。

var crypto = require('crypto'); var encrypt = function encrypt(input, password) { var key = generateKey(password); var initializationVector = generateInitializationVector(password); var data = new Buffer(input.toString(), 'utf8').toString('binary'); var cipher = crypto.createCipheriv('aes-256-cbc', key, initializationVector.slice(0,16)); var encrypted = cipher.update(data, 'utf8', 'hex'); encrypted += cipher.final('hex'); var encoded = new Buffer(encrypted, 'binary').toString('base64'); return encoded; }; var decrypt = function decrypt(input, password) { var key = generateKey(password); var initializationVector = generateInitializationVector(password); var input = input.replace(/\-/g, '+').replace(/_/g, '/'); var edata = new Buffer(input, 'base64').toString('binary'); var decipher = crypto.createDecipheriv('aes-256-cbc', key, initializationVector.slice(0,16)); var decrypted = decipher.update(edata, 'hex', 'utf8'); decrypted += decipher.final('utf8'); var decoded = new Buffer(decrypted, 'binary').toString('utf8'); return decoded; }; var generateKey = function generateKey(password) { var cryptographicHash = crypto.createHash('md5'); cryptographicHash.update(password); key = cryptographicHash.digest('hex'); return key; } var generateInitializationVector = function generateInitializationVector(password) { var cryptographicHash = crypto.createHash('md5'); cryptographicHash.update(password + key); initializationVector = cryptographicHash.digest('hex'); return initializationVector; } var password = 'MyPassword'; var originalStr = 'hello world!'; var encryptedStr = encrypt(originalStr, password); var decryptedStr = decrypt(encryptedStr, password); 

提供解决scheme的启发。
他的post可以在这里: 这里

我原本是通过dave得到了这个post的工作,但是对于字符长度大于15的input值不起作用。上面更新的代码适用于任何长度的input。