在Python 3 RSA中创建私钥时出错

编程入门 行业动态 更新时间:2024-10-22 21:17:37
本文介绍了在Python 3 RSA中创建私钥时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在Python中创建一个随机的RSA私钥,但是我收到一条错误消息,我不知道该怎么做。 我现在使用的代码来自这个我之前创建的线程,但我不会工作。

代码:

来自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中创建私钥时出错

本文发布于:2023-11-10 00:52:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   RSA   私钥时

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!