def encrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) # 加密文本必须是bytes(utf8或ascii),而不能是unicode(str) text = OHHOOperation.to_bytes(text) # 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用 length = 16 count = len(text) add = length - (count % length) text = text + (b'\0' * add) self.ciphertext = cryptor.encrypt(text) # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 # 所以这里统一把加密后的字符串转化为16进制字符串 # 以unicode存储文件信息 return OHHOOperation.to_str(b2a_hex(self.ciphertext))
def check_verification_code(cellphone_number, code): code_in_redis = Code.get_code(cellphone_number) if code_in_redis: code_in_redis_unicode = OHHOOperation.to_str(code_in_redis) OHHOLog.print_log(code_in_redis_unicode) OHHOLog.print_log(code) if code_in_redis_unicode == code: Code.delete_code(cellphone_number) result = Result.result_success() else: result = Result.result_failed() else: result = Result.result_not_exist() return result
def decrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) # 解密文本必须是必须是bytes(utf8或ascii),而不能是unicode(str) utf8_text = OHHOOperation.to_bytes(text) plain_text = cryptor.decrypt(a2b_hex(utf8_text)) return OHHOOperation.to_str(plain_text.rstrip(b'\0'))
text = text + (b'\0' * add) self.ciphertext = cryptor.encrypt(text) # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 # 所以这里统一把加密后的字符串转化为16进制字符串 # 以unicode存储文件信息 return OHHOOperation.to_str(b2a_hex(self.ciphertext)) # 解密后,去掉补足的空格用strip() 去掉 def decrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) # 解密文本必须是必须是bytes(utf8或ascii),而不能是unicode(str) utf8_text = OHHOOperation.to_bytes(text) plain_text = cryptor.decrypt(a2b_hex(utf8_text)) return OHHOOperation.to_str(plain_text.rstrip(b'\0')) if __name__ == '__main__': pc = OHHOEncryption(OHHOOperation.to_bytes('keyskeyskeyskeys')) # 初始化密钥 e = pc.encrypt("00000") print(len(e)) d = pc.decrypt(e) print(d) from Tools.ohho_random import OHHORandom test = OHHORandom.get_nonce(15) test_password = OHHOEncryption(OHHOOperation.to_str("ztr7vnwg4jiaeqh8")) print(test_password.encrypt(test)) b = "e332f36055acd3e0272ab1b5a58b14fb" print(len(b))