【问题标题】:Convert openssl AES in Php to Python AES将 PHP 中的 openssl AES 转换为 Python AES
【发布时间】:2018-03-02 22:48:20
【问题描述】:

我有一个php文件,如下:

$encryption_encoded_key = 'c7e1wJFz+PBwQix80D1MbIwwOmOceZOzFGoidzDkF5g=';

function my_encrypt($data, $key) {
    $encryption_key = base64_decode($key);
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cfb'));

    $encrypted = openssl_encrypt($data, 'aes-256-cfb', $encryption_key, 1, $iv);

    // The $iv is just as important as the key for decrypting, so save it with encrypted data using a unique separator (::)
    return base64_encode($encrypted . '::' . $iv);
}

function my_decrypt($data, $key) {
    // Remove the base64 encoding from key
    $encryption_key = base64_decode($key);

    // To decrypt, split the encrypted data from IV - unique separator used was "::"
    list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);

    return openssl_decrypt($encrypted_data, 'aes-256-cfb', $encryption_key, 1, $iv);
}

$data = 'USER_ID||NAME||EMAIL||MOBILE';
$data_encrypted = my_encrypt($data, $encryption_encoded_key);
echo $data_encrypted;
$data_decrypted = my_decrypt($data_encrypted, $encryption_encoded_key);
echo "Decrypted string: ". $data_decrypted;

这很好用,并且能够使用加密密钥进行加密/解密,现在我也有一个 python 文件:

import hashlib
import base64
from Crypto.Cipher import AES
from Crypto import Random

encryption_encoded_key = 'c7e1wJFz+PBwQix80D1MbIwwOmOceZOzFGoidzDkF5g='

def my_encrypt(data, key):
    #Remove the base64 encoding from key
    encryption_key = base64.b64decode(key)
    #Generate an initialization vector
    bs = AES.block_size
    iv = Random.new().read(bs)

    cipher = AES.new(encryption_key, AES.MODE_CFB, iv)
    #Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
    encrypted = cipher.encrypt(data)

    #The iv is just as important as the key for decrypting, so save it with encrypted data using a unique separator (::)
    return base64.b64encode(encrypted + '::' + iv)


def my_decrypt(data, key):
    #Remove the base64 encoding from key
    encryption_key = base64.b64decode(key)

    #To decrypt, split the encrypted data from IV - unique separator used was "::"
    encrypted_data, iv = base64.b64decode(data).split('::')

    cipher = AES.new(encryption_key, AES.MODE_CFB, iv)

    return cipher.decrypt(encrypted_data)

data = 'USER_ID||NAME||EMAIL||MOBILE'

print "Actual string: %s" %(data)
data_encrypted = my_encrypt(data, encryption_encoded_key)
print data_encrypted

data_decrypted = my_decrypt(data_encrypted, encryption_encoded_key)
print "Decrypted string: %s" %(data_decrypted)

当我尝试从 python 中使用它时,它也可以正常工作,它能够加密/解密输入字符串, 我想使用php文件加密并在python中解密输出,两者都应该使用CFB模式的AES 256加密,我做错了什么?

【问题讨论】:

  • 流程哪里出了问题?在为每种方法运行加密/解密操作之前,您可以打印出您解析的密钥、iv、数据等吗?这至少会给你一个起点
  • 哦,还有一件事要注意 - 对 python 或 php 了解不多,请注意参考类型。 iv 在运行加密/解密操作时可能会通过每个块进行修改。如果您要(作为一个简单的例子)从...0001 的iv 开始,在第一个块之后,该iv 可能表示为...0002。确保在两个实现中都附加了“原始”iv,而不是(可能)修改后的 iv。

标签: php python encryption cryptography aes


【解决方案1】:

要使用CFB mode,您需要为其指定段大小。 OpenSSL 有aes-256-cfb(128 位)、aes-256-cfb1(即 1 位)和aes-256-cfb8(8 位)(以及 AES-128 和 192 的类似模式)。所以你在你的 php 代码中使用 128 位 cfb。

Python 库接受 AES.newsegment_size 参数,但默认值为 8,因此您在两个版本中使用不同的模式。

要获取 Python 代码来解密 PHP 代码的输出,请将 128 的段大小添加到 cipher 对象:

cipher = AES.new(encryption_key, AES.MODE_CFB, iv, segment_size=128)

(注意,这是使用 PyCrypto 的较新的 PyCryptodome fork。PyCrypto 在这里有一个错误,无法工作。)

或者,您可以通过设置密码使 PHP 代码使用 CFB-8(显然不要同时更改):

$encrypted = openssl_encrypt($data, 'aes-256-cfb8', $encryption_key, 1, $iv);

【讨论】:

  • 现在完美运行,有没有更好的方法来解决这个问题?就像python中是否有任何openssl包装器一样,我可以做同样的事情而不是试图猜测加密类型,假设php代码将密码从aes更改为cast5-cbc,这样我就不会重写我的加密/解密方法?跨度>
  • 不错的答案,我感觉这个问题与segment_size有关,但不知道如何改变......
猜你喜欢
  • 2017-02-03
  • 2020-04-22
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多