写点什么

加解密

用户头像
GalaxyCreater
关注
发布于: 2020 年 08 月 26 日



描述

使用aes base64加密



加解密函数

# coding: utf-8
#Python 2.7.3
import base64
from Crypto.Cipher import AES
v3_key = 'ZAdse41592653510'
v3_iv = 'AHkiled3566745'
#加密
def encrypt(plaintext, key, iv):
cryptor = AES.new(key, mode, iv)
ciphertext = cryptor.encrypt(pad(plaintext))
ciphertext = base64.encodestring(ciphertext)
return ciphertext
#解密
def decrypt(ciphertext, key, iv):
ciphertext = base64.decodestring(ciphertext)
cryptor = AES.new(key, mode, iv)
plaintext = cryptor.decrypt(ciphertext)
ride = plaintext[-1]
return plaintext.rstrip(ride)



检查密码

def checkPw(userid, pwd, encrypt_pwd):
result = decrypt(encrypt_pwd, v3_key, v3_iv)
check = userid + pwd
print("check",check)
print("result",result)
if check==result:
return True
return False



主函数

if __name__ == '__main__':
userid = "jack"
pwd = "1234abcd"
encrypt_pwd = encrypt(userid+pwd, v3_key, v3_iv)
print("pwd:",pwd, "encrypt_pwd:", encrypt_pwd)
print("user:" + userid + ",check password:" + pwd + ", encrypt string:"\
+ encrypt_pwd + "result:" +str(checkPw(userid, pwd, encrypt_pwd)))



输出结果

user:jack,check password:1234abcd, encrypt string:dx+OwMce/SFFnxXEBT85lw==

result:True



发布于: 2020 年 08 月 26 日阅读数: 52
用户头像

GalaxyCreater

关注

还未添加个人签名 2019.04.21 加入

还未添加个人简介

评论

发布
暂无评论
加解密