Google 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__
打印不起作用。
如果互操作性对您来说不那么重要,那么应该可以使用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)
只需查看代码即可看到:
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('pn')
obj.p=pubkey.getPrime(bits/2, randfunc)
if progress_func: progress_func('qn')
obj.q=pubkey.getPrime((bits/2)+difference, randfunc)
obj.n=obj.p*obj.q
# Generate encryption exponent
if progress_func: progress_func('en')
obj.e=pubkey.getPrime(17, randfunc)
if progress_func: progress_func('dn')
obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))
return obj
这个网站对每个变量的含义都有很好的解释。