Openssl 是否使用RSA私钥生成公钥?

Openssl 是否使用RSA私钥生成公钥?,openssl,rsa,public-key-encryption,Openssl,Rsa,Public Key Encryption,我真的不明白这一点: 根据:,您可以从私钥生成公钥 openssl genrsa -out mykey.pem 1024 openssl rsa -in mykey.pem -pubout > mykey.pub 我最初的想法是,它们是成对产生的。 RSA私钥是否包含总和?还是公钥 openssl genrsa -out mykey.pem 1024 将实际生成一个公钥-私钥对。该对存储在生成的mykey.pem文件中 openssl rsa -in mykey.pem -pubout

我真的不明白这一点:

根据:,您可以从私钥生成公钥

openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub
我最初的想法是,它们是成对产生的。 RSA私钥是否包含总和?还是公钥

openssl genrsa -out mykey.pem 1024
将实际生成一个公钥-私钥对。该对存储在生成的
mykey.pem
文件中

openssl rsa -in mykey.pem -pubout > mykey.pub
将提取公钥并打印出来。是指向一个页面的链接,可以更好地描述这一点

编辑:检查示例部分。要仅输出私钥的公共部分,请执行以下操作:

openssl rsa -in key.pem -pubout -out pubkey.pem
要获取用于SSH目的的可用公钥,请使用:


在大多数生成RSA私钥的软件(包括openssl)中,私钥表示为对象或其某些变体:

A.1.2 RSA私钥语法

RSA私钥应使用ASN.1类型表示
RSAPrivateKey:

  RSAPrivateKey ::= SEQUENCE {
      version           Version,
      modulus           INTEGER,  -- n
      publicExponent    INTEGER,  -- e
      privateExponent   INTEGER,  -- d
      prime1            INTEGER,  -- p
      prime2            INTEGER,  -- q
      exponent1         INTEGER,  -- d mod (p-1)
      exponent2         INTEGER,  -- d mod (q-1)
      coefficient       INTEGER,  -- (inverse of q) mod p
      otherPrimeInfos   OtherPrimeInfos OPTIONAL
  }

如您所见,这种格式有许多字段,包括模数和公共指数,因此是一个严格的信息超集。

在这段代码中,我们首先创建RSA密钥,它是私有的,但它也有一对公钥,所以要获得实际的公钥,我们只需这样做

openssl rsa -in mykey.pem -pubout > mykey.pub
希望您能获得更多信息

寻找SSH公钥的人。。。 如果您希望提取公钥以与OpenSSH一起使用,则需要以稍微不同的方式获取公钥

$ ssh-keygen -y -f mykey.pem > mykey.pub
此公钥格式与OpenSSH兼容。将公钥附加到
remote:~/.ssh/authorized_keys
,您就可以开始了


来自
SSH-KEYGEN(1)

-y此选项将读取私有OpenSSH格式文件,并将OpenSSH公钥打印到stdout


公钥并不像有些人认为的那样存储在PEM文件中。私钥文件上存在以下DER结构:

mykey.pem中的openssl rsa-text


因此有足够的数据来计算公钥(模数和公钥指数),这就是
openssl rsa-in mykey.pem-pubout
使用以下命令所做的:

  • openssl-req-x509-nodes-days 365-sha256-newkey rsa:2048-keyout mycert.pem-out mycert.pem

     Loading 'screen' into random state - done
     Generating a 2048 bit RSA private key
     .............+++
     ..................................................................................................................................................................+++
     writing new private key to 'mycert.pem'
     -----
     You are about to be asked to enter information that will be incorporated
     into your certificate request.
     What you are about to enter is what is called a Distinguished Name or a DN.
     There are quite a few fields but you can leave some blank
     For some fields there will be a default value,
     If you enter '.', the field will be left blank.
    
  • 如果选中,将创建一个名为:
    mycert.pem

     Loading 'screen' into random state - done
     Generating a 2048 bit RSA private key
     .............+++
     ..................................................................................................................................................................+++
     writing new private key to 'mycert.pem'
     -----
     You are about to be asked to enter information that will be incorporated
     into your certificate request.
     What you are about to enter is what is called a Distinguished Name or a DN.
     There are quite a few fields but you can leave some blank
     For some fields there will be a default value,
     If you enter '.', the field will be left blank.
    
  • openssl rsa-in mycert.pem-pubout>mykey.txt

    writing RSA key
    
  • 如果检查相同的文件位置,则会创建一个新的公钥
    mykey.txt


  • 我下面的答案有点长,但希望它能提供一些以前答案中缺少的细节。我将从一些相关的陈述开始,最后回答最初的问题

    要使用RSA算法加密某些内容,需要模数和加密(公共)指数对(n,e)。那是你的公钥。要使用RSA算法解密某些内容,需要模和解密(私有)指数对(n,d)。那是你的私钥

    openssl genrsa -out mykey.pem 1024
    openssl rsa -in mykey.pem -pubout > mykey.pub
    
    要使用RSA公钥加密某些内容,请将明文视为一个数字,并将其提高到e模n的幂:

    ciphertext = ( plaintext^e ) mod n
    
    plaintext = ( ciphertext^d ) mod n
    
    要使用RSA私钥解密某些内容,请将密文视为一个数字,并将其提高到d模n的幂:

    ciphertext = ( plaintext^e ) mod n
    
    plaintext = ( ciphertext^d ) mod n
    
    要使用openssl生成私有(d,n)密钥,可以使用以下命令:

    openssl genrsa -out private.pem 1024
    
    openssl rsa -in private.pem -out public.pem -pubout
    
    要使用openssl从私钥生成公钥(e,n),可以使用以下命令:

    openssl genrsa -out private.pem 1024
    
    openssl rsa -in private.pem -out public.pem -pubout
    
    要解析上面的openssl命令生成的private.pem private RSA密钥的内容,请运行以下命令(此处输出截断为标签):

    私钥不应该只包含(n,d)对吗?为什么有6个额外的组件?它包含e(公共指数),因此可以从private.pem私有RSA密钥生成/提取/派生公共RSA密钥。其余5个组件用于加速解密过程。事实证明,通过预计算和存储这5个值,可以将RSA解密速度提高4倍。解密将在没有这5个组件的情况下工作,但如果你手头有它们,它可以做得更快。加速算法是基于

    是的,private.pem RSA私钥实际上包含所有这8个值;在运行上一个命令时,不会动态生成这些命令。尝试运行以下命令并比较输出:

    # Convert the key from PEM to DER (binary) format
    openssl rsa -in private.pem -outform der -out private.der
    
    # Print private.der private key contents as binary stream
    xxd -p private.der
    
    # Now compare the output of the above command with output 
    # of the earlier openssl command that outputs private key
    # components. If you stare at both outputs long enough
    # you should be able to confirm that all components are
    # indeed lurking somewhere in the binary stream
    openssl rsa -in private.pem -text -noout | less
    
    RSA私钥的这种结构被推荐作为替代(第二)表示形式。标准将e和d指数从替代表示中完全排除。并提出对替代表示法的进一步修改,可选地包括更多与CRT相关的组件

    要查看public.pem公钥RSA密钥的内容,请运行以下命令(此处输出截断为标签):

    这一点也不奇怪。它只是(n,e)对,就像承诺的那样


    现在终于回答了最初的问题:如上所示,使用openssl生成的RSA私钥包含公钥和私钥以及其他一些组件。当您从私钥生成/提取/派生公钥时,openssl会将其中两个组件(e,n)复制到一个单独的文件中,该文件将成为您的公钥。

    首先简要回顾一下RSA密钥的生成

  • 随机选取两个大小合适的随机可能素数(p和q)
  • 将两个素数相乘得到模数(n)
  • 选择一个公共指数(e)
  • 对素数和公共指数进行一些数学运算,以生成私有指数(d)
  • 公钥由模数和公钥指数组成

    最小私钥由模数和私钥指数组成。从已知的模和私有指数到相应的公共指数,没有计算上可行的可靠方法

    然而:

  • 实用的私钥格式几乎总是存储n和d以上的密钥
  • e通常不是pick
    openssl rsa -in mykey.pem -pubout > mykey.pub
    
    openssl rsa -in mykey.pem -pubout -out mykey.pub
    
    openssl genpkey -algorithm ed25519 -out pvt.pem
    
    openssl pkey -in pvt.pem -pubout > public.pem
    
    openssl ec -in ecprivkey.pem -pubout -out ecpubkey.pem
    
    openssl pkey -in key.pem -des3 -out keyout.pem
    
    openssl ec -aes-128-cbc -in pk8file.pem -out tradfile.pem