关于python3 aes_cbc_128加密的一些问题
class AESCipher:
def __init__(self, key):
self.key = key[0:16] #只截取16位
self.iv = "YFGIVPARAMETERAA"
def __pad(self, text):
text_length = len(text)
amount_to_pad = AES.block_size - (text_length % AES.block_size)
if amount_to_pad == 0:
amount_to_pad = AES.block_size
pad = chr(amount_to_pad)
return text + pad * amount_to_pad
def __unpad(self, text):
pad = ord(text[-1])
return text[:-pad]
def encrypt(self, raw):
"""加密"""
raw = self.__pad(raw)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
return base64.b64encode(cipher.encrypt(raw))
def decrypt(self, enc):
"""解密"""
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv )
return self.__unpad(cipher.decrypt(enc).decode("utf-8"))
if __name__ == '__main__':
e = AESCipher('YFGPASSWORDAAAAA')
secret_data = "123456"
enc_str = e.encrypt(secret_data)
print('enc_str: ' + enc_str.decode())
dec_str = e.decrypt(enc_str)
print('dec str: ' + dec_str)
代码如上,运行之后报错
TypeError: Object type <class 'str'> cannot be passed to C code
这个偏移量需要怎么填写的?
def __init__(self, key):
self.key = key[0:16] #只截取16位
self.iv = "YFGIVPARAMETERAA"
def __pad(self, text):
text_length = len(text)
amount_to_pad = AES.block_size - (text_length % AES.block_size)
if amount_to_pad == 0:
amount_to_pad = AES.block_size
pad = chr(amount_to_pad)
return text + pad * amount_to_pad
def __unpad(self, text):
pad = ord(text[-1])
return text[:-pad]
def encrypt(self, raw):
"""加密"""
raw = self.__pad(raw)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
return base64.b64encode(cipher.encrypt(raw))
def decrypt(self, enc):
"""解密"""
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv )
return self.__unpad(cipher.decrypt(enc).decode("utf-8"))
if __name__ == '__main__':
e = AESCipher('YFGPASSWORDAAAAA')
secret_data = "123456"
enc_str = e.encrypt(secret_data)
print('enc_str: ' + enc_str.decode())
dec_str = e.decrypt(enc_str)
print('dec str: ' + dec_str)
代码如上,运行之后报错
TypeError: Object type <class 'str'> cannot be passed to C code
这个偏移量需要怎么填写的?
...全文
相关推荐
AES算法-python
基于python实现的AES算法和其五种工作模式
python实现的AES双向对称加密解密与用法分析
主要介绍了python实现的AES双向对称加密解密与用法,简单分析了AES加密解密算法的基本概念并结合实例形式给出了AES加密解密算法的相关实现技巧与使用注意事项,需要的朋友可以参考下
python3.6 实现AES加密的示例(pyCryptodome)
本篇文章主要介绍了python3.6 实现AES加密的示例(pyCryptodome),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
python aes算法库
python aes算法库,比较适合用了做测试
python实现AES加密和解密
主要为大家详细介绍了python实现AES加密和解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


