如何使用python中的RSA私钥加密数据? [英] How to encrypt data with RSA private key in python?

查看:678
本文介绍了如何使用python中的RSA私钥加密数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Python 2.7.1上安装了 pyCrypto 软件包来执行一些操作加密操作。

I've installed pyCrypto package on Python 2.7.1 to do some cryptography operations.

第一季度:
我要执行的操作是使用私钥对某些数据进行加密(而不是公钥)。该图书馆似乎无法做到这一点。我对吗?如果是这样,是否有图书馆能够做到这一点?

Q1: The operation that I want to do is encrypting some data with private Key (instead of public Key). It seems that this library can't do it. Am I right? If so, is there any library capable to do that?

第二季度:
文档并没有提到哪种哈希算法用于计算签名!我该如何找出用于 sign 方法的哈希函数?

Q2: In the documentation it is not mentioned which hash algorithm is used to calculate the signature! How can I find out which hash function is used for sign method?

第三季度:
您会看到有关加密方法的文档的一部分:

Q3: You see a part of documentation about encrypt method:


加密(自身,明文,K)

encrypt(self, plaintext, K)

使用RSA加密部分数据

Encrypt a piece of data with RSA.

参数:

纯文本(字节字符串或长整数)-要通过RSA加密的
数据。它的数值可能不超过RSA模块
(n)。

plaintext (byte string or long) - The piece of data to encrypt with RSA. It may not be numerically larger than the RSA module (n).

(已删节!)

如上所示,输入数据仅限于数字上不大于RSA模块的数据。这是否意味着我无法使用带有模块= 0x11 ...(257的RSA密钥对)来加密 0x21 ...(257字节)个字节)(例如),因为0x21大于0x11?如果是这样,为什么?每次在加密之前比较这些值是不是很奇怪?还是仅意味着数据长度必须等于或小于模块长度?

As you see above, the input data is limited to those that are not numerically larger than the RSA module. Does this mean that I can't encrypt 0x21...(257 bytes) with an RSA key pair with module = 0x11...(257 bytes) (for example) because 0x21 is greater than 0x11? If so, why? Isn't it weird to compare the values before encryption each time?! Or it is only meant that the data length must be equal or smaller than the module length?

推荐答案

在公共密钥加密中,您可以请勿使用私钥加密-您始终使用公钥。否则,由于公钥是公钥,因此任何人都可以解密密文。

In public key cryptography, you do not encrypt with the private key -- you always use the public key. Otherwise, since the public key is "public", anybody could decrypt the ciphertext.

您可能会倾向于互换使用公钥和私钥,但是通常,私钥,您无需做很多工作就可以弄清楚公钥。因此,如果您让某人的私钥认为您将保持公钥的安全,那将是不安全的。

You might be tempted to use the public and private keys interchangeably, but generally, given the private key, you can figure out the public key without much work. So, if you give someone the private key thinking that you will keep the public key safe, well, it won't be safe.

当他们说不大于模数,它们表示以字节为单位的大小。实际上,它会小于模数的大小(2048位RSA密钥对为256个字节)。但是数据实际上应该比模块小,因为您将希望始终填充数据。填充(例如,使用OAEP填充)可将密文随机化。每次对相同的明文进行加密时,都会得到看起来随机的不同密文。这很重要,因为否则,即使攻击者没有私钥,密文也很弱并且容易受到攻击。因此,您要为数据和填充留出一定的空间以适合模数(例如256个字节)。

When they say not larger than the modulus, they mean the size in bytes. It will actually be smaller that the size of the modulus (256 bytes for a 2048 bit RSA key pair). But the data should actually be smaller than the modules because you will want to always pad the data. Padding, for example with OAEP padding, randomizes the ciphertext. Each time you encrypt the same plaintext, you get different ciphertext that looks random. This is important because otherwise the ciphertext is weak and open to attack even if the attacker does not have the private key. So you want to leave some room for the data plus the padding to fit in the modulus (e.g. 256 bytes).

通常,您使用私钥签名。在RSA中,这实际上是用私钥加密的,但是我认为您不会在任何流行的API中找到用私钥加密。

Generally, you sign with the private key. In RSA, this actually does "encrypt" with the private key, but I don't think you will find "encrypt with private key" in any popular API.

I我对pyCrypto并不熟悉,但是在我看来,您在签名时选择了自己喜欢的哈希。据我从谷歌搜索的示例中所知,您可以对自己进行哈希处理,然后将摘要提供给sign函数。

I am not familiar with pyCrypto but it looks to me like you pick your favorite hash when signing. You hash yourself, and give the digest to the sign function, as far as I can tell from examples I've googled.

这篇关于如何使用python中的RSA私钥加密数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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