[Python]实现对称加密AES/CBC/PKCS5PADDING(支持中文)

2021-04-16 86点热度 0人点赞 0条评论

本文是Python版本的AES/CBC/PKCS5PADDING加解密实现,可以加密成十六进制的文本,或者Base64编码的文本,也支持中文!支持密码和iv(也可以简单点密码key和iv一样)

JavaScript版本 加解密AES/CBC/PKCS5PADDING(加解密结果和本文Python版本的也互通),请看我的博客:https://blog.terrynow.com/2021/04/15/javascript-tool-cbc-pkcs5padding-implement/

AES/CBC/PKCS5PADDING 加解密的Java实现(加解密结果和Javascript版本互通),请看我的博客:https://blog.terrynow.com/2021/04/14/java-tool-aes-cbc-pkcs5padding-implement/

# 如果提示Crypto问题,就先删除:crypto pycrypto重新安装:pycrypto
# pip uninstall pycrypto
# pip uninstall crypto
# pip install pycrypto
from Crypto.Cipher import AES
from Crypto import Random
import base64
import random

block_size = AES.block_size

# unpad = lambda s : s[0:-ord(s[-1])]
unpad = lambda s : s[:-ord(s[len(s)-1:])]

def pad(text):
    """
    明文使用PKCS7填充
    最终调用AES加密方法时,传入的是一个byte数组,要求是16的整数倍,因此需要对明文进行处理
    """
    bs = AES.block_size  # 16
    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 encrypt(key, iv, source):
    plain = pad(source)

    cipher = AES.new(key, AES.MODE_CBC, iv)
    return cipher.encrypt(plain)


# AES加密 加密后的密文转成Base64
def encryptToBase64(key, iv, source):
    return base64.encodebytes(encrypt(key, iv, source)).decode("utf-8")


# AES加密 加密后的密文转成16进制文本
def encryptToHexString(key, iv, source):
    return encrypt(key, iv, source).hex()


# AES加密 加密后的密文转成Base64Url
def encryptToBase64Url(key, iv, source):
    return base64.urlsafe_b64encode(encrypt(key, iv, source)).decode("utf-8")


def decrypt(key, iv, source):
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return unpad(cipher.decrypt(source))

# AES解密,把十六进制密文source解密
def decryptFromHexString(key, iv, source):
    source_bytes = bytes.fromhex(source)
    return decrypt(key, iv, source_bytes).decode('utf-8')


# AES解密,把Base64密文source解密
def decryptFromBase64(key, iv, source):
    source_bytes = base64.b64decode(source)
    return decrypt(key, iv, source_bytes).decode('utf-8')



key='sixteencharacter' #Key密钥需要16个bytes的
iv = '1234567890ABCDEF'
source='你好'

print('Encrypted Text(Base64): ' + encryptToBase64(key, iv, source))
# 输出:aX6TOQZi1OyUonGPzEDeCw==

print('Encrypted Text(Hexstring): ' + encryptToHexString(key, iv, source))
# 输出:697E93390662D4EC94A2718FCC40DE0B

print('Decrypted Text: ' + decryptFromHexString(key, iv, '697E93390662D4EC94A2718FCC40DE0B')) #你好
print('Decrypted Text: ' + decryptFromBase64(key, iv, 'aX6TOQZi1OyUonGPzEDeCw==')) #你好

 

admin

这个人很懒,什么都没留下

文章评论

*

code