如何将自定义 RSA 密钥对添加到 .pem 文件
How to add a custom RSA key pair to a .pem file
我在 python 中创建了一个自定义 RSA 密钥对,仅用于测试目的。我想将私钥和 public 密钥添加到 .pem 文件,但我在研究中没有找到任何东西。我发现的只是人们从图书馆生成 RSA 密钥对。
我有 public 密钥 [e, n] 和私钥 [d, n] 的 e、d 和 n 变量。
大多数主要的加密库都支持这一点,例如PyCryptodome(通过 construct()
and exportKey()
) or Cryptography (as described in the Numbers and Key Serialization 部分),例如
PyCryptodome:
from Crypto.PublicKey import RSA
n = int("b83b...529b", 16);
d = int("4eea...a721", 16);
e = int("010001", 16);
privateKey = RSA.construct((n, e, d))
privateKeyPem = privateKey.exportKey(pkcs=8) # export in PKCS#8 format
publicKey = RSA.construct((n, e))
publicKeyPem = publicKey.exportKey() # export in X.509/SPKI format
print(privateKeyPem.decode('utf8'))
print(publicKeyPem.decode('utf8'))
或密码学:
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
n = int("b83b...529b", 16);
d = int("4eea...a721", 16);
e = int("010001", 16);
(q, p) = rsa.rsa_recover_prime_factors(n, e, d)
dmq1 = rsa.rsa_crt_dmq1(d, q)
dmp1 = rsa.rsa_crt_dmp1(d, p)
iqmp = rsa.rsa_crt_iqmp(p, q)
publicNumbers = rsa.RSAPublicNumbers(e, n)
privateNumbers = rsa.RSAPrivateNumbers(p, q, d, dmp1, dmq1, iqmp, publicNumbers)
privateKey = privateNumbers.private_key();
publicKey = publicNumbers.public_key();
privateKeyPem = privateKey.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
publicKeyPem = publicKey.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(privateKeyPem.decode('utf8'))
print(publicKeyPem.decode('utf8'))
请注意,原始密钥在 p 和 q 中是对称的,因此交换 p 和 q 会更改 PEM 或 DER 编码密钥,但不会更改原始密钥 (n, e, d)。
我在 python 中创建了一个自定义 RSA 密钥对,仅用于测试目的。我想将私钥和 public 密钥添加到 .pem 文件,但我在研究中没有找到任何东西。我发现的只是人们从图书馆生成 RSA 密钥对。 我有 public 密钥 [e, n] 和私钥 [d, n] 的 e、d 和 n 变量。
大多数主要的加密库都支持这一点,例如PyCryptodome(通过 construct()
and exportKey()
) or Cryptography (as described in the Numbers and Key Serialization 部分),例如
PyCryptodome:
from Crypto.PublicKey import RSA
n = int("b83b...529b", 16);
d = int("4eea...a721", 16);
e = int("010001", 16);
privateKey = RSA.construct((n, e, d))
privateKeyPem = privateKey.exportKey(pkcs=8) # export in PKCS#8 format
publicKey = RSA.construct((n, e))
publicKeyPem = publicKey.exportKey() # export in X.509/SPKI format
print(privateKeyPem.decode('utf8'))
print(publicKeyPem.decode('utf8'))
或密码学:
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
n = int("b83b...529b", 16);
d = int("4eea...a721", 16);
e = int("010001", 16);
(q, p) = rsa.rsa_recover_prime_factors(n, e, d)
dmq1 = rsa.rsa_crt_dmq1(d, q)
dmp1 = rsa.rsa_crt_dmp1(d, p)
iqmp = rsa.rsa_crt_iqmp(p, q)
publicNumbers = rsa.RSAPublicNumbers(e, n)
privateNumbers = rsa.RSAPrivateNumbers(p, q, d, dmp1, dmq1, iqmp, publicNumbers)
privateKey = privateNumbers.private_key();
publicKey = publicNumbers.public_key();
privateKeyPem = privateKey.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
publicKeyPem = publicKey.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(privateKeyPem.decode('utf8'))
print(publicKeyPem.decode('utf8'))
请注意,原始密钥在 p 和 q 中是对称的,因此交换 p 和 q 会更改 PEM 或 DER 编码密钥,但不会更改原始密钥 (n, e, d)。