python + pycryptodome 实现AES加密
from Crypto.Cipher import AES
from binascii import b2a_hex
from binascii import a2b_hex
import pandas as pd
import numpy as np
def aes_encrypt(x, key='abcdefghABCDEFGH'):
key_encode = key.encode('utf-8')
aes = AES.new(key_encode, AES.MODE_ECB)
x_enc = aes.encrypt(x.encode('utf-8'))
x_hex = b2a_hex(x_enc).decode('utf-8')
return x_hex
def aes_decrypt(x, key='abcdefghABCDEFGH'):
key_encode = key.encode('utf-8')
aes = AES.new(key_encode, AES.MODE_ECB)
x = a2b_hex(x)
x_dec = aes.decrypt(x).decode('utf-8')
return x_dec
## 虚拟数据
df = pd.DataFrame(columns=['group', 'number1', 'number2', 'number3'])
df['group'] = ['00', '01', '02', '03', '04', '99']
df['number1'] = [1111,2222,3333,4444,5555,6666]
df['number2'] = [1234,5678,9012,3456,7890,1234]
df['number3'] = [1122,3344,5566,7788,9900,1199]
df['newtoken'] = df.apply(lambda x: x['group'] + str(x['number1']) + '_' + str(x['number2'])+ '_' + str(x['number3']), axis=1)
# 数据加密
df['encrypted_newtoken'] = df.newtoken.apply(aes_encrypt)
# 数据解密
df['decrypted_encrypted_newtoken'] = df.encrypted_newtoken.apply(aes_decrypt)
df
方式1:
#秘钥,此处需要将字符串转为字节
key = 'abcdefgh'
key_encode = key.encode('utf8')
key_encode
print('密钥key: {}'.format(key))
#加密内容需要长达16位字符,所以进行空格拼接
def pad(text):
while len(text) % 16 != 0:
text += b'_'
return text
#加密秘钥需要长达16位字符,所以进行空格拼接
def pad_key(key):
while len(key) % 16 != 0:
key += b'_'
return key
#进行加密算法,模式ECB模式,把叠加完16位的秘钥传进来
aes = AES.new(pad_key(key_encode), AES.MODE_ECB)
#加密内容,此处需要将字符串转为字节
text = '09_1234_5678'
text_encode = text.encode('utf-8')
print('待加密的文字: {}'.format(text))
#进行内容拼接16位字符后传入加密类中,结果为字节类型
encrypted_text = aes.encrypt(pad(text_encode))
print('加密结果(字节型): {}'.format(encrypted_text))
print('加密结果(字符串): {}'.format(b2a_hex(encrypted_text)))
#用aes对象进行解密,将字节类型转为str类型,错误编码忽略不计
de = str(aes.decrypt(encrypted_text), encoding='utf-8',errors="ignore")
#获取str从0开始到文本内容的字符串长度。
print('解密后的文字: {}'.format(de[:len(text)]))
密钥key: abcdefgh
待加密的文字: 09_1234_5678
加密结果(字节型): b'!\xfat\xa3\xef\xecgn\xf5\x1c=\x9ax]B\x1d'
加密结果(字符串): b'21fa74a3efec676ef51c3d9a785d421d'
解密后的文字: 09_1234_5678
from Crypto.Cipher import AES
from binascii import b2a_hex
from Crypto import Random
# 要加密的明文
data = '09_1234_5678'
print('待加密的文字: {}'.format(data))
# 密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.
# 目前AES-128足够用
key = b'aaaaaaaaaaaaaaaa'
# 生成长度等于AES块大小的不可重复的密钥向量
iv = Random.new().read(AES.block_size)
# 使用key和iv初始化AES对象, 使用MODE_CFB模式
mycipher = AES.new(key, AES.MODE_CFB, iv)
# 加密的明文长度必须为16的倍数,如果长度不为16的倍数,则需要补足为16的倍数
# 将iv(密钥向量)加到加密的密文开头,一起传输
ciphertext = iv + mycipher.encrypt(data.encode())
# 解密的话要用key和iv生成新的AES对象
mydecrypt = AES.new(key, AES.MODE_CFB, ciphertext[:16])
# 使用新生成的AES对象,将加密的密文解密
decrypttext = mydecrypt.decrypt(ciphertext[16:])
print('密钥key: {}'.format(key))
print('iv为:', b2a_hex(ciphertext)[:16])
print('加密后数据为:', b2a_hex(ciphertext)[16:])
print('解密后数据为:', decrypttext.decode())
待加密的文字: 09_1234_5678
密钥key: b'aaaaaaaaaaaaaaaa'
iv为: b'a993d7fcb6523eb8'
加密后数据为: b'2f25048409404fbbe85eaa7d36d7ed17e73ab386'
解密后数据为: 09_1234_5678