关于python3 aes_cbc_128加密的一些问题

Lllzhu 2019-06-24 03:35:17
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
这个偏移量需要怎么填写的?
...全文
481 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧