我不能crypto
去上班。
- 视窗 7 64 位
- node.js v0.10.18
加密似乎有效:
var fs = require('fs');
var img = new Buffer (fs.readFileSync('./image.png'), 'binary');
var crypto = require('crypto')
, key = 'salt_from_the_user_document'
, plaintext = img
, cipher = crypto.createCipher('aes-256-cbc', key)
, decipher = crypto.createDecipher('aes-256-cbc', key);
cipher.update(plaintext, 'binary', 'base64');
var encryptBinary = cipher.final('base64')
console.log('encrypted :', encryptBinary);
...但解密不是。
decipher.update(encryptBinary, 'base64', 'binary');
var decryptBinary = decipher.final('binary');
console.log('decrypted :', decryptBinary);
与node-efs相同的解密问题。
var efs = require('efs').init('aes-128-cbc', 'password');
// encrypt and write file
efs.writeFileSync('/tmp/example', 'hello world');
// decrypt and read file
efs.readFileSync('/tmp/example');
与file-encryptor相同的问题。
这有效(仅适用于字符串):
var crypto = require('crypto')
, key = 'salt_from_the_user_document'
, plaintext = 'password'
, cipher = crypto.createCipher('aes-256-cbc', key)
, decipher = crypto.createDecipher('aes-256-cbc', key);
cipher.update(plaintext, 'utf8', 'base64');
var encryptedPassword = cipher.final('base64')
decipher.update(encryptedPassword, 'base64', 'utf8');
var decryptedPassword = decipher.final('utf8');
console.log('encrypted :', encryptedPassword);
console.log('decrypted :', decryptedPassword);