使用模数和指数生成私钥c语言,我有模数和私有指数。如何构造RSA私钥并签名?...

实际上,为了解密用公钥加密的消息,有了私有指数就足够了。

这也意味着您可以对消息进行签名,因为签名基本上就是用私钥对明文进行*de*加密,当用公钥对明文进行*en*加密时,私钥将再次给出明文。通常你会在明文之前使用一个哈希摘要并签名。。。

不能仅用n和d对消息进行解密的原因是,它在消息解密过程中执行blinding step,这是involves the public exponent,但解密并不真正需要。

但是通过使用对私有API的一些调用,可以绕过这一步。

因此,这应该起作用:from Crypto.PublicKey import RSA

from Crypto.Util.number import bytes_to_long, long_to_bytes

full = RSA.generate(2048)

# construct key using only n and d

try:

# pycrypto >=2.5, only tested with _slowmath

impl = RSA.RSAImplementation(use_fast_math=False)

partial = impl.construct((full.n, 0L))

partial.key.d = full.d

except TypeError:

# pycrypto <=2.4.1

partial = RSA.construct((full.n, 0L, full.d))

pub = full.publickey()

# create message with padding

# http://en.wikipedia.org/wiki/RSA_%28algorithm%29#Padding_schemes

cleartext = ...

signature = partial.sign(cleartext, None)

print "validating message: ", pub.verify(cleartext, signature)

message = pub.encrypt(cleartext, None)

# bypassing the blinding step on decrypt

enc_msg=map(bytes_to_long, message)

dec_msg = map(partial.key._decrypt, enc_msg)

print "decrypting: "

for m in dec_msg:

print long_to_bytes(m)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值