记一次Python与C#的AES加密对接

前言

这几天做自动化测试的同事找到我,说是帮她看看有个AES加密的问题要怎么处理。

大概就是文档中贴了一段C#的AES加密代码,然后她要翻译成python的版本,去做一些测试相关的工作。

在我印象中,AES加密在不同语言中对接或多或少都有一点点不一样,并不会像同一种一言那么流畅,总是要踩踩坑才能解决。

文档中C#版本的实现

public static string AesEncrypt(string toEncrypt, string key)
{
    byte[] keyArray = SHA256(key);
    byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };

    RijndaelManaged rDel = new RijndaelManaged
    {
        Key = keyArray,
        IV = iv,
        Mode = CipherMode.CBC,
        Padding = PaddingMode.PKCS7,
        BlockSize = 128
    };

    ICryptoTransform cTransform = rDel.CreateEncryptor();

    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

public static byte[] SHA256(string str)
{
    byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
    SHA256Managed Sha256 = new SHA256Managed();
    byte[] by = Sha256.ComputeHash(SHA256Data);
    return by;
}

实现上都很常见,网上一搜也是一堆,重点在于 CBC 和 PKCS7。

对应的Python版本

from Crypto.Cipher import AES
import base64
import hashlib

def jm_sha256(data):
    sha256 = hashlib.sha256()
    sha256.update(data.encode("utf-8"))    
    res = sha256.digest()
    # print("sha256加密结果:", res)
    return res

def pkcs7padding(text):

    bs = AES.block_size 
    length = len(text)
    bytes_length = len(bytes(text, encoding='utf-8'))

    # tips:utf-8编码时,英文占1个byte,而中文占3个byte
    padding_size = length if(bytes_length == length) else bytes_length
    padding = bs - padding_size % bs

    # tips:chr(padding)看与其它语言的约定,有的会使用'\0'
    padding_text = chr(padding) * padding
    return text + padding_text

def aes_encrypt_v2(content, key):

    key_bytes = jm_sha256(key)
    iv = "\0".encode("utf-8") * 16

    aes = AES.new(key_bytes, AES.MODE_CBC, iv)

    content_padding = pkcs7padding(content)

    encrypt_bytes = aes.encrypt(bytes(content_padding, encoding='utf-8'))

    result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
    return result


mystr1 = "123"
mykey1 = "12345678"

# 3gVLeGnili1JBTYLHAk8pQ==
print(aes_encrypt_v2(mystr1, mykey1))

mystr2 = "你好abcd1234"
mykey2 = "1234567812345678"

# Qkz+MXCIESJZVgHJffouTQ==
print(aes_encrypt_v2(mystr2, mykey2))

参考文章

posted @ 2019-07-20 17:20  Catcher8  阅读(1227)  评论(0编辑  收藏  举报