AES加密

2019-01-14  本文已影响0人  aq_wzj
from Crypto.Cipher import AES

# ==============加密=============
def encrypt(massage):
    key = b'dfdsdfsasdfdsdfs'
    cipher = AES.new(key, AES.MODE_CBC, key)
    ba_data = bytearray(massage, encoding='utf-8')
    v1 = len(ba_data)
    v2 = v1 % 16
    if v2 == 0:
        v3 = 16
    else:
        v3 = 16 - v2
    for i in range(v3):
        ba_data.append(v3)
    msg = cipher.encrypt(ba_data)
    return msg

# ==============解密==============
def decrypt(msg):
    from Crypto.Cipher import AES
    key = b'dfdsdfsasdfdsdfs'
    cipher = AES.new(key, AES.MODE_CBC, key)
    result = cipher.decrypt(msg)
    data = result[0:-result[-1]]
    return str(data,encoding='utf-8')

msg = encrypt('这里是要加密的字符串')

res = decrypt(msg)
print(res)
上一篇下一篇

猜你喜欢

热点阅读