Python RSA操作

6 篇文章 0 订阅

公钥加密、私钥解密

# -*- coding: utf-8 -*-
import rsa

# rsa加密
def rsaEncrypt(str):
    # 生成公钥、私钥
    (pubkey, privkey) = rsa.newkeys(512)
    print("pub: ", pubkey)
    print("priv: ", privkey)
    # 明文编码格式
    content = str.encode('utf-8')
    # 公钥加密
    crypto = rsa.encrypt(content, pubkey)
    return (crypto, privkey)


# rsa解密
def rsaDecrypt(str, pk):
    # 私钥解密
    content = rsa.decrypt(str, pk)
    con = content.decode('utf-8')
    return con


(a, b) = rsaEncrypt("hello")
print('加密后密文:')
print(a)
content = rsaDecrypt(a, b)
print('解密后明文:')
print(content)  

密钥导出、签名验证

# -*- coding: utf-8 -*-
import rsa

# 先生成一对密钥,然后保存.pem格式文件,当然也可以直接使用
(pubkey, privkey) = rsa.newkeys(1024)

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

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

# load公钥和密钥
message = 'lovesoo.org'
with open('public.pem', "rb") as publickfile:
    p = publickfile.read()
    pubkey = rsa.PublicKey.load_pkcs1(p)
    print(pubkey)
with open('private.pem', "rb") as privatefile:
    p = privatefile.read()
    privkey = rsa.PrivateKey.load_pkcs1(p)
    print(privkey)
# 用公钥加密、再用私钥解密
crypto = rsa.encrypt(message.encode('utf-8'), pubkey)
message = rsa.decrypt(crypto, privkey)
message = message.decode('utf-8')
print (message)

# sign 用私钥签名认证、再用公钥验证签名
signature = rsa.sign(message.encode('utf-8'), privkey, 'SHA-1')
method_name = rsa.verify('lovesoo.org'.encode('utf-8'), signature, pubkey)
print(method_name)

对文件进行RSA加密解密

from rsa.bigfile import *
import rsa

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)
with open('mysec.txt', 'rb') as infile, open('outputfile', 'wb') as outfile: #加密输出
    encrypt_bigfile(infile, outfile, pubkey)

with open('outputfile', 'rb') as infile2, open('result', 'wb') as outfile2:  #解密输出
    decrypt_bigfile(infile2, outfile2, privkey)

4.0的主要变化
版本3.4是3.x范围内的最后一个版本。版本4.0删除了以下模块,因为它们不安全:

rsa._version133
rsa._version200
rsa.bigfile
rsa.varblock
这些模块在3.4版中被标记为已弃用。

此外,在4.0中,I / O函数经过简化,可以在所有支持的Python版本上使用字节。

4.0版本不再支持Python 2.6和3.3。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Python中的RSA是一种非对称加密算法,用于数据的加密和解密。您可以使用Python中的标准库`cryptography`来实现RSA算法。 下面是一个使用Python进行RSA加密和解密的示例代码: ```python from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.asymmetric import rsa, padding # 生成RSA密钥对 private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048 ) public_key = private_key.public_key() # 将密钥保存为PEM格式 pem_private_key = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) pem_public_key = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) # 加密 message = b"Hello, RSA!" ciphertext = public_key.encrypt( message, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) # 解密 plaintext = private_key.decrypt( ciphertext, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) print("原始消息:", message) print("加密后:", ciphertext) print("解密后:", plaintext) ``` 这是一个简单的示例,展示了如何使用Python中的`cryptography`库进行RSA加密和解密操作。请注意,为了运行此代码,您需要先安装`cryptography`库。可以使用`pip install cryptography`命令进行安装。 希望这可以帮助到您!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gmHappy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值