Encrypt a message with RSA in python
May 5, 2008 6 Comments
As an aside as I’m going through old posts: There’s a lot more that needs to be done to make this “good”. For one, the message shouldn’t be applied directly to RSA – rather, a hash should be used…. and for another, there’s no auth, and for another, the unsafe pickle might be code execution… below is just a toy for basic usage
For some people in my class this was easy, and others it was difficult. Some people have spent a good 40 hours on this, so I thought I’d post some code to help out. There isn’t much documentation on the crypto modules.
#!/usr/bin/env python from Crypto.Hash import MD5 from Crypto.PublicKey import RSA from Crypto.Util import randpool import pickle import socket import sys #generate the RSA key blah = randpool.RandomPool() RSAKey = RSA.generate(512, blah.get_bytes) RSAPubKey = RSAKey.publickey() #listen for a connection host = '' port = 12345 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host,port)) s.listen(1) print "Server is running on port %d; press Ctrl-C to terminate." % port while 1: clientsock, clientaddr = s.accept() print "got connection from ", clientsock.getpeername() #send the public key over clientsock.send(pickle.dumps(RSAPubKey)) rcstring = '' while 1: buf = clientsock.recv(1024) rcstring += buf if not len(buf): break clientsock.close() #done with the network stuff, at least for this connection #encmessage is the cipher text encmessage = pickle.loads(rcstring) print RSAKey.decrypt(encmessage)
#!/usr/bin/env python from Crypto.Hash import MD5 from Crypto.PublicKey import RSA from Crypto.Util import randpool import pickle import socket host = 'localhost' port = 12345 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) #this should loop around until a delimeter is read #or something similar rcstring = s.recv(2048) #this object is of type RSAobj_c, which only has public key #encryption is possible, but not decryption publickey = pickle.loads(rcstring) print publickey #encrypt the top secret data secretText = publickey.encrypt("Hello, this is Rich.", 32) s.sendall(pickle.dumps(secretText)) s.close()