RSA加密算法的Python实现

Github:https://github.com/Myoontyee/RSA

1 环境

Windows7 x64
Python 2.7.14

2 步骤

i)输入明文;
ii)生成公匙、私匙;
iii)输出密文;
iv)检验。

3 代码

3.1 配置RSA库

该方案通过rsa库实现,可以在shell通过以下代码安装rsa库。
输入

pip install rsa

输出
通过pip安装rsa。

3.2 明文输入

输入明文。
输入

Code = 'Test1'
VerifyCode = 'Test1'

输出
'Test1’明文保存到Code里头。
VerifyCode内保存的’Test1’用于检验。

3.3 密匙生成

生成密匙并保存为.pem文件。
输入

(pubkey, privkey) = rsa.newkeys(2048)

pub = pubkey.save_pkcs1()
pubfile = open('public.pem', 'w+')
pubfile.write(pub)
pubfile.close()

pri = privkey.save_pkcs1()
prifile = open('private.pem', 'w+')
prifile.write(pri)
prifile.close()

输出
i)一对2048位的RSA密匙:公匙与私匙;
ii)保存在public.pem里的公匙;
iii)保存在private.pem里的私钥。
Tip
.pem文件可以用记事本打开

3.4 密文生成

先通过如下代码读入公匙、私匙。
输入

message = Code
with open('public.pem') as publickfile:
    p = publickfile.read()
    pubkey = rsa.PublicKey.load_pkcs1(p)

with open('private.pem') as privatefile:
    p = privatefile.read()
    privkey = rsa.PrivateKey.load_pkcs1(p)

输出
载入公匙、私匙。

后通过如下代码加密明文,结合Base64编码后输出。
输入

# 密文写入文件
def CypherWriter(CypherCodename):
    f = open('Cyphertext.txt', 'w')
    f.write(CypherCodename)
    f.close()
    
crypto = rsa.encrypt(message, pubkey)
crypto64 = base64.b64encode(crypto)
CypherWriter(crypto64)
print('The cyphertext is :')
print crypto64

输出
输出如下密文,并写入Cyphertext.txt文件。

The cyphertext is :
CuAUYnu7kLaq3bwwAzAR5liousn0ZlgDL5FxPZzkeHoYFWnf8me28cMRmyEpOf0jSc4X91xRSx8Jlx0FrNpjKe/Cjb+wixE6+leOTC4ZEK+zsd3PQhyOu+xtJy0gl8DsoSRkXx6S53EtWS4JotYE9fQb2hrYqA25fh5NIfelQNDjVM17XdHjOf8/bc6/fxEy1NpkVKlQ9gGUJGcnhHImSjATniyATfLlD/ymapuy93FTypoGIe4MZArF6xXEnMhjm0dVTa20vmARGAlU3zhw0Z2if9I7W1X4QuyOBv/lrn1ZwK8PPO57p5Op9ihXkv0uAlKiJHlEfC2sobVBUMZwcg==

Tip
加密后输出的文本是16进制的,并非都是可以打印的字符,需通过Base64编码后可正常打印输出。

3.5 检验与签名

将密文通过Base64解码,通过私匙解密,通过如下代码实现:
输入

crypto = base64.b64decode(crypto64)
message = rsa.decrypt(crypto, privkey)
print('The code is: ')
print message

输出

The code is: 
Test1

通过私钥签名认证、再用公钥验证签名,在签名相符的情况下程序可正常编译。
输入

signature = rsa.sign(message, privkey, 'SHA-1')
rsa.verify(VerifyCode, signature, pubkey)

输出
若签名正确,程序可正常编译。

4 全文

代码全文如下:

# -*- coding: utf-8 -*-
# Author:Myoontyee
# description:RSA
import rsa
import base64

# 密文写入文件
def CypherWriter(CypherCodename):
    f = open('Cyphertext.txt', 'w')
    f.write(CypherCodename)
    f.close()

# 初始化
Code = 'Test1'
VerifyCode = 'Test1'

# 生成一对密钥并保存
(pubkey, privkey) = rsa.newkeys(2048)

pub = pubkey.save_pkcs1()
pubfile = open('public.pem', 'w+')
pubfile.write(pub)
pubfile.close()

pri = privkey.save_pkcs1()
prifile = open('private.pem', 'w+')
prifile.write(pri)
prifile.close()

# 载入公钥和密钥
message = Code
with open('public.pem') as publickfile:
    p = publickfile.read()
    pubkey = rsa.PublicKey.load_pkcs1(p)

with open('private.pem') as privatefile:
    p = privatefile.read()
    privkey = rsa.PrivateKey.load_pkcs1(p)

# 用公钥加密,并用Base64编码后输出
crypto = rsa.encrypt(message, pubkey)
crypto64 = base64.b64encode(crypto)
CypherWriter(crypto64)
print('The cyphertext is :')
print crypto64

# 通过Base64解码,用私钥解密
crypto = base64.b64decode(crypto64)
message = rsa.decrypt(crypto, privkey)
print('The code is: ')
print message

# 签名 用私钥签名认证、再用公钥验证签名
signature = rsa.sign(message, privkey, 'SHA-1')
rsa.verify(VerifyCode, signature, pubkey)

5 参考

[1]python实现RSA加密解密方法
[2]rsa加密的出来的密文乱码 如何,怎么解决
[3]Python中进行Base64编码和解码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值