mikemaccana
mikemaccana

Reputation: 123470

Printing/exporting a public key on AppEngine PyCrypto

Google AppEngine currently uses an old version of PyCrypto.

After making an RSAkey, I can't find any way to export the publickey.

Alas docs for pycrypto 2.01 currently 404. And the .export methods I see in current code don't work on PyCrypto 2.01:

Making the keypair:

rsa_key = RSA.generate(384, random_generator) 

Checking methods available:

In [84]: rsa_key.publickey. <tab>
RSAkey.publickey.__call__          RSAkey.publickey.__func__          RSAkey.publickey.__reduce__        RSAkey.publickey.__str__
RSAkey.publickey.__class__         RSAkey.publickey.__get__           RSAkey.publickey.__reduce_ex__     RSAkey.publickey.__subclasshook__
RSAkey.publickey.__cmp__           RSAkey.publickey.__getattribute__  RSAkey.publickey.__repr__          RSAkey.publickey.im_class
RSAkey.publickey.__delattr__       RSAkey.publickey.__hash__          RSAkey.publickey.__self__          RSAkey.publickey.im_func
RSAkey.publickey.__doc__           RSAkey.publickey.__init__          RSAkey.publickey.__setattr__       RSAkey.publickey.im_self
RSAkey.publickey.__format__        RSAkey.publickey.__new__           RSAkey.publickey.__sizeof__        

Printing doesn't work.

Upvotes: 2

Views: 540

Answers (2)

speedplane
speedplane

Reputation: 16141

Just take a look at the code to see:

def generate(bits, randfunc, progress_func=None):
    """generate(bits:int, randfunc:callable, progress_func:callable)

    Generate an RSA key of length 'bits', using 'randfunc' to get
    random data and 'progress_func', if present, to display
    the progress of the key generation.
    """
    obj=RSAobj()
    # Generate random number from 0 to 7
    difference=ord(randfunc(1)) & 7

    # Generate the prime factors of n
    if progress_func: progress_func('p\n')
    obj.p=pubkey.getPrime(bits/2, randfunc)
    if progress_func: progress_func('q\n')
    obj.q=pubkey.getPrime((bits/2)+difference, randfunc)
    obj.n=obj.p*obj.q

    # Generate encryption exponent
    if progress_func: progress_func('e\n')
    obj.e=pubkey.getPrime(17, randfunc)
    if progress_func: progress_func('d\n')
    obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))
    return obj

This site has a good explanation of what each variable means.

Upvotes: 1

SquareRootOfTwentyThree
SquareRootOfTwentyThree

Reputation: 7786

It should be possible to use the pickle module, provided interoperability is not that important to you.

import pickle

keyout = pickle.dumps(rsa_key)
# Save keyout into a file or a db
[ ... ]
# Retrieve keyin from the same file or db
rsa_key = pickle.loads(keyin)

Upvotes: 1

Related Questions