Use RSA with Python

Ng Pheng Siong ngps at madcap.dyndns.org
Tue Nov 14 15:58:08 CET 2000


According to guillaume.gonnord <guillaume.gonnord at wanadoo.fr>:
> I have found m2crypto lib that implement RSA but it uses two files as
> private and public key.
> I would like to know how can I generate this two files.
> I have tried with openssl, but it generates keys with a different file
> format.

Generate the key pair:

/tmp:$ openssl genrsa -?
usage: genrsa [args] [numbits]
 -des            encrypt the generated key with DES in cbc mode
 -des3           encrypt the generated key with DES in ede cbc mode (168 bit key)
 -idea           encrypt the generated key with IDEA in cbc mode
 -out file       output the key to 'file
 -passout arg    output file pass phrase source
 -f4             use F4 (0x10001) for the E value
 -3              use 3 for the E value
 -rand file:file:...
                 load the file (or the files in the directory) into
                 the random number generator

/tmp:$ openssl genrsa > xxx.pem
warning, not much extra random data, consider using the -rand option
Generating RSA private key, 512 bit long modulus
...++++++++++++
...............++++++++++++
e is 65537 (0x10001)

/tmp:$ cat xxx.pem
-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBANMpyvFD+QSf88l+fzrJTWqVKmfZOycDeBfnmsSNNq6V/zneZwQA
SzcgVhGtxsNyVxBqCrMHA16kxDP4N8tGHvcCAwEAAQJAMUMJtjlOSbbrD7G0CsnV
hqzf5LQ36YUUqjb80yfCfv3Qbdg8rzJlGaJ27hc8YMbhCxBn/s5MsVAwObaeoA4/
OQIhAPrqRhov+vetwudfy6OiPtfTbNV1pvYJll5FDji4OQd9AiEA13FKIUUGcXUt
zYYg2karicm3JlnI8f7krgBY4vu6koMCIQD2KuwV+ybE4kQKzfAzHRV8l1/6WJAy
aJuO5z8ZpJvG3QIgUHvnKKSjspEHCmeYMFMEVFZCEJasJhOGFKbWjf1ihscCIQCU
H47BdEyo2uJszIeSExi2cDvw6rRis1md/JFPIbb/QA==
-----END RSA PRIVATE KEY-----


Use the key from M2Crypto:

/tmp:$ python1.5
Python 1.5.2 (#2, Mar 12 2000, 12:38:38)  [GCC 2.95.2 19991024 (release)] on freebsd4
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from M2Crypto import RSA
>>> dir(RSA)
['BIO', 'Err', 'RCS_id', 'RSA', 'RSA_pub', '__builtins__', '__doc__', '__file__', '__name__', 'load_key', 'load_key0', 'load_pub_key', 'load_pub_key0', 'm2', 'new_pub_key', 'no_padding', 'pkcs1_oaep_padding', 'pkcs1_padding', 'sslv23_padding', 'util']
>>> r = RSA.load_key('/tmp/xxx.pem')
>>> r
<M2Crypto.RSA.RSA instance at 8138190>
>>> dir(r)
['_pyfree', 'this']
>>> r.this
'_815bf00_RSA_p'
>>> 
>>> import sha         
>>> s = sha.sha('the magic words are squeamish ossifrage')
>>> data = s.digest()
>>> data
'\200\276\223\036o<\0232]\347Twu\022j\225\353k\010\371'
>>> sig = r.private_encrypt(data, RSA.pkcs1_padding)
>>> r.public_decrypt(sig, RSA.pkcs1_padding)
'\200\276\223\036o<\0232]\347Twu\022j\225\353k\010\371'
>>> 


-- 
Ng Pheng Siong <ngps at post1.com> * http://www.post1.com/home/ngps




More information about the Python-list mailing list