Python中的RSA加密和解密 [英] RSA encryption and decryption in Python
问题描述
我正在创建一个私钥/公钥对,用密钥加密消息并向文件写入消息。然后我正在从文件中读取密文,并使用密钥解密文本。
我在解密部分遇到问题。正如你可以在下面的代码中看到的那样,当我放入程序工作的 decryptpted = key.decrypt(message)
时,再次加密解密的消息。看起来它没有从文件中读取密文。
任何人都可以帮我写这段代码,以便解密从文件读取密文,然后使用密钥解密密文?
从Crypto.PublicKey导入Crypto
导入RSA
从Crypto导入随机
random_generator = Random.new()。读
key = RSA.generate(1024,random_generator)#生成公钥和私钥
publickey = key.publickey#pub key export for exchange
encrypted = publickey.encrypt '加密此消息',32)
#message加密在上面的一行'加密这个消息'
打印'加密消息:',加密#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 (消息)
打印'解密',解密
f = open('en
f.write(str(message))
f.write(str(decryptpted))
f.close()
为了使其工作,您需要在解密之前将密钥从str转换为元组(ast.literal_eval函数)。这是固定代码:
导入Crypto
从Crypto.PublicKey导入RSA
从Crypto import Random
import ast
random_generator = Random.new()。read
key = RSA.generate(1024,random_generator)#generate pub和priv key
publickey = key.publickey()#pub key export for exchange
encrypted = publickey.encrypt('加密此消息',32)
#message加密在上面的一行'encrypt这个消息'
print'加密消息:',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()
decryptpted = key.decrypt(ast.literal_eval(str(encrypted)))
打印'decryptpted'解密
f = open('encryption.txt','w')
f.write(str(message))
f.write(str(decryptpted))
f.close()
I need help using RSA encryption and decryption in Python.
I am creating a private/public key pair, encrypting a message with keys and writing message to a file. Then I am reading ciphertext from file and decrypting text using key.
I am having trouble with the decryption portion. As you can see in my code below, when I put in decrypted = key.decrypt(message)
that the program works, yet the decrypted message is encrypted again. It seems like it is not reading the ciphertext from the file.
Can anyone help me write this code so decryption reads ciphertext from file and then uses key to decrypt ciphertext?
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()
In order to make it work you need to convert key from str to tuple before decryption(ast.literal_eval function). Here is fixed code:
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()
这篇关于Python中的RSA加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!