Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加签验签
2020-06-25 08:07:29 来源:易采站长站 作者:易采站长站整理
self.key = key.encode('utf8')
self.iv = iv if iv else bytes(key[0:16], 'utf8')
def _pad(self, text):
text_length = len(text)
padding_len = AES.block_size - int(text_length % AES.block_size)
if padding_len == 0:
padding_len = AES.block_size
t2 = chr(padding_len) * padding_len
t2 = t2.encode('utf8')
# print('text ', type(text), text)
# print('t2 ', type(t2), t2)
t3 = text + t2
return t3
def _unpad(self, text):
pad = ord(text[-1])
return text[:-pad] def encrypt(self, raw):
raw = raw.encode('utf8')
raw = self._pad(raw)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
encrypted = cipher.encrypt(raw)
return base64.b64encode(encrypted).decode('utf8')
def decrypt(self, enc):
enc = enc.encode('utf8')
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
decrypted = cipher.decrypt(enc)
return self._unpad(decrypted.decode('utf8'))
class AESSkyPay:
"""
Tested under Python 3.7 and pycryptodome
"""
BLOCK_SIZE = 16
def __init__(self, key):
#菲律宾支付通道 SkyPay Payment Specification.lending.v1.16.pdf
# SkyPay 对密码做了如下处理
s1 = hashlib.sha1(bytes(key, encoding='utf-8')).digest()
s2 = hashlib.sha1(s1).digest()
self.key = s2[0:16] self.mode = AES.MODE_ECB
def pkcs5_pad(self,s):
"""
padding to blocksize according to PKCS #5
calculates the number of missing chars to BLOCK_SIZE and pads with
ord(number of missing chars)
@see: http://www.di-mgt.com.au/cryptopad.html
@param s: string to pad
@type s: string
@rtype: string
"""
BS = self.BLOCK_SIZE
return s + ((BS - len(s) % BS) * chr(BS - len(s) % BS)).encode('utf8')
def pkcs5_unpad(self,s):
"""
unpadding according to PKCS #5
@param s: string to unpad
@type s: string
@rtype: string
"""
return s[:-ord(s[len(s) - 1:])] # 加密函数,如果text不足16位就用空格补足为16位,
# 如果大于16当时不是16的倍数,那就补足为16的倍数。
# 补足方法:PKCS5
def encrypt(self, text):
cryptor = AES.new(self.key, self.mode)
# 这里密钥key 长度必须为16(AES-128),
# 24(AES-192),或者32 (AES-256)Bytes 长度
# 目前AES-128 足够目前使用
ciphertext = cryptor.encrypt(self.pkcs5_pad(text.encode('utf8')))
# 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
# 所以这里将加密的字符串进行base64编码
return base64.b64encode(ciphertext).decode()
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode)
plain_text = cryptor.decrypt(base64.b64decode(text))
暂时禁止评论













闽公网安备 35020302000061号