MathMXC MathMXC - 6 months ago 215
Python Question

Python RSA encryption

I am trying to write a RSA encryption software tool that will use the same key every single time. here is what I have so far.

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

key = <_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>
publickey = key.publickey()
encrypted = publickey.encrypt('hi', 32)
print(encrypted)


I get a syntax error at line 5 pointing at the < sign. I know that this is a valid private key. what is the problem and how do I fix it. Also I am using python 2.7.3

[EDIT] I get the key from this code

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import os
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
print(key)
raw_input()


Also I am getting a 'RSA key format not supported error' from this code after the 'raw_input()'

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
text_file = open("keyfile.txt", "w")
text_file.write('<_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>')
text_file.close()
raw_input()
with open('keyfile.txt', 'r') as f:
externKey = f.readline()
key = RSA.importKey(externKey, passphrase=None)
publickey = key.publickey()
encrypted = publickey.encrypt('hi', 32)
print(encrypted)

Answer

First, <_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private> is not a valid key, not sure how you get this, but it is a string representation of your key as a Python object only, the actual key content is not presented, also note that you cannot rebuild the key object with this string representation.

Before you do RSA encrypt with your key, you should import your key from some place like File, Generating In Memory etc.

So what you should do is:

key = RSA.importKey(externKey, passphrase=None)

Where the externKey is a String, so you can load the key string from your key File with this way.

Or:

key = RSA.generate(bits, randfunc=None, progress_func=None, e=65537)

Where the bits is the strength of your key, e.g 2048.

Either way you will have an RSA key object (_RSAobj) returned, then you can do encryption as the rest of your code did.

[EDIT] Complete Code

import Crypto
from Crypto.PublicKey import RSA

#Quick way to generate a new key
private_key = RSA.generate(1024)

#Show the real content of the private part to console, be careful with this!
print(private_key.exportKey())

#Get the public part
public_key = private_key.publickey()

#Show the real content of the public part to console
print(public_key.exportKey())

#Save both keys into some file for future usage if needed
with open("rsa.pub", "w") as pub_file:
    pub_file.write(public_key.exportKey())

with open("rsa.pvt", "w") as pvt_file:
    pvt_file.write(private_key.exportKey())

#Load public key back from file and we only need public key for encryption
with open('rsa.pub', 'r') as pub_file:
    pub_key = RSA.importKey(pub_file.read())

#Encrypt something with public key and print to console
encrypted = pub_key.encrypt('hello world', None) # the second param None here is useless
print(encrypted)

#Load private key back from file and we must need private key for decryption
with open('rsa.pvt', 'r') as pvt_file:
    pvt_key = RSA.importKey(pvt_file.read())

#Decrypt the text back with private key and print to console
text = pvt_key.decrypt(encrypted)
print(text)
Comments