python - RSA Encryption Using Generated Key Pair
问题描述
I have been stuck on this problem since morning. I have generated key pair for RSA encryption using ssh-keygen.
Encryption works fine:
with open("keys.pub", "rb") as f:
pubkey = f.read()
with open("keys", "rb") as f:
prvkey = f.read()
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
msg = "test"
print("raw msg->", msg)
keyPub = RSA.importKey(pubkey) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode()) # now we have the cipher
print("cipher text->", cipher_text)
However, I get error in decryption:
keyPriv = RSA.importKey(prvkey) # import the private key
cipher = Cipher_PKCS1_v1_5.new(keyPriv)
decrypt_text = cipher.decrypt(cipher_text, None).decode()
print("decrypted msg->", decrypt_text)
assert msg == decrypt_text # check that
print("test passed")
ValueError: RSA key format is not supported
I am using PyCrypto 2.6.1. Any help would be greatly appreciated. I am not able to attach the keys due to to some server error.
解决方案
发出 RSA.importKey 后,变量 keyPriv 是否正确包含密钥?
您生成的 RSA 密钥对似乎与 pycrypto 2.6.1 不兼容。安装 pycryptodome 3,它是 pycrypto 的一个分支并且正在积极开发中,应该可以解决这个问题。
推荐阅读
- android-studio - 在 TabLayout 中的选项卡之间添加空格
- c# - 我想进入类的表单对象类型的构造函数
- node.js - 在 ID 数组中搜索具有相同 ID 的 mongoose 上的多个文档
- ios - 有人知道如何使用 Apple 的视觉框架进行实时文本识别吗?
- c# - 为射击脚本添加冷却时间
- java - 如何在 Spring Boot 的资源文件夹中添加可执行文件
- azure - 是否可以从我的 PC 上租用 Azure 磁盘并通过 iSCSI 连接到它们?
- python-3.x - discord.py 获取某个类别下的所有文本通道
- flutter - Flutter是否可以将每行中的项目对齐?
- go - 使用“go build”获取依赖项,即使它们在 vendor/