Created
July 10, 2012 08:11
-
-
Save chensoren/3081970 to your computer and use it in GitHub Desktop.
nodejs AES encrypt and decrypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'); | |
var AESCrypt = {}; | |
AESCrypt.decrypt = function(cryptkey, iv, encryptdata) { | |
encryptdata = new Buffer(encryptdata, 'base64').toString('binary'); | |
var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv), | |
decoded = decipher.update(encryptdata); | |
decoded += decipher.final(); | |
return decoded; | |
} | |
AESCrypt.encrypt = function(cryptkey, iv, cleardata) { | |
var encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv), | |
encryptdata = encipher.update(cleardata); | |
encryptdata += encipher.final(); | |
encode_encryptdata = new Buffer(encryptdata, 'binary').toString('base64'); | |
return encode_encryptdata; | |
} | |
var cryptkey = crypto.createHash('sha256').update('Nixnogen').digest(), | |
iv = 'a2xhcgAAAAAAAAAA', | |
buf = "Here is some data for the encrypt", // 32 chars | |
enc = AESCrypt.encrypt(cryptkey, iv, buf); | |
var dec = AESCrypt.decrypt(cryptkey, iv, enc); | |
console.warn("encrypt length: ", enc.length); | |
console.warn("encrypt in Base64:", enc); | |
console.warn("decrypt all: " + dec); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does it work for streams?
const encryptInputFile = async (filePath, cb) => {
try {
// Use the async
crypto.scrypt()
instead.const key = crypto.scryptSync(PASSWORD, 'salt', 24);
// Use
crypto.randomBytes()
to generate a random iv instead of the static iv// shown here.
const iv = Buffer.alloc(16, 0);
// Initialization vector.
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
// create input and output stream
const input = fs.createReadStream(filePath);
const output = fs.createWriteStream(TARGET_PATH);
}
const decryptFile = async (encryptedFile, targetPath, cb) => {
try {
// Use the async
crypto.scrypt()
instead.const key = crypto.scryptSync(PASSWORD, 'salt', 24);
// The IV is usually passed along with the ciphertext.
const iv = Buffer.alloc(16, 0); // Initialization vector.
}
error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
at Decipheriv._flush (internal/crypto/cipher.js:143:29)
at Decipheriv.prefinish (_stream_transform.js:141:10)
at Decipheriv.emit (events.js:188:13)
at prefinish (_stream_writable.js:635:14)
at finishMaybe (_stream_writable.js:643:5)
at endWritable (_stream_writable.js:663:3)
at Decipheriv.Writable.end (_stream_writable.js:594:5)
at ReadStream.onend (_stream_readable.js:655:10)
at Object.onceWrapper (events.js:276:13)
at ReadStream.emit (events.js:193:15)
Emitted 'error' event at:
at errorOrDestroy (internal/streams/destroy.js:98:12)
at Decipheriv.onerror (_stream_readable.js:717:7)
at Decipheriv.emit (events.js:188:13)
at done (_stream_transform.js:208:19)
at _flush (_stream_transform.js:142:7)
at Decipheriv._flush (internal/crypto/cipher.js:145:5)
at Decipheriv.prefinish (_stream_transform.js:141:10)
[... lines matching original stack trace ...]
at finishMaybe (_stream_writable.js:643:5)