使用节点加密。使用python2.7 PyCrypto来解密aes256和解密。

时间:2022-06-28 15:23:14

I am trying to encrypt using node.js as follows (node.js v0.10.33):

我正在尝试使用node加密。js如下(节点。js v0.10.33):

var crypto = require('crypto');
var assert = require('assert');

var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = 'mykey';
var text = 'this-needs-to-be-encrypted';

var cipher = crypto.createCipher(algorithm, key);  
var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
console.log('encrypted', encrypted, encrypted.length)
/*
var decipher = crypto.createDecipher(algorithm, key);
try {
    var decrypted = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');
} catch (e) {
    console.error('Couldnt decipher encrypted text. Invalid key provided', e)
} finally {
    assert.equal(decrypted, text);
}
*/

How can I decrypt the encrypted text using PyCrypto (v2.6.1) on py2.7?

在py2.7上如何使用PyCrypto (v2.6.1)解密加密文本?

1 个解决方案

#1


4  

You should be using crypto.createCipheriv as stated in https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password.

您应该使用加密技术。createCipheriv如https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password所述。

The answer below assumes you change your snippet to use crypto.createCipheriv, as following:

下面的答案假设您更改了代码段以使用crypto。createCipheriv,如下:

var crypto = require('crypto');
var assert = require('assert');

var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = '00000000000000000000000000000000';
var iv = '0000000000000000';
var text = 'this-needs-to-be-encrypted';

var cipher = crypto.createCipheriv(algorithm, key, iv);  
var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
console.log('encrypted', encrypted, encrypted.length)

which generates the encrypted text b88e5f69c7bd5cd67c9c12b9ad73e8c1ca948ab26da01e6dad0e7f95448e79f4.

生成加密文本b88e5f69c7bd5cd67c9c12b9ad73e8c1ca948ab26da01e6dad0e7f95448e79f4。

Python Solution with explicit key and IV:

带有显式键和IV的Python解决方案:

from Crypto import Random
from Crypto.Cipher import AES

BS = 16
def pad(data):
    padding = BS - len(data) % BS
    return data + padding * chr(padding)

def unpad(data):
    return data[0:-ord(data[-1])]

def decrypt_node(hex_data, key='0'*32, iv='0'*16):
    data = ''.join(map(chr, bytearray.fromhex(hex_data)))
    aes = AES.new(key, AES.MODE_CBC, iv)
    return unpad(aes.decrypt(data))

def encrypt_node(data, key='0'*32, iv='0'*16):
    aes = AES.new(key, AES.MODE_CBC, iv)
    return aes.encrypt(pad(data)).encode('hex')

print(encrypt_node('this-needs-to-be-encrypted'))
print(decrypt_node('b88e5f69c7bd5cd67c9c12b9ad73e8c1ca948ab26da01e6dad0e7f95448e79f4'))

If you keep using plain crypto.createCipher you will need to derive the key and iv from the password using https://www.openssl.org/docs/man1.0.2/crypto/EVP_BytesToKey.html.

如果你继续使用普通密码。您将需要使用https://www.openssl.org/docs/man1.0.2/ crypto/evp_by睪丸.html从密码中获得密钥和iv。

#1


4  

You should be using crypto.createCipheriv as stated in https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password.

您应该使用加密技术。createCipheriv如https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password所述。

The answer below assumes you change your snippet to use crypto.createCipheriv, as following:

下面的答案假设您更改了代码段以使用crypto。createCipheriv,如下:

var crypto = require('crypto');
var assert = require('assert');

var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = '00000000000000000000000000000000';
var iv = '0000000000000000';
var text = 'this-needs-to-be-encrypted';

var cipher = crypto.createCipheriv(algorithm, key, iv);  
var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
console.log('encrypted', encrypted, encrypted.length)

which generates the encrypted text b88e5f69c7bd5cd67c9c12b9ad73e8c1ca948ab26da01e6dad0e7f95448e79f4.

生成加密文本b88e5f69c7bd5cd67c9c12b9ad73e8c1ca948ab26da01e6dad0e7f95448e79f4。

Python Solution with explicit key and IV:

带有显式键和IV的Python解决方案:

from Crypto import Random
from Crypto.Cipher import AES

BS = 16
def pad(data):
    padding = BS - len(data) % BS
    return data + padding * chr(padding)

def unpad(data):
    return data[0:-ord(data[-1])]

def decrypt_node(hex_data, key='0'*32, iv='0'*16):
    data = ''.join(map(chr, bytearray.fromhex(hex_data)))
    aes = AES.new(key, AES.MODE_CBC, iv)
    return unpad(aes.decrypt(data))

def encrypt_node(data, key='0'*32, iv='0'*16):
    aes = AES.new(key, AES.MODE_CBC, iv)
    return aes.encrypt(pad(data)).encode('hex')

print(encrypt_node('this-needs-to-be-encrypted'))
print(decrypt_node('b88e5f69c7bd5cd67c9c12b9ad73e8c1ca948ab26da01e6dad0e7f95448e79f4'))

If you keep using plain crypto.createCipher you will need to derive the key and iv from the password using https://www.openssl.org/docs/man1.0.2/crypto/EVP_BytesToKey.html.

如果你继续使用普通密码。您将需要使用https://www.openssl.org/docs/man1.0.2/ crypto/evp_by睪丸.html从密码中获得密钥和iv。