I am using the python Cryptography module and I have generated private and public keys using examples from the documentation. The problem I am having is that I want to be able to generate different key pairs. Like I generate one but it is always the same one being generated every time I run it. I've tried reading through the documentation as much as I cam but cannot find any kind of solution to this problem. Thanks.
-
Check where is the set up for seeding. Make sure you seed the algorithm always differentlySteephen– Steephen2017-12-09 00:35:24 +00:00Commented Dec 9, 2017 at 0:35
-
I hate to ask if it might just be a simple answer, but how would you go about doing that. That seems to be the problem I'm having, I've been searching through the documentation but can't find it.orosmatthew– orosmatthew2017-12-09 00:45:44 +00:00Commented Dec 9, 2017 at 0:45
-
Refer your documentation and check for function 'seed' or something similar to that. Usually pass current time to this function.Steephen– Steephen2017-12-09 00:46:51 +00:00Commented Dec 9, 2017 at 0:46
-
What specific module are you using? Can you provide a link to the documentation and post your code?DoesData– DoesData2017-12-09 00:50:13 +00:00Commented Dec 9, 2017 at 0:50
-
Im using the 'Cryptography' module. website is cryptography.io, my code is pretty much identical to the rsa generation example that they giveorosmatthew– orosmatthew2017-12-09 00:56:20 +00:00Commented Dec 9, 2017 at 0:56
Add a comment
|
1 Answer
I actually did that a while ago, so i'll just copy/paste my solution :)
import os
from Cryptodome.PublicKey import RSA
from Cryptodome import Random
def generate_keypair(bits=2048):
random_generator = Random.new().read
rsa_key = RSA.generate(bits, random_generator)
return rsa_key.exportKey(), rsa_key.publickey().exportKey()
To genarate a random key, you have to include a random generator in RSA.generate.
A little more human readable:
from Cryptodome.PublicKey import RSA
from Cryptodome import Random
def generate_keypair(bits=2048):
random_generator = Random.new().read
rsa_key = RSA.generate(bits, random_generator)
print(repr(rsa_key))
print(repr(rsa_key.publickey()))
generate_keypair()
4 Comments
orosmatthew
I copied and pasted exactly what you typed and I keep getting the same keys :/
orosmatthew
Oh, im sorry, I was just not looking close enough, i just looked at the first few characters and didnt see it changes later on. Thanks!
Pat
Oh yeah, the
.exportKey() makes it a little more cryptic and hard to read. But i came in handy for my programDaksh
I copied the exact code and I waited for like 5 minutes after running the program but there is not output on the terminal