创建自定义RSA密钥

时间:2017-06-23 08:24:09

标签: python encryption rsa

我目前正在使用加密模块来创建我的私钥和公钥。标准方法是使用RSA.generate(KEY_LENGTH,random_gen)。但是这使用随机数生成器创建的值。我想在一定程度上定制钥匙。

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

现在问题是当我尝试打印random_gen时,我得到以下输出

  

绑定方法RNGFile.read of Crypto.Random._UserFriendlyRNG.RNGFile   对象位于0x75713c50>>

如果我尝试在random_gen中放置一个固定值,我会得到一个TypeError:randfunc必须是可调用的。

2 个答案:

答案 0 :(得分:1)

如果您查看RSA module documentation,您会发现它还支持construct()功能,您可以通过提供自己的参数来创建RSA密钥。

以下是一个例子:

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()

有些参数是可选的;要创建公钥,您只需指定ne

答案 1 :(得分:0)

Random.new()。read是一个生成随机数的函数。

RSA.generate方法使用它来随机创建新的私钥。

如果你真的想出于某种原因“看到”随机数(也许可以解释一下你想要实现的内容),你可以用一个首先打印返回值的函数来包装它:

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)

现在将wrapper_rand_gen传递给RSA.generate而不是random_gen