【发布时间】:2017-01-18 22:37:27
【问题描述】:
我遇到了这个挑战,我要求使用导出的公钥解密加密消息。我得到了 3 个文件:
- 加密python脚本
- 加密消息
- 导出的公钥
我尝试导入公钥然后解密,但我认为我必须找出私钥才能解密消息。
加密代码为:
import gmpy
from Crypto.Util.number import *
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
message = open('message', 'r').read() * 30
def ext_rsa_encrypt(p, q, e, msg):
m = bytes_to_long(msg)
while True:
n = p * q
try:
phi = (p - 1)*(q - 1)
d = gmpy.invert(e, phi)
pubkey = RSA.construct((long(n), long(e)))
key = PKCS1_v1_5.new(pubkey)
enc = key.encrypt(msg).encode('base64')
return enc
except:
p = gmpy.next_prime(p**2 + q**2)
q = gmpy.next_prime(2*p*q)
e = gmpy.next_prime(e**2)
p = getPrime(128)
q = getPrime(128)
n = p*q
e = getPrime(64)
pubkey = RSA.construct((long(n), long(e)))
f = open('pubkey.pem', 'w')
f.write(pubkey.exportKey())
g = open('msg.enc', 'w')
g.write(ext_rsa_encrypt(p, q, e, message))
【问题讨论】:
-
256 位密钥(原文如此!)非常小。您可以使用(一般)数字字段筛实现来分解模数。
-
@ArtjomB。谢谢,我现在创建了私钥,但是如何使用“key = PKCS1_v1_5.new(privatekey); key.decrypt(encmsg.decode('base64'))”解密消息? PKCS1_v1_5 需要一个哨兵。我怎样才能知道哨兵才能解密?
-
哨兵是一个对象,您可以在出现解密错误时取回。你决定哨兵是什么。
-1可能是哨兵。"chicken"可能是哨兵。你明白了。 -
@ArtjomB.,再次感谢! key.decrypt(encmsg.decode('base64'),"---") 返回错误“密文长度不正确”。我使用 n、e、d(以及与原始公钥完全对应的私钥)以正确的方式构建了密钥。有什么办法可以解决这个问题?
-
@RAHAB2 您可能必须使用密钥大小来拆分消息。