-1

我是 python 和密码学的新手。我想从变量生成 PrivateKey。在寻找了这么多选项之后,我才知道我只能从Random函数生成密钥对。

这是我的变量

a = [[[3, 1, 85, 33, 0, 0, 255, 254, 255, 254, 255, 254, 255, 254, 255, 6, 248, 0, 240, 0, 224, 0, 192, 0, 192, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 181, 11, 254, 92, 186, 205, 94, 27, 192, 36, 126, 26, 164, 13, 159, 114, 59, 162, 95, 50, 189, 78, 95, 99, 24, 140, 124, 72, 165, 204, 156, 114, 40, 99, 28, 57, 25, 141, 122, 107, 39, 226, 122, 24, 169, 35, 186, 93, 39, 162, 242, 24, 185, 229, 120, 38, 169, 205, 57, 31, 184, 141, 153, 78, 152, 12, 86, 92, 152, 13, 22, 77, 155, 226, 182, 66, 163, 163, 16, 89, 157, 227, 180, 67, 160, 204, 142, 101, 165, 226, 244, 45, 167, 99, 20, 117, 157, 76, 44, 38, 181, 225, 44, 107, 32, 75, 179, 110, 32, 163, 16, 38, 165, 101, 48, 32, 43, 14, 76, 38, 50, 204, 205, 50, 163, 227, 172, 38, 46, 98, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [3, 1, 101, 34, 0, 0, 255, 254, 255, 14, 248, 2, 240, 2, 224, 0, 192, 0, 192, 0, 128, 0, 128, 0, 128, 0, 128, 0, 192, 0, 192, 0, 224, 0, 224, 0, 240, 0, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 17, 229, 30, 78, 19, 140, 30, 99, 21, 226, 190, 113, 22, 97, 190, 30, 152, 36, 94, 115, 162, 75, 222, 21, 35, 228, 126, 119, 168, 97, 222, 99, 40, 140, 222, 32, 52, 14, 222, 108, 53, 204, 94, 67, 55, 78, 222, 76, 193, 100, 62, 57, 172, 142, 31, 92, 181, 77, 191, 47, 55, 101, 31, 119, 184, 75, 63, 75, 11, 22, 92, 108, 15, 227, 156, 44, 157, 34, 28, 25, 158, 13, 220, 32, 33, 228, 92, 35, 165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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]

所以我的问题是,

  1. 是否可以从序列/数据生成 PrivateKey
  2. 如果是这样,任何关于我该如何继续的建议
  3. 最后但并非最不重要的一点是,我应该使用哪种算法(RSA/ECC)
4

1 回答 1

0
  1. 不,不可能仅从序列/数据生成私钥。密钥是使用使用系统熵种子的随机数生成器 (RNG) 或伪随机数生成器 (PRNG) 生成的,这使得攻击者更难猜测。

以 RSA 为例,正如您所提到的,您可以从随机函数创建私钥,也可以通过手动设置变量来更好地控制私钥的生成。在这里,您可以找到使用“PyCryptodome”包的“Crypto.PublicKey.RSA.construct()”函数手动决定的一些变量生成私钥的方法。为此,您应该对 RSA 有一个很好的了解。

  1. 可以从对称密码学中的用户输入生成密钥。'Scrypt''Argon2'是两个可以帮助您实现它的软件包。输入必须首先转换为二进制。输入可以是字符串或数字。这是一个使用“Scrypt”包的简单示例。您可以在此处找到更多详细信息。
import scrypt, secrets

password = b'not a number'
salt = secrets.token_bytes(32)

scrypt_key = scrypt.hash(password, salt, N=16384, r=8, p=1, 32)

print('Salt: ', salt)
print('Key: ', scrypt_key)
Output:

Salt:  b'\xdbS\x1e\xa2\x81e\xd3\x948p\xc3lmk\xd6\x8b\xb94\x1c\xd5A/\xa5gZ\xb1\xc2\x15\x99\x9d\xc8\xb8'

Key:  b'J\xc1\xc1"\xfd\x05\xfb\x14J\x96\xea\xe3\x1d\xa6\xbb\x01\xf7sj\x87\xf9\x18%\x00YK\x1f\xe8\xc8\x8d\xff%'

  1. 该算法取决于您的应用程序。虽然,现在 ECC 加密被认为比 RSA 加密更安全。
于 2019-03-07T19:55:50.220 回答