python AES加密,使用pycrypto

it2022-12-26  76

首先安装 pip install pycryptodome 然后卸载 pip uninstall pycryptodome 然后再安装 pip install pycryptodome 我也不知道为啥,反正这么做才能安装成功 至于pycrypto已经不更新了,所以用上面这个

# AES-demo import base64 from Crypto.Cipher import AES from urllib import parse ''' 采用AES对称加密算法 ''' # str不是16的倍数那就补足为16的倍数 def add_to_16(value): while len(value) % 16 != 0: value += '\0' return str.encode(value) # 返回bytes # 加密方法 def encrypt_oracle(): # 秘钥 key = '123456' # 待加密文本 text = 'abc123def456' # 偏移值 Iv = b'1234567890111111' # 初始化加密器 aes = AES.new(add_to_16(key), AES.MODE_CBC, Iv) # 先进行aes加密 encrypt_aes = aes.encrypt(add_to_16(text)) # 用base64转成字符串形式 encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8').replace('\n','') # 执行加密并转码返回bytes # 然后进行url加密 encrypt_aes_url = parse.quote(encrypted_text) print(encrypt_aes_url) # 解密方法 def decrypt_oralce(): # 密文=encrypt_aes_url的值 value = '7r6AthMmhsM/3exk2YgLkg%3D%3D' text = parse.unquote(value) print(text) # 秘钥 # key = '1q2w3e4r5t6y7u8i' key='123456' # 偏移值 Iv = b'1234567890111111' # 初始化加密器 aes = AES.new(add_to_16(key), AES.MODE_CBC,iv=Iv) # 优先逆向解密base64成bytes base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8')) # 执行解密密并转码返回str decrypted_text = str(aes.decrypt(base64_decrypted),encoding='utf-8') print(decrypted_text) if __name__ == '__main__': encrypt_oracle() decrypt_oralce()

文章主体参考https://blog.csdn.net/s740556472/article/details/79026128这个链接 其余自由发挥啦

最新回复(0)