-
-
Save chensoren/3081970 to your computer and use it in GitHub Desktop.
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); |
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);
input.pipe(cipher).pipe(output);
cb(null)
} catch (error) {
console.log('ERROR: Encryption failed', error);
cb(error);
}
}
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.
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
console.log(`Source file: ${encryptedFile}, target file: ${targetPath}`);
const input = fs.createReadStream(encryptedFile);
const output = fs.createWriteStream(targetPath);
input.pipe(decipher).pipe(output);
cb(null);
} catch (error) {
console.log('ERROR: Failed in decryption', error);
cb(error);
}
}
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)
I had the same issue, and worked it out by setting auto padding = true. I thought id add to the thread incase it helps someone else. This is some code for a project I'm working on.