2012-01-27 70 views
2

Google AppEngine目前使用old version of PyCrypto在AppEngine上打印/導出公鑰PyCrypto

做出一個RSAkey後,我找不到任何方式來導出公鑰。

唉文檔的pycrypto 2.01目前404而.EXPORT方法我在當前的代碼見不上PyCrypto 2.01工作:

使得密鑰對:

rsa_key = RSA.generate(384, random_generator) 

檢查方法可供選擇:

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__   

打印不起作用。

+0

[在谷歌App Engine的驗證數字簽名]的可能重複(http://stackoverflow.com/questions/8516277/verifying-digital-signature-on-google-app-engine) – mikemaccana 2012-01-27 15:18:56

回答

1

應該可以使用pickle模塊,只要互操作性對您不重要。

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) 
+0

採摘給人持久的,但通常出口意味着以PEM或其他格式傳遞給別人。 – mikemaccana 2012-02-02 11:47:28

+0

如果這個「其他人」有一個非常相似的python版本,你可以交換「pickled」鍵對象。正如我在回答中所說的,如果互操作性不重要,泡菜就可以。如果是,您也可以從pycrypto 2.2+複製導出的RSA功能。 – SquareRootOfTwentyThree 2012-02-05 10:56:51

1

只要看看代碼,看看:

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有什麼每個變量的手段一個很好的解釋。