代码:
来自Crypto.PublicKey的RSA def random_generator():返回Random.new()。 (32) private_key = RSA.generate(1024,random_generator) print(str(private_key))错误消息:
追溯(最近最近通话):文件/home/simon/Python/Projects/FileServer/encrypt.py,第7行在< module> private_key = RSA.generate(1024,random_generator)文件/usr/lib/python3/dist-packages/Crypto/PublicKey/RSA.py,第508行,生成 obj = _RSA.generate_py(bits,rf,progress_func,e)#TODO:不要使用旧的_RSA模块文件/usr/lib/python3/dist-packages/Crypto/PublicKey/_RSA.py,行50,在generate_py p = pubkey.getStrongPrime(bits>>> 1,obj.e,1e-12,randfunc)文件/ usr / lib / python3 / dist-packages / Crypto / Util / number.py,行265,在getStrongPrime randfunc) TypeError:random_generator()取0位置参数,但1被赋予解决方案
从文档:
generate位,randfunc =无,progress_func =无,e = 65537)
参数:
...
- randfunc (可调用) - 随机数生成函数;它应该接受一个整数N,并返回一个随机数字的字节长度为N个字节。如果没有指定,将从Crypto.Random中实例化一个新的。
您的 random_generator()不采取任何参数。它应该采用一个参数 - 返回的次数。此外,实现是坏的 - 您每次都创建一个新的实例,这可能会严重削弱其产生的数字的随机性。
但是,由于您只是使用 Crypto.Random 实例,您根本无需指定此参数 - 只需将其退出。
<$ p $从Crypto.PublicKey导入RSA private_key = RSA.generate(1024) print(str(private_key))
如果您提供自己的randfunc ,请将其传递给绑定读取 方法Crypto.Random 实例:
<$从Crypto导入的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $生成(1024,r.read)打印(str(private_key))
I'm trying to create a random RSA Private key in Python but I'm getting a Error message and I don't know what to do. The code I'm using now is from this Thread I created earlier, but I won't work.
Code:
from Crypto import Random from Crypto.PublicKey import RSA def random_generator(): return Random.new().read(32) private_key = RSA.generate(1024, random_generator) print(str(private_key))Error Message:
Traceback (most recent call last): File "/home/simon/Python/Projects/FileServer/encrypt.py", line 7, in <module> private_key = RSA.generate(1024, random_generator) File "/usr/lib/python3/dist-packages/Crypto/PublicKey/RSA.py", line 508, in generate obj = _RSA.generate_py(bits, rf, progress_func, e) # TODO: Don't use legacy _RSA module File "/usr/lib/python3/dist-packages/Crypto/PublicKey/_RSA.py", line 50, in generate_py p = pubkey.getStrongPrime(bits>>1, obj.e, 1e-12, randfunc) File "/usr/lib/python3/dist-packages/Crypto/Util/number.py", line 265, in getStrongPrime randfunc) TypeError: random_generator() takes 0 positional arguments but 1 was given解决方案
From the documentation:
generate(bits, randfunc=None, progress_func=None, e=65537)
Parameters:
...
- randfunc (callable) - Random number generation function; it should accept a single integer N and return a string of random data N bytes long. If not specified, a new one will be instantiated from Crypto.Random.
Your random_generator() doesn't take any parameters. It is supposed to take one parameter - the number of byes to return. Also the implementation is bad - you're creating a new instance every time which could seriously weaken the randomness of the numbers it generates.
But since you're just using a Crypto.Random instance there's no need for you to specify this parameter at all - just leave it out.
from Crypto.PublicKey import RSA private_key = RSA.generate(1024) print(str(private_key))
If you insist on providing your own randfunc, pass it the bound read method of a Crypto.Random instance:
from Crypto import Random from Crypto.PublicKey import RSA r = Random.new() private_key = RSA.generate(1024, r.read) print(str(private_key))
更多推荐
在Python 3 RSA中创建私钥时出错
发布评论