# 商业转载请联系作者获得授权,非商业转载请注明出处。
# For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source.
# 协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
# 作者(Author):syy
import base64
import os
import sys
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKC
from Crypto import Random
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5 as Signature_PKC
class HandleRSA():
def creatRSA_key(self):
# 伪随机数生成器
random_gen = Random.new().read
# 生成秘钥对实例对象:1024是秘钥的长度
rsa = RSA.generate(1024, random_gen)
'''# Server的秘钥对的生成
private_pem = rsa.exportKey()
with open("server_private.pem", "wb") as f:
f.write(private_pem)
public_pem = rsa.publickey().exportKey()
with open("server_public.pem", "wb") as f:
f.write(public_pem)'''
# Client的秘钥对的生成
private_pem = rsa.exportKey()
with open("client_private.pem", "wb") as f:
f.write(private_pem)
public_pem = rsa.publickey().exportKey()
with open("client_public.pem", "wb") as f:
f.write(public_pem)
# Server使用Client的公钥对内容进行rsa 加密
def encrypt(self, plaintext):
"""
client 公钥进行加密
plaintext:需要加密的明文文本,公钥加密,私钥解密
"""
# 加载公钥
rsa_key = RSA.import_key(open("client_public.pem").read())
# 加密
cipher_rsa = Cipher_PKC.new(rsa_key)
en_data = cipher_rsa.encrypt(plaintext.encode("utf-8")) # 加密
# base64 进行编码
base64_text = base64.b64encode(en_data)
return base64_text.decode() # 返回字符串
# Client使用自己的私钥对内容进行rsa 解密
def decrypt(self, en_data):
"""
en_data:加密过后的数据,传进来是一个字符串
"""
# base64 解码
try:
base64_data = base64.b64decode(en_data.encode("utf-8"))
# 读取私钥
private_key = RSA.import_key(open("client_private.pem").read())
# 解密
cipher_rsa = Cipher_PKC.new(private_key)
data = cipher_rsa.decrypt(base64_data, None)
return data.decode()
except:
print('解密失败,秘钥对不匹配')
sys.exit('程序退出!')
a=HandleRSA()
#获取账号信息函数
def getMessage(search_name):
f=open(pwdName,mode='r')
d=f.readlines()
f.close()
for i in d:
c = a.decrypt(i)
if search_name in c:
print(c)
break
elif not search_name in c:
pass
else:
print('你要查找的账号不存在!')
#添加账号函数
def message():
WebsiteName = '| ' + '网站名称:' + input("请输入网站名称:")
Weburl = ' 网站链接:' + input("请输入网站链接:")
account = ' 账号:' + input("请输入账号:")
pwd = ' 密码:' + input("请输入密码:") + ' |'
strmsg = WebsiteName + Weburl + account + pwd
return strmsg
def addMessage():
plaintext = message()
b = a.encrypt(plaintext)
f1 = open(pwdName, mode='a')
f1.write(b+'\n')
f1.close()
print('添加成功!')
#修改密码函数
def changePwd(search_name,old_pwd,new_pwd):
f = open(pwdName, mode='r')
d = f.read().splitlines()
f.close()
os.remove(pwdName)
f1 = open(pwdName, mode='w')
f1.close()
for i in d: #i还是加密后的内容
c = a.decrypt(i)
if search_name in c:
if not '密码:' + old_pwd + ' |' in c: # 防止判断时依据为账号
print('原密码错误!')
f5 = open(pwdName, mode='a')
f5.write(i + '\n')
f5.close()
continue
else:
e = c.replace('密码:'+old_pwd+' |','密码:'+new_pwd+' |') # 这里为文本信息
g = a.encrypt(e) # 这里为加密后的内容
f2 = open(pwdName, mode='a')
f2.write(g+'\n')
f2.close()
print('修改成功!')
break
elif not search_name in c:
f4 = open(pwdName, mode='a')
f4.write(i + '\n')
f4.close()
else:
print('修改失败!')
#删除账号信息函数
def delePwd(dele_webname):
f = open(pwdName, mode='r')
date = f.read().splitlines()
f.close()
os.remove(pwdName)
f1 = open(pwdName, mode='w')
f1.close()
for i in date:
#b = i.replace("b'", '').replace("'", '')
#a = bytes(b, encoding='utf-8')
msg = a.decrypt(i) # 解码后的内容(一行)
if not dele_webname in msg:
f6 = open(pwdName, mode='a')
f6.write(i + '\n')
f6.close()
elif dele_webname in msg:
print('删除成功!')
break
else:
print('该网站不存在!')
if __name__ == '__main__':
pwdName='E:/MESSAGE.txt'
print('Tip1:首次使用请输入 creatkey 创建秘钥对,否则程序无效')
print('Tip2:请勿重复创建秘钥对,否则可能导致无法解密')
print('------开始界面------')
print('1.密码查询')
print('2.新增密码')
print('3.修改密码')
print('4.删除密码')
print('------开始界面------')
num=input('请输入你要进行操作的序号或creatkey创建秘钥对:')
if num == 'creatkey':
if os.path.exists("client_public.pem"):
print('秘钥对已存在,请勿重复创建,或删除后创建。')
sys.exit('程序退出!')
else:
a.creatRSA_key()
print('创建秘钥对完成,请妥善保管私钥!')
elif num == '1': # 密码查询
search_name=input('请输入你要查找的账号:')
getMessage(search_name)
elif num == '2':
addMessage()
elif num == '3':
search_name = input('请输入你要修改密码的网站名称:')
old_pwd = input('请输入原始密码:')
new_pwd = input('请输入新密码:')
changePwd(search_name,old_pwd,new_pwd)
elif num == '4':
dele_webname=input('请输入你要删除账号的网站名称:')
delePwd(dele_webname)
else:
print('请输入正确的序号!')
昨天发了一个没有加密的,根据评论区建议学了一下RSA加密,改了一下,
还可能存在诸多bug或不足,若有发现烦请指正,或者有其他建议也可以提出,万分感谢! |