Skip to content

Instantly share code, notes, and snippets.

@charlietuna
Last active March 17, 2016 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlietuna/6c5c2665f49f20694ceb to your computer and use it in GitHub Desktop.
Save charlietuna/6c5c2665f49f20694ceb to your computer and use it in GitHub Desktop.
RSA in 4 lines of Python.
#!/usr/bin/python # Usage: rsa.py exponent modulus < plaintext > ciphertext
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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment