Categories
Cryptography Python Security

Pure Python RSA implementation compatible OpenSSL

$ sudo aptitude install python-pyasn1
$ hg clone https://bitbucket.org/sybren/python-rsa/
$ cd python-rsa
$ sudo python setup.py install
$ cd ..

# Generation of a private key:
$ openssl genrsa -out myprivatekey.pem 512


# To get a Python-RSA-compatible public key from OpenSSL:
$ pyrsa-priv2pub -i myprivatekey.pem -o mypublickey.pem

# Get a public key with the standard method:
$ openssl rsa -in myprivatekey.pem -out public.pem -outform PEM -pubout
writing RSA key


# Test 1: Python-RSA-compatible public key
$ echo hello there > testfile.txt
$ pyrsa-encrypt -i testfile.txt -o testfile.rsa mypublickey.pem
Reading public key from mypublickey.pem
Reading input from testfile.txt
Encrypting
Writing output to testfile.rsa
$ openssl rsautl -in testfile.rsa -inkey myprivatekey.pem -decrypt
hello there


# Test 2: standard public key
$ openssl rsautl -encrypt -inkey public.pem -pubin -in testfile.txt -out file.ssl
$ pyrsa-decrypt -i file.ssl -o file.txt myprivatekey.pem 
Reading private key from myprivatekey.pem
Reading input from file.ssl
Decrypting
Writing output to file.txt
$ cat file.txt 
hello there

C’est le module Python RSA que j’utilise en général.