Python中的RSA加密和解密

Python中的RSA加密和解密,python,encryption,rsa,pycrypto,Python,Encryption,Rsa,Pycrypto,我需要在Python中使用RSA加密和解密的帮助 我正在创建私钥/公钥对,用密钥加密消息并将消息写入文件。然后我从文件中读取密文,并使用密钥解密文本 我在解密部分遇到问题。正如您在下面的代码中所看到的,当我输入decrypted=key.decrypt(message)时,程序运行正常,但解密后的消息会再次加密。它似乎没有从文件中读取密文 有谁能帮我写下这段代码,让解密从文件中读取密文,然后用密钥解密密文 import Crypto from Crypto.PublicKey import RS

我需要在Python中使用RSA加密和解密的帮助

我正在创建私钥/公钥对,用密钥加密消息并将消息写入文件。然后我从文件中读取密文,并使用密钥解密文本

我在解密部分遇到问题。正如您在下面的代码中所看到的,当我输入
decrypted=key.decrypt(message)
时,程序运行正常,但解密后的消息会再次加密。它似乎没有从文件中读取密文

有谁能帮我写下这段代码,让解密从文件中读取密文,然后用密钥解密密文

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random

random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate public and private keys

publickey = key.publickey # pub key export for exchange

encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'

print 'encrypted message:', encrypted #ciphertext

f = open ('encryption.txt', 'w'w)
f.write(str(encrypted)) #write ciphertext to file
f.close()

#decrypted code below

f = open ('encryption.txt', 'r')
message = f.read()

decrypted = key.decrypt(message)

print 'decrypted', decrypted

f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()

为了使其工作,您需要在解密之前将密钥从str转换为tuple(ast.literal\u eval函数)。下面是固定代码:

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast

random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate pub and priv key

publickey = key.publickey() # pub key export for exchange

encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'

print 'encrypted message:', encrypted #ciphertext
f = open ('encryption.txt', 'w')
f.write(str(encrypted)) #write ciphertext to file
f.close()

#decrypted code below

f = open('encryption.txt', 'r')
message = f.read()


decrypted = key.decrypt(ast.literal_eval(str(encrypted)))

print 'decrypted', decrypted

f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()
和使用

KEYS_DIRECTORY = settings.SURVEY_DIR_WITH_ENCRYPTED_KEYS

class TestingEncryption(RSAEncryption):
    PRIVATE_KEY_FILE_PATH = KEYS_DIRECTORY + 'private.key'
    PUBLIC_KEY_FILE_PATH = KEYS_DIRECTORY + 'public.key'


# django/flask
from django.core.files import File

class ProductionEncryption(RSAEncryption):
    PUBLIC_KEY_FILE_PATH = settings.SURVEY_DIR_WITH_ENCRYPTED_KEYS + 'public.key'

    def _get_private_key(self):
        """run generate_keys() before get keys """
        from corportal.utils import global_elements
        private_key = global_elements.request.FILES.get('private_key')
        if private_key:
            private_key_file = File(private_key)
            return private_key_file.read()

message = 'Hello мой friend'
encrypted_mes = ProductionEncryption().encrypt(message)
decrypted_mes = ProductionEncryption().decrypt(message)

下面是我对Python3和pycrypto的实现

from Crypto.PublicKey import RSA
key = RSA.generate(4096)
f = open('/home/john/Desktop/my_rsa_public.pem', 'wb')
f.write(key.publickey().exportKey('PEM'))
f.close()
f = open('/home/john/Desktop/my_rsa_private.pem', 'wb')
f.write(key.exportKey('PEM'))
f.close()

f = open('/home/john/Desktop/my_rsa_public.pem', 'rb')
f1 = open('/home/john/Desktop/my_rsa_private.pem', 'rb')
key = RSA.importKey(f.read())
key1 = RSA.importKey(f1.read())

x = key.encrypt(b"dddddd",32)

print(x)
z = key1.decrypt(x)
print(z)
PKCS#1oaep是一种基于RSA和OAEP填充的非对称密码

from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Cipher import PKCS1_OAEP


def rsa_encrypt_decrypt():
    key = RSA.generate(2048)
    private_key = key.export_key('PEM')
    public_key = key.publickey().exportKey('PEM')
    message = input('plain text for RSA encryption and decryption:')
    message = str.encode(message)

    rsa_public_key = RSA.importKey(public_key)
    rsa_public_key = PKCS1_OAEP.new(rsa_public_key)
    encrypted_text = rsa_public_key.encrypt(message)
    #encrypted_text = b64encode(encrypted_text)

    print('your encrypted_text is : {}'.format(encrypted_text))


    rsa_private_key = RSA.importKey(private_key)
    rsa_private_key = PKCS1_OAEP.new(rsa_private_key)
    decrypted_text = rsa_private_key.decrypt(encrypted_text)

    print('your decrypted_text is : {}'.format(decrypted_text))

您可以使用简单的方法来生成RSA。使用rsa库

pip install rsa 
小心使用加密!!!
这是一个很棒的库,但在python3.8中有一个问题,因为从库时间中删除了属性clock()。要修复它,只需修改
/usr/lib/python3.8/site packages/Crypto/Random/\u userfriendlyring.py
第77行中的源代码,更改
t=time.clock()
int
t=time.perf\u counter()

我在解密部分遇到问题,这并不能很好地描述您的问题。请阅读。你的问题应该包括你的输入、输出和预期的输出。编辑有帮助,但如果你包含了程序的输出和预期的输出,那么编辑仍然会有帮助。你的代码中有多处输入错误。那么你到底尝试了什么呢?记住这是一个很弱的方法,特别是对于短消息。Padding(通过PKCS#1)解决了PyCrypto(以及PyCryptodome,维护的fork)可以解决的这些问题!非常感谢你!我从来没有想到要补充这一点。我感谢你的帮助@Alysson PyCrypto文档介绍了publickey.encrypt():它建议(对于代码示例,请遵循链接)您可以为Python 3.3+制作一个版本吗?encrypted=publickey.encrypt('encrypt this message',32)-pow()不支持的操作数类型:'str',int',“int”您能解释一下如何对加密结果进行Base64编码吗?您使用过哪种加密模块(例如pycryptodome)?pycryptodome是完整的文档对您来说是不可能的这种加密整个文件的方法\u RSAobj对象没有“导出密钥”属性
pip install rsa