pythonDES加密与解密以及hex输出和bs64格式输出
程序员文章站
2022-05-17 09:13:14
```python import pyDes import base64 Key = "1" #加密的key Iv = None #偏移量 def bytesToHexString(bs): ''' bytes转16进制 ''' return ''.join(['%02X ' % b for b i... ......
import pydes import base64 key = "1" #加密的key iv = none #偏移量 def bytestohexstring(bs): ''' bytes转16进制 ''' return ''.join(['%02x ' % b for b in bs]) def hexstringtobytes(str): ''' 16进制转bytes ''' str = str.replace(" ", "") return bytes.fromhex(str) # 加密 def encrypt_str(data): # 加密方法 #padmode填充方式 #pydes.ecb模式 method = pydes.des(key, pydes.ecb, iv, pad=none, padmode=pydes.pad_pkcs5) # 执行加密码 hex输出 k = method.encrypt(data) data = bytestohexstring(k).replace(' ','') #bs64手粗 #data =base64.b64encode(k) return data # 解密 def decrypt_str(data): method = pydes.des(key, pydes.ecb, iv, pad=none, padmode=pydes.pad_pkcs5) k =hexstringtobytes(data) #bs64 #k = base64.b64decode(data) return method.decrypt(k) encrypt = encrypt_str("aaa") print(encrypt) decrypt = decrypt_str(encrypt) print(decrypt)