[4962] in cryptography@c2.net mail archive

home help back first fref pref prev next nref lref last post

Python RSA

daemon@ATHENA.MIT.EDU (Eugene Leitl)
Fri Jun 25 00:30:29 1999

From: Eugene Leitl <eugene.leitl@lrz.uni-muenchen.de>
Date: Thu, 24 Jun 1999 16:07:44 -0700 (PDT)
To: cryptography@c2.net


Got this from Mordy Ovits <movits@lockstar.com>

Is the following of more than trivial value? It does seem to use L
integers... 

#!/usr/bin/python 
from sys import*;from string import*;a=argv;[s,p,q]=filter(lambda x:x[:1]!=
'-',a);d='-d'in a;e,n=atol(p,16),atol(q,16);l=(len(q)+1)/2;o,inb=l-d,l-1+d
while s:s=stdin.read(inb);s and map(stdout.write,map(lambda i,b=pow(reduce(
lambda x,y:(x<<8L)+y,map(ord,s)),e,n):chr(b>>8*i&255),range(o-1,-1,-1)))

Invoked as:

	echo 'Top secret message.' | rsa.py 10001 1967cb529 >ciphertext
	cat ciphertext             | rsa.py -d ac363601 1967cb529 

	* rsa.py (4 lines): Performs RSA public key
encryption/decryption.  It requires two arguments, and can accept a
single option: '-d' for decryption (the default action is encryption).
The first argument must be the required exponent, expressed in
hexadecimal, and the second is the modulus, also in hex.  You still
have to choose the correct exponent, whether the '-d' option is
present or not; '-d' simply changes the number of bytes read at a
single time.

	As an example: Let us assume the modulus is 6819722537, the
encryption exponent is 65537, and the decryption exponent is
2889233921.  Then, after converting the numbers to hex, we can encrypt
and then decrypt by the following commands:



home help back first fref pref prev next nref lref last post