1
我看了AES - Encryption with Crypto (node-js)/decryption with Pycrypto (python)後,因爲我試圖做的完全相反,但我似乎無法得到它的權利。這是我到目前爲止已經試過......AES-pycrypto加密在node.js加密解密
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
感謝您的建議@Artjom,但我仍然收到相同的'錯誤的最終塊長度'的錯誤。我也嘗試在readFileSync中使用'binary',但結果也一樣。 – MCaw
你能提供一個密文測試嗎? –
當然,我運行我的Python腳本完全按照它在我的文章中的方式,併產生了密文 Ew2rBvVohbJdFp9D3rBmIAGQWekoL3W3JuF6sQc4DRQ = – MCaw