AEE加密解密
Posted 逍遥无名
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AEE加密解密相关的知识,希望对你有一定的参考价值。
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
class AesHandler(object):
def __init__(self, key, iv, mode):
self.key = key
self.iv = iv
self.mode = mode
self.BS = AES.block_size # Size of a data block (in bytes) 16
self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(
self.BS - len(s) % self.BS)
self.unpad = lambda s: s[0:-ord(s[-1])]
def encrypt(self, text):
text = self.pad(text)
cipher = AES.new(self.key, self.mode, self.iv)
cipher_text = cipher.encrypt(text)
return b2a_hex(cipher_text)
def decrypt(self, text):
cipher = AES.new(self.key, self.mode, self.iv)
plain_text = cipher.decrypt(a2b_hex(text))
return self.unpad(plain_text.rstrip(‘\0‘))
if __name__ == ‘__main__‘:
key = ‘zhyh37MmA67Ato%Z‘
iv = ‘[email protected]%TLoLCEMollMq‘
aes = AesHandler(key, iv, AES.MODE_CBC)
e = aes.encrypt("in the wifi.com")
print "加密:", e
d = aes.decrypt(e)
print "解密:", d
from binascii import b2a_hex, a2b_hex
class AesHandler(object):
def __init__(self, key, iv, mode):
self.key = key
self.iv = iv
self.mode = mode
self.BS = AES.block_size # Size of a data block (in bytes) 16
self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(
self.BS - len(s) % self.BS)
self.unpad = lambda s: s[0:-ord(s[-1])]
def encrypt(self, text):
text = self.pad(text)
cipher = AES.new(self.key, self.mode, self.iv)
cipher_text = cipher.encrypt(text)
return b2a_hex(cipher_text)
def decrypt(self, text):
cipher = AES.new(self.key, self.mode, self.iv)
plain_text = cipher.decrypt(a2b_hex(text))
return self.unpad(plain_text.rstrip(‘\0‘))
if __name__ == ‘__main__‘:
key = ‘zhyh37MmA67Ato%Z‘
iv = ‘[email protected]%TLoLCEMollMq‘
aes = AesHandler(key, iv, AES.MODE_CBC)
e = aes.encrypt("in the wifi.com")
print "加密:", e
d = aes.decrypt(e)
print "解密:", d
以上是关于AEE加密解密的主要内容,如果未能解决你的问题,请参考以下文章