【发布时间】:2015-10-14 22:29:31
【问题描述】:
我查看了AES - Encryption with Crypto (node-js) / decryption with Pycrypto (python) 的帖子,因为我试图完全相反,但我似乎无法做到正确。这是我迄今为止尝试过的......
Python 加密
import base64
from Crypto import Random
from Crypto.Cipher import AES
text_file = open("cryptic.txt", "w")
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[:-ord(s[len(s)-1:])]
plaintxt = 'dolladollabillzz'
iv = Random.new().read( AES.block_size )
print AES.block_size
key = 'totallyasecret!!'
cipher = AES.new(key=key, mode=AES.MODE_CBC, IV=iv)
encrypted = base64.b64encode(iv + cipher.encrypt(plaintxt))
text_file.write(encrypted)
text_file.close()
Node.js 解密
var fs = require('fs');
var crypto = require('crypto');
var Buffer = require('buffer').Buffer;
var algorithm = 'aes-128-cbc';
var key = new Buffer('totallyasecret!!', 'binary');
var cryptic = fs.readFileSync('./cryptic.txt', 'base64');
var iv = cryptic.slice(0, 16);
var ciphertext = cryptic.slice(16);
var decipher = crypto.createDecipheriv(algorithm, key, iv);
var decrypted = [decipher.update(ciphertext)];
decrypted.push(decipher.final('utf8'));
var finished = Buffer.concat(decrypted).toString('utf8');
console.log(finished);
每次我尝试运行 Node.js 解密时,都会收到错误消息:
Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
【问题讨论】:
标签: javascript python node.js cryptography aes