Python 对称加密
转载:https://www.cnblogs.com/pywen/p/15831353.html
from pyDes import des, CBC, PAD_PKCS5
import binascii
def KEY_8(KEY):
if len(KEY) > 8:
KEY = KEY[0:8]
else:
while len(KEY) < 8:
KEY = KEY + ' '
return KEY
def des_encrypt(s, KEY):
secret_key = KEY_8(KEY)
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
en = k.encrypt(s, padmode=PAD_PKCS5)
return binascii.b2a_hex(en).decode()
def des_descrypt(s, KEY):
secret_key = KEY_8(KEY)
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
return de.decode()
if __name__ == '__main__':
# strOrigin 秘钥
KEY = 'mykey'
strOrigin = "abcdefg-888-dddabcdefg-888-dddabcdefg-888-ddd"
# 加密
res = des_encrypt(strOrigin, KEY)
print(res)
# 解密
res2 = des_descrypt(res, KEY)
print(res2)
本文来自博客园,作者:xdd1997
转载请注明:https://www.cnblogs.com/xdd1997/p/encrypt.html