且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用RSA公钥在Openssl中生成私钥?

更新时间:2023-01-18 09:15:16

解决方案的关键(双关语意味)在提示中:

The key (pun intended) to the solution is in the hint:

可能未像正常的RSA模数那样生成模数.

The modulus might not have been generated like a normal RSA modulus.

这是我采用的方法.

步骤1:推导素数

  1. 我首先获取了您的文件,并将其另存为 public.pem .
  2. 要获取模数( n )和公共指数( e )的值,我运行了:
    openssl rsa -pubin -in pub -text -noout
  3. 我将十六进制模数和指数转换为十进制数,结果为
    n = 1124202659400195453855809312646626918918763763490634139383382395080583005489187313313228228768768569569104529080640890399115524503023755575656009230563411417506875247048447256322965528249863717190044852508574479369625892305046623339906489427598628079277123078923688792 e = 3
  4. 我将该号码插入了在线因子分解计算器中,发现两者之一用于计算模量的素数相对较小:
    p = 55685342628135644993
    q = 20188484192466464768949460945755645151768625616299799562832273933494261171941951733572446448212770737107951345399860187693939287193405193558047495064314130173143967843349121361122537360034973620809175171575355560559559686686561415104860468111655728125125382549549969474
  1. I first took your file and saved it as public.pem.
  2. To get the value of the modulus (n) and the public exponent (e), I ran:
    openssl rsa -pubin -in pub -text -noout
  3. I converted the hexadecimal modulus and exponent to decimal numbers, which yielded
    n=112420265940019545385580931264662691888876377549063413938338239508058300548918731393322848876821656910452908064089039911552450302375557565600923056341141750687524704844725632296552824986371719004485250857447936962589230504662333990648942759862805127715014382377701044586628936249950092121536791020138692688871
    e=3
  4. I plugged that number into an online factorialization calculator to discover that one of the two primes used to calculate the modulus was relatively small:
    p=55685342628135644993
    q=2018848419246646476894946094575564515176862561629979956283227393349426117194195173357244644821277073710795134539986018769393928719340504755806449531413017314396784334912136112253736003497362080917517151753555605597776865614151048604681116557282512513238254935296910445878892354969335089447

第2步:计算其他所需值

  1. 我使用python REPL将 n 的总和计算为 ϕ(n)=(p-1)*(q-1): ϕ(n)= 11242026594001954538356208284541601 119814314544878494231613769464283205926355039999999922722727427437037063463419267762837841655167831571531527627601201231731718255313312614982156040651459582892231514853950574881671713352908778385051168654241320789320-120
  2. 然后,我使用了此答案中的python脚本来计算私有指数( d )和系数( c ),得出:
    d = 749468439600130302557080552302773636079876209696589692821075846309522137284236693333156151730849744913913989461785085085685654234237137145545527517478674972878170170875875076654770693767639729281543432359670992111447556860585225670077770072663243675223065674 c = 1040291110785843997
  1. I calculated the totient of n as ϕ(n) = (p - 1) * (q - 1) using the python REPL:
    ϕ(n)=112420265940019545383562082845416045411981431454487849423161376946428320592635503999973422759627461737095663419267762837841655167835571546831529127621801245931718255313312614982156040651459582892231514853950574881671713352908778385051165894248654079110333265820418532073390681314653181675602213322541221954432
  2. I then used the python script from this answer to calculate the private exponent (d) and the coefficient (c), which yielded:
    d=74946843960013030255708055230277363607987620969658566282107584630952213728423669333315615173084974491397108946178508558561103445223714364554352751747867497287812170208875076654770693767639721928154343235967049921114475568605852256700777262832436052740222177213612354715593787543102121117068142215027481302955
    c=1040291110785843997

第3步:创建私钥的ASN.1结构

然后,我使用计算出的值在名为 ASN.1 结构.此答案中所述的代码> asn :

I then used the calculated values to create an ASN.1 structure in a file named asn as described in this answer:

asn1=SEQUENCE:rsa_key

[rsa_key]
version=INTEGER:0
modulus=INTEGER:112420265940019545385580931264662691888876377549063413938338239508058300548918731393322848876821656910452908064089039911552450302375557565600923056341141750687524704844725632296552824986371719004485250857447936962589230504662333990648942759862805127715014382377701044586628936249950092121536791020138692688871
pubExp=INTEGER:3
privExp=INTEGER:74946843960013030255708055230277363607987620969658566282107584630952213728423669333315615173084974491397108946178508558561103445223714364554352751747867497287812170208875076654770693767639721928154343235967049921114475568605852256700777262832436052740222177213612354715593787543102121117068142215027481302955
p=INTEGER:55685342628135644993
q=INTEGER:2018848419246646476894946094575564515176862561629979956283227393349426117194195173357244644821277073710795134539986018769393928719340504755806449531413017314396784334912136112253736003497362080917517151753555605597776865614151048604681116557282512513238254935296910445878892354969335089447
e1=INTEGER:37123561752090429995
e2=INTEGER:903312890059631
coeff=INTEGER:1040291110785843997

第4步:创建私钥

基于ASN.1结构,我生成了如下私钥:

Based on the ASN.1 structure, I generated the private key as follows:

  1. 以DER格式创建私钥:
    openssl asn1parse -genconf asn -out private.der
  2. 将私钥转换为PEM格式:
    openssl rsa -in private.der -inform der -out private.pem -outform pem

这将导致创建一个具有以下内容的 private.pem 文件:

This results in a private.pem file being created with the following contents:

-----BEGIN RSA PRIVATE KEY-----
MIIBsAIBAAKBgQCgF35rHhOWi9+r4n9xM/ejvMEsQ8h6lams962k4U0WSdfySUev
hyI1bd3FRIb5fFqSBt6qPTiiiIw0KXte5dANB6lPe6HdUPTA/U4xHWi2FB/BfAyP
sOlUBfFp6dtkEEcEKt+Z8KTJYJEerRie24y+nsfZMnLBst6tsEBfx/U75wIBAwKB
gGq6VEdpYmRdHGzsbmP7vDiYe2zYHLwQ0AKnPKNErq6KQyQC5eEngbgT4WpWl+J2
Xn+R9m0vwNbaiDam0uD3p5192BaN2tdaW5P5JjfGa95ytRBCQ/cr+z03FjG9C6zQ
QZG5eyOoMloHAfnYiJMV5SZarfTiF9BGFvtcfrjhbterAgkDBMoUFjHxL0ECeDUI
f9nbOl1O2AgI/51gfHGo/NKv+kcQenM8RO7dy9+hUAulwqMlyszSq+0GdZdgQL/i
Lz8NclSgyuUtptmaSWtjB5Tdc8boaBApGKac7vB4M1AfTkng1+SplKbkdFlCVg4n
6EvCOrUFFsLp308JSbkv2240Q93JJwIJAgMxYrl2oMorAgcDNY7r7ttvAggOb9tA
6WMDHQ==
-----END RSA PRIVATE KEY-----

第5步:验证结果

要检查创建的私钥(在 private.pem 中)是否与提供的公钥匹配,我刚刚从私钥中生成了一个新的公钥:

To check whether the created private key (in private.pem) matches the provided public key, I just generated a new public key from the private key:

openssl rsa -in private.pem -pubout

这将产生以下输出:

writing RSA key
-----BEGIN PUBLIC KEY-----
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCgF35rHhOWi9+r4n9xM/ejvMEs
Q8h6lams962k4U0WSdfySUevhyI1bd3FRIb5fFqSBt6qPTiiiIw0KXte5dANB6lP
e6HdUPTA/U4xHWi2FB/BfAyPsOlUBfFp6dtkEEcEKt+Z8KTJYJEerRie24y+nsfZ
MnLBst6tsEBfx/U75wIBAw==
-----END PUBLIC KEY-----

此输出与您提供的公钥完全匹配.

This output exactly matches the public key that you provided.