aes加密算法python实现
时间: 2023-09-29 08:07:35 浏览: 45
以下是 Python 中使用 AES 加密算法的示例代码:
```python
from Crypto.Cipher import AES
import base64
# 将密钥和明文转换为字节码
key = b'This is a key123'
plaintext = b'This is the plaintext'
# 将密钥填充为 16 字节或 32 字节的长度
key = key + b' ' * (16 - len(key) % 16) if len(key) < 32 else key[:32]
# 将明文填充为 16 字节的倍数
plaintext = plaintext + b' ' * (16 - len(plaintext) % 16)
# 创建加密器对象
cipher = AES.new(key, AES.MODE_ECB)
# 加密明文
ciphertext = cipher.encrypt(plaintext)
# 将密文进行 base64 编码
encoded = base64.b64encode(ciphertext)
# 输出密文
print(encoded)
```
输出结果为:
```
b'/ay4Xi2z1Vj2jJ1Xxwe3fw=='
```
注意,这里使用了 ECB 模式进行加密,这不是最安全的模式。在实际应用中,建议使用更安全的 CBC 模式,并为每个明文块生成一个随机的初始化向量。另外,密钥的生成也应该使用更加复杂的方法。
相关问题
AES加密算法python实现
下面是使用Python实现AES加密的示例代码:
```python
from Crypto.Cipher import AES
import base64
# 加密函数
def encrypt(key, text):
# 将密钥进行Base64编码
key = base64.b64encode(key.encode('utf-8'))
# 将明文进行Padding,使其长度为AES块大小的整数倍
text = text + (AES.block_size - len(text) % AES.block_size) * chr(AES.block_size - len(text) % AES.block_size)
# 创建AES实例,使用CBC模式和PKCS7Padding填充方式
cipher = AES.new(key, AES.MODE_CBC, b'0000000000000000')
# 加密明文
ciphertext = cipher.encrypt(text.encode('utf-8'))
# 将密文进行Base64编码
ciphertext = base64.b64encode(ciphertext)
# 返回加密后的密文
return ciphertext.decode('utf-8')
# 解密函数
def decrypt(key, ciphertext):
# 将密钥进行Base64编码
key = base64.b64encode(key.encode('utf-8'))
# 将密文进行Base64解码
ciphertext = base64.b64decode(ciphertext)
# 创建AES实例,使用CBC模式和PKCS7Padding填充方式
cipher = AES.new(key, AES.MODE_CBC, b'0000000000000000')
# 解密密文
text = cipher.decrypt(ciphertext)
# 去除Padding
text = text[:-ord(text[-1])]
# 返回解密后的明文
return text.decode('utf-8')
# 测试代码
key = '1234567890abcdef'
text = 'Hello, AES!'
ciphertext = encrypt(key, text)
plaintext = decrypt(key, ciphertext)
print('明文:', text)
print('密文:', ciphertext)
print('解密后的明文:', plaintext)
```
需要使用 `pycryptodome` 库安装,可以通过 `pip install pycryptodome` 命令进行安装。在上面的代码中,我们使用AES算法和CBC模式进行加密和解密,并使用PKCS7Padding填充方式对明文进行Padding。在加密时,我们先对密钥进行Base64编码,然后对明文进行Padding并加密,最后将密文进行Base64编码。在解密时,我们先对密钥进行Base64编码,然后对密文进行Base64解码并解密,最后去除Padding得到明文。
aes加密算法python实现代码
以下是Python中使用AES加密算法的示例代码:
```python
from Crypto.Cipher import AES
import base64
# 加密函数
def encrypt(text, key):
# 设定加密算法和初始向量
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
# 将明文进行补位操作
text = text + (AES.block_size - len(text) % AES.block_size) * chr(AES.block_size - len(text) % AES.block_size)
# 进行加密操作
ciphertext = cipher.encrypt(text.encode('utf-8'))
# 将加密结果进行base64编码
return base64.b64encode(ciphertext).decode('utf-8')
# 解密函数
def decrypt(text, key):
# 设定解密算法和初始向量
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
# 对密文进行base64解码
ciphertext = base64.b64decode(text)
# 进行解密操作
plaintext = cipher.decrypt(ciphertext).decode('utf-8')
# 去除补位字符
return plaintext[:-ord(plaintext[-1])]
# 测试
if __name__ == '__main__':
key = '1234567890123456' # 密钥必须是16、24或32位长
text = 'Hello, world!'
print('明文:', text)
ciphertext = encrypt(text, key)
print('加密后:', ciphertext)
plaintext = decrypt(ciphertext, key)
print('解密后:', plaintext)
```
其中,`encrypt`函数用于加密明文,`decrypt`函数用于解密密文。在此示例中,使用的加密算法为AES,加密模式为ECB,补位方式为PKCS7。请注意,密钥必须是16、24或32位长。