pythonrsa数据加密_如何使用python中的RSA私钥加密数据?

I've installed

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?

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:

encrypt(self, plaintext, K)

Encrypt a piece of data with RSA.

Parameters:

plaintext (byte string or long) - The piece of data to

encrypt with RSA. It may not be numerically larger than the RSA module

(n).

(Censored!)

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.

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).

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 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Python编写的RSA加密工具类,可以使用指定的公钥和私钥进行数据加密和解密。 ```python import base64 from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 class RSAUtil: def __init__(self, public_key=None, private_key=None): if public_key: self.public_key = RSA.importKey(public_key) if private_key: self.private_key = RSA.importKey(private_key) def generate_key(self, bits=2048): self.key = RSA.generate(bits) self.public_key = self.key.publickey() self.private_key = self.key def export_public_key(self): return self.public_key.exportKey().decode('utf-8') def export_private_key(self): return self.private_key.exportKey().decode('utf-8') def encrypt(self, data): cipher = PKCS1_v1_5.new(self.public_key) ciphertext = cipher.encrypt(data.encode('utf-8')) return base64.b64encode(ciphertext).decode('utf-8') def decrypt(self, ciphertext): cipher = PKCS1_v1_5.new(self.private_key) data = cipher.decrypt(base64.b64decode(ciphertext), None) return data.decode('utf-8') ``` 使用示例: ```python public_key = """-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQ8QuKZ4kbwYpSfS+/x7c9j3q m7C4Jz4hF7QZbWkR+Yw5OfNlJN7V9yWdFyKv5/5a5e/7p8W+5g5w6x+U6H7A9Xy 6XOgB+OznPzjL6LZMcgJZM6aKj+Q2QJXOyRrmtURjI3DKb3L+1s3qE0GwX9AxRj OOJQ2F+I6rT8CnG3qQIDAQAB -----END PUBLIC KEY-----""" private_key = """-----BEGIN PRIVATE KEY----- MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANDxC4pniRvBilJ 9L7/Htz2PeqbsLgnPiEXtBltaRH5jDk585ySTe1fclnRcir+f/lrl7/unxb7mDn DrH5TofsD1fLpc6AH47Oc/PMsotkxyAlkzpoqP5DZAlc7JGua1RGMyNwyW9y/tb N6hNBsF/QMUYziUNhfiOq0/Apxt6kAgMBAAECgYB6xJU+HcOQkFTe6LfZwzJZP6 M+0X9R6jJvI2+1rI0rC8BZqJj1f+Gdp0zB72yU9X+U6uuJU6NcU6xZJ/l2bI6+P D0i/jz/9y+XmVksjTupm2n0JrW+LzqY3s3sMz7Jqu0Wf9X7El3q+3uK7VklGmxRZ Y37hJ1t2Qz0j4OEx9QJBAOcPzKjgV+JWJ5Z5wGZfz5a1+EpMsfW2Ji7KT/ak5zL EFa+D5lGw5Y5K5zg/UfjFtW8uV7q3t+G5V7S5bRmBvECQQDQ1QOT4n4BhrE0oIb Lr1Q2rT/GJ0/cZ6Y1qIzEBZiJc0oUkXyvPc6Ugwi6U1x6uvFQZwY+ZU84gDuxzHI v7eZAkEA1iLcJx+0zLw5LV5H5lGZf1FZ5gOVf7F3tAVh9V7PzOyBjFCYiYtZP4V4 4jQO4+TmIliE9XrTnTJ2A/3qf3zN8wJBAI6zohsJvA9+hUW8yUzC6gMn6BzE6Q2 TwfzG6b0i6Gv6cWd9XgU6JF+6xkH0CQe6OXJFzg0b6P7+TTH0y9XrECQAsTJIfO s+1aZLs0GpPfOJz7VhN9l9X7V/Ox5tDgYhZiJtZ8z4ZBEmj4N7JmH2m9yjg= -----END PRIVATE KEY-----""" rsa = RSAUtil(public_key, private_key) # 加密 data = "hello world" encrypted_data = rsa.encrypt(data) print("加密后的数据:", encrypted_data) # 解密 decrypted_data = rsa.decrypt(encrypted_data) print("解密后的数据:", decrypted_data) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值