0

I'm currently using the Crypto module to create my private and public key. The standard way is to use RSA.generate(KEY_LENGTH, random_gen). But this uses values created by a random number generator. I would like the key to be customized to a certain extent.

KEY_LENGTH = 1024
random_gen = Random.new().read
privateKey = RSA.generate(KEY_LENGTH, random_gen)
public_Key = keys.publickey()

Problem now is when I try to print random_gen, I get the following output

bound method RNGFile.read of Crypto.Random._UserFriendlyRNG.RNGFile object at 0x75713c50>>

And if i try to put a fixed value in the random_gen spot, I get a TypeError: randfunc must be callable.

1
  • Basically, I don't want a randomized key Commented Jun 23, 2017 at 8:26

2 Answers 2

1

If you take a look at the RSA module documentation, you'll see that it also supports a construct() function that you can use to create RSA keys by supplying your own parameters.

Here's an example:

from Crypto.PublicKey import RSA

n = long('2c1c62f96c8b4a177de41d3df08148eb58bc852edd0fc343faa1de9ada5dccb8e4db708f' \
         'cd71451b4135c7328f1641e79049ab9e88de3d5be28817b898f7ae431a52909fb7c1902e' \
         '694ecda41ff821035a0a45f20871c5a8dbae366189bdd84aac3f0e88541711fe670b4ea2' \
         '2125762dbd39788ebe4ef7fc2d4da5d468f6353c1ab88d2ee5b658bdf195dfd0c7d3fc1f' \
         'bf2511de97d10399259d41d476d38ae18a094ece4ef718d8aef72a33df31737be404cf02' \
         '425db3ee736b279f2be5557b3b4593f02ff8b7853709e791064d0d793063b1ca09747a35' \
         'b2ff98f7c19275224657f879d07b22671a9b37964f45324c9dc6f9de8e65346724b7376f',16)
e = 0x10001L
d = long('b369c2273e964c85c04653a9dacfff073fa5890f5395096335c7ace3abca5924afb4dbe0' \
         '0a7cebd82f23cd52972ab0646b3b3a0f582296ef6434a646ede67ba3f32c5f914296c919' \
         'fcb9c93205d5f670eb895d7553636dd1a1b4b4fb8483e779d19da83e57c87e2782773b1d' \
         'b633e8b9a12c606e37207b05f83e1a801967f10e0c0e1ca7a38feed9d0ab8e1bf3c6fecd' \
         '87cb799afa8140d5437226dd94a9ab835cfeffbfa8c40eb7e13829f995986eeb457072ad' \
         '9dd9539c955bf267dcf4bfd8a9a3423b0f4fb86d1a457490c7df177df339fdfc79c19c06' \
         'fdece56d112660115b485a51737452bb53c4925de7dd111ba72c116aa46e6222ee90661',16)
p = long('6fd004c04efd1b023a50eb9abaf9d701bd4c8294a832bde96426c3de156e6ce861e3abe7' \
         'c712329f86cf0f190f4618a12220f75dcedac64c72c21f76359a4171b7b1ea7012befc8a' \
         'e5e508e8c299a44bf951616109f461fc5372bd3e8e485924837e891180dc646f5028931c' \
         '3f98676ae22d219bf22114acf89d24a7570b',16)
q = long('64fe6f8eda823fadc5335ebb1178973bdabdb7b71299bb05beb5ed631eccc017b8c68a99' \
         '2c958893e019fd4f20b85d0301a1cde4b7f747d0da89780b3027ff5275935f19031965b2' \
         '582cbb5397b309a2357100ae769c50ce5d84abe2a100ac54441251908ac4609bb4342a8f' \
         'c5fced8329b6f0f181aa003dd9ebba6f4fad',16)
u = long('30e5313ae3ce770a951f9c3cc659cb0e79dc2f93b6464ebd5dae0207b38b96e099e383cc' \
         'c48af796ba8587802f8e7bafda432b5e8194841f4535b07f665af110dff573d98a2496c1' \
         '5610b8e78521101ca381ac6cde11c23e60559931db2da08de94205002b0ad1d021e6d9ce' \
         'bd5e3131c76bf57046a289c52ba1ae336bf8',16)

key_params = (n, e, d, p, q, u)
key = RSA.construct(key_params)
print key.exportKey()

Some of the parameters are optional; to create a public key, you only need to specify n and e.

Sign up to request clarification or add additional context in comments.

Comments

0

Random.new().read is a function that generates random numbers.

It is used by the RSA.generate method to randomly create a new private key.

If you really want to "see" the random numbers for some reason (perhaps explain what you're trying to acheive with this) you can wrap it with a function that prints the returned values first:

def create_wrapper_rand(random_gen):
   def wrapper_rand(n):
      values = random_gen(n)
      print values
      return values
   return wrapper_rand

wrapper_rand_gen = create_wrapper_rand(random_gen)

Now pass wrapper_rand_gen to RSA.generate instead of random_gen

3 Comments

Instead if really don't want random values, you can just create a method that returns a fixed string of size n
I'm trying to create a private key based on custom values such as a passphrase etc. In a way removing the random aspect.
@KelvTheKid that would be a very weak crypto. You need a KDF (key derivation function) to create a secret key; generate a random RSA keypair and use the secret key to encrypt private key. When you need the private key back — ask user for passphrase, use the KDF to derive secret key and decrypt private key. And read a good book on cryptography.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.