Python pyCrypto RSA 加密方法使用私钥或公钥给出相同的结果 [英] Python pyCrypto RSA encrypt method gives same results using private or public key

查看:57
本文介绍了Python pyCrypto RSA 加密方法使用私钥或公钥给出相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解公钥和私钥的 pyCrypto 加密和解密方法,但我看到了一些奇怪的东西.假设我有一组私钥和公钥,存储在文件 dummy_private.txt 和 dummy_public.txt 中.

我像这样创建了一个私钥对象和公钥对象:

private_key_file='dummy_private.txt'f = 打开(private_key_file,'r')privateKey = RSA.importKey(f.read(),None)f.close()public_key_file='dummy_public.txt'f = 打开(public_key_file,'r')publicKey = RSA.importKey(f.read(),None)f.close()

现在假设我想加密一些消息.我可以这样做:

s='这是一条超级秘密信息'sutf8=s.encode('utf8')enc=publicKey.encrypt(sutf8,None)[0]encb64=base64.encodestring(enc)打印公钥编码的消息是 %s" % (encb64,)

这是有道理的,因为我用公钥加密,我应该可以用私钥解密.

但是,我也可以使用私钥对上面的内容进行加密,它给了我相同的结果!

enc2=privateKey.encrypt(sutf8,None)[0]encb642=base64.encodestring(enc2)打印私钥编码的消息是 %s" % (encb642,)

当我使用私钥或公钥打印出加密数据的 base64 编码版本时,它们是相同的!这是为什么?

这引发了使用私钥对某些内容进行数字签名的问题.如果我可以使用公钥对某些内容进行签名并获得相同的结果,那么签名如何验证我是我所说的人?这一定是我不明白的加密方法的一些问题.有人可以解释一下吗?

由于使用公钥和私钥加密的结果相同,因此无论是使用私钥还是公钥进行加密,似乎都可以使用私钥进行解密.我完全不明白为什么可以用私钥加密并得到与用公钥加密一样的结果.

解决方案

当你用私钥加密时,pycrypto 实际上是在使用公钥(可以从私钥生成).

来源:PyCrypto:解密仅使用文件中的公钥(无私钥+公钥)

你会发现 pycrypto 有充分的理由不允许你使用公钥解密:

<预><代码>>>>publicKey.decrypt(enc2)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/RSA.py",第174行,解密返回 pubkey.pubkey.decrypt(self, ciphertext)文件/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/pubkey.py",第93行,解密明文=self._decrypt(密文)文件/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/RSA.py",第 239 行,在 _decryptmp = self.key._decrypt(cp)文件/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/_slowmath.py",第52行,_decrypt引发类型错误(没有私钥")类型错误:没有私钥

在数学上,RSA 可以使用私钥加密并使用公钥解密,但您不应该这样做.公钥是公开的——它是你很容易分享的东西,因此很容易传播.与使用对称密码和共享密钥相比,在这种情况下没有附加价值(请参阅:https://crypto.stackexchange.com/questions/2123/rsa-encryption-with-private-key-and-decryption-with-a-public-key)

从概念上讲,使用私钥加密"对消息签名更有用,而使用公钥解密"则用于验证消息.

更多背景:在PKCS中交换公钥/私钥#1 OAEP加解密

I'm trying to understand the pyCrypto encrypt and decrypt methods for public and private keys, and I'm seeing something strange. Suppose I have a set of private and public keys, stored in files dummy_private.txt and dummy_public.txt.

I create a private key object and public key object like this:

private_key_file='dummy_private.txt'
f = open(private_key_file, 'r')
privateKey = RSA.importKey(f.read(),None)
f.close()

public_key_file='dummy_public.txt'
f = open(public_key_file, 'r')
publicKey = RSA.importKey(f.read(),None)
f.close()

Now suppose I want to encrypt some message. I can do it like this:

s='This is a super secret message'
sutf8=s.encode('utf8')

enc=publicKey.encrypt(sutf8,None)[0]
encb64=base64.encodestring(enc)
print "Public key Encoded message is %s" % (encb64,)

This makes sense because I am encrypting with the public key and I should be able to decrypt with the private key.

However, I can also encrypt the above using the private key, and it gives me the same result!

enc2=privateKey.encrypt(sutf8,None)[0]
encb642=base64.encodestring(enc2)
print "Private key Encoded message is %s" % (encb642,)

When I print out the base64 encoded version of the encrypted data, using either the private key or the public key, they are the same! Why is that?

And this raises the problem of digitally signing something with the private key. If I can sign something with the public key and get the same results, then how does signing verify that I am who I say I am? This must be some issue with the encrypt method that I don't understand. Can someone please explain?

Since encrypting with both the public key and private key gives the same results, it appears that decrypting with the private key can be done regardless of whether the encryption was done with the private key or the public key. I'm totally confused as to why one could encrypt with the private key and get a result that is the same as if it were done with the public key.

解决方案

When you encrypt with a private key, pycrypto is actually using the public key (which can be generated from the private key).

Source: PyCrypto: Decrypt only with public key in file (no private+public key)

You'll find that pycrypto doesn't allow you to decrypt using the public key for good reason:

>>> publicKey.decrypt(enc2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/RSA.py", line 174, in decrypt
    return pubkey.pubkey.decrypt(self, ciphertext)
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/pubkey.py", line 93, in decrypt
    plaintext=self._decrypt(ciphertext)
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/RSA.py", line 239, in _decrypt
    mp = self.key._decrypt(cp)
  File "/usr/local/lib/python2.7/site-packages/pycrypto-2.6-py2.7-linux-x86_64.egg/Crypto/PublicKey/_slowmath.py", line 52, in _decrypt
    raise TypeError("No private key")
TypeError: No private key

Mathematically, RSA makes it possible to encrypt with the private key and decrypt with the public key, but you're not supposed to do that. The public key is PUBLIC - it's something you would readily share and thus would be easily disseminated. There's no added value in that case compared to using a symmetric cipher and a shared key (see: https://crypto.stackexchange.com/questions/2123/rsa-encryption-with-private-key-and-decryption-with-a-public-key)

Conceptually, "encrypting" with the private key is more useful for signing a message whereas the "decryption" using the public key is used for verifying the message.

More background: exchange public/private key in PKCS#1 OAEP encryption/decryption

这篇关于Python pyCrypto RSA 加密方法使用私钥或公钥给出相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆