ssh-keygen -t rsa
pip3 install pyCryptodome
from email import message from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 import base64 from Crypto.Signature import PKCS1_v1_5 def encode(): """加密并打印密码""" sign = 'C:/Users/hello/sign' # 私钥的存储地址 pub = 'C:/Users/hello/sign.pub' # 公钥的存储地址 with open(sign) as f: private = f.read() with open(pub) as f: public = f.read() private_key = RSA.import_key(private) public_key = RSA.import_key(public) digest = SHA256.new() message = 'deadline: 2022-03-01' # 这里可以修改截止时间 digest.update(message.encode()) signer = PKCS1_v1_5.new(private_key) code = signer.sign(digest) signature = base64.b64encode(code) print(signature.decode()) encode()
from email import message from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 import base64 from Crypto.Signature import PKCS1_v1_5 import json def decode(): """验证秘钥并返回一个布尔值""" pub = 'C:/Users/hello/sign.pub' # 给出公钥 with open(pub) as f: public = f.read() public_key = RSA.import_key(public) message = 'deadline: 2022-03-01' digest = SHA256.new() digest.update(message.encode()) reader = PKCS1_v1_5.new(public_key) verify = reader.verify(digest, base64.b64decode(Signature.encode())) return verify def permission(): """秘钥正确与否提示""" if decode(): print("OK") else: print('试用期已过,请联系管理员!') """如果用户已经填写过注册码,则无需再次输入; 若是新用户,则需要提交一次正确的验证码""" try: with open('keyname.json') as f: Signature = json.load(f) except FileNotFoundError: Signature = input('请输入注册码\n') with open('keyname.json', 'w') as f: json.dump(Signature, f) permission() else: permission()
至此,加密和解密文件都已编写成功,在解密文件中添加了一个功能。该文件将自动识别用户是否是首次试用软件。如果是,则会要求用户输入一次注册码,之后软件就自动识别,直到过期;若不是,则直接进入。
补充: