欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python如何用代码实现一款rsa加解密工具?

程序员文章站 2022-04-19 17:43:52
python如何实现一款rsa加解密工具? 由于最近本人老忘记许多密码,直接把密码明文传到网盘或者其他地方太不安全。便想着利用rsa的公钥将容易忘记的密码加密后再上传到网上,下次...

python如何实现一款rsa加解密工具?

由于最近本人老忘记许多密码,直接把密码明文传到网盘或者其他地方太不安全。便想着利用rsa的公钥将容易忘记的密码加密后再上传到网上,下次忘记了密码,直接将保存到网上的东西下载下来利用本地的私钥进行解密。


程序代码

# -*- coding: utf-8 -*-
#
# @author: lihailin<415787837@qq.com>
# 

import rsa
from optparse import OptionParser

def save_keys(pubkey, prikey):
    '''
    持久化公私钥
    '''
    with open('public.pem', 'w+') as f:
        f.write(pubkey.save_pkcs1().decode())

    with open('private.pem', 'w+') as f:
        f.write(prikey.save_pkcs1().decode())

def load_pub(path):
    '''
    导入公钥
    '''
    with open(path,'r') as f:
        pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
        return pubkey
    print 'load public key error'
    return -1

def load_pri(path):
    '''
    导入私钥
    '''
    with open(path,'r') as f:
        prikey = rsa.PrivateKey.load_pkcs1(f.read().encode())
        return prikey
    print 'load private key error'
    return -1

def readf(path):
    f = open(path)
    return f.read()

def writef(path, cnt):
    f = open(path, 'wb')
    return f.write(cnt)

def arg():
    opt = OptionParser()

    opt.add_option('--genkey',
            action="store_true",
            dest="is_genkey",
            default=False,
            help="to generate rsa key")

    opt.add_option('--enorde',
            action="store",
            dest="erd",
            default=False,
            help="value is encry or decry")


    opt.add_option('--infile',
            action="store",
            dest="infile",
            default=False,
            help="--input file")

    opt.add_option('--outfile',
            action="store",
            dest="outfile",
            default=False,
            help="--output file")

    opt.add_option('--keyfile',
            action="store",
            dest="kf",
            default=False,
            help="--the key to encry file")

    (options, args) = opt.parse_args()
    return options

def main():
    options = arg()
    if options.is_genkey:
        # rsa生成公私钥
        (pubkey, prikey) = rsa.newkeys(1024)
        save_keys(pubkey, prikey) 
    if options.infile and options.outfile and options.kf:
        f = readf(options.infile)
        if options.erd == 'encry':
            pubkey = load_pub(options.kf)
            # 公钥加密
            crypto = rsa.encrypt(f, pubkey)
            writef(options.outfile, crypto)
            print 'encrypt complited'
        if options.erd == 'decry':
            prikey = load_pri(options.kf)
            # 私钥解密
            crypto = rsa.decrypt(f, prikey)
            writef(options.outfile, crypto)
            print 'edecrypt complited'


if __name__ == "__main__":
    main()


    '''
    # 公钥加密
    crypto = rsa.encrypt(message.encode(), pubkey)

    # 私钥解密
    message = rsa.decrypt(crypto, privkey).decode()
    print(message)
    '''
    '''
    with open('public.pem','r') as f:
        pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())

    with open('private.pem','r') as f:
        privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
    message = 'dsfsdfsdfssfs'
    # 私钥签名
    signature = rsa.sign(message.encode(), privkey, 'SHA-1')

    # 公钥验证
    print rsa.verify(message.encode(), signature, pubkey)
    '''

使用

生成公私钥

python rsa_tool.py --genkey

执行改代码后会自动生成公私钥匙private.pem、public.pem

加密

python rsa_tool.py --infile infile.txt --keyfile private.pem --outfile outfile.txt --enorde 'decry'

解密

python rsa_tool.py --infile outfile.txt --keyfile private.pem --outfile mingwen.txt --enorde 'decry'

帮助

python rsa_tool.py -h

环境

python2.7 pip install rsa