首页 文章

使用RSA私钥生成公钥?

提问于
浏览
319

我真的不明白这个:

根据:http://www.madboa.com/geek/openssl/#key-rsa,您可以从私钥生成公钥 .

openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub

我最初的想法是,它们是一起生成的 . RSA私钥是否包含总和?还是公钥?

8 回答

  • 15

    在这个代码中首先我们创建RSA密钥,它是私有的,但它有一对公钥,所以为了得到你的实际公钥我们只是这样做

    openssl rsa -in mykey.pem -pubout > mykey.pub
    

    希望你能得到更多信息check this

  • 8

    寻找SSH公钥的人......

    如果您要提取公钥以用于OpenSSH,则需要以不同方式获取公钥

    $ ssh-keygen -y -f mykey.pem > mykey.pub
    

    此公钥格式与OpenSSH兼容 . 将公钥附加到 remote:~/.ssh/authorized_keys ,你会很高兴


    来自 SSH-KEYGEN(1) 的文档

    ssh-keygen -y [-f input_keyfile]
    -y此选项将读取私有OpenSSH格式文件并将OpenSSH公钥打印到stdout .

  • 0

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

    A.1.2 RSA私钥语法RSA私钥应使用ASN.1类型表示RSAPrivateKey:RSAPrivateKey :: = SEQUENCE {
    版本版本,
    模数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)
    系数INTEGER, - (q的倒数)mod p
    otherPrimeInfos OtherPrimeInfos OPTIONAL
    }

    如您所见,此格式包含许多字段,包括模数和公共指数,因此是RSA public key中信息的严格超集 .

  • 61

    有些人认为公钥不存储在PEM文件中 . 私钥文件中存在以下DER结构:

    openssl rsa -text -in mykey.pem

    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
    }
    

    所以有足够的数据来计算公钥(模数和公共指数),这就是 openssl rsa -in mykey.pem -pubout 所做的

  • 3

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

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

    要使用RSA公钥加密某些内容,您可以将明文视为数字并将其提高到e模数n的幂:

    ciphertext = ( plaintext^e ) mod n
    

    要使用RSA私钥解密某些内容,您可以将密文视为一个数字并将其提升到d模数n的幂:

    plaintext = ( ciphertext^d ) mod n
    

    要使用openssl生成私有(d,n)密钥,您可以使用以下命令:

    openssl genrsa -out private.pem 1024
    

    要使用openssl从私钥生成公共(e,n)密钥,您可以使用以下命令:

    openssl rsa -in private.pem -out public.pem -pubout
    

    要剖析上面openssl命令生成的private.pem私有RSA密钥的内容,请运行以下命令(输出截断为标签):

    openssl rsa -in private.pem -text -noout | less
    
    modulus         - n
    privateExponent - d
    publicExponent  - e
    prime1          - p
    prime2          - q
    exponent1       - d mod (p-1)
    exponent2       - d mod (q-1)
    coefficient     - (q^-1) mod p
    

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

    是的,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
    

    作为替代(第二)表示,PKCS#1 v1.5推荐RSA私钥的这种结构 . PKCS#1 v2.0标准完全排除了替代表示中的e和d指数 . PKCS#1 v2.1v2.2通过可选地包括更多与CRT相关的组件,建议对替代表示的进一步改变 .

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

    openssl rsa -in public.pem -text -pubin -noout
    
    Modulus             - n
    Exponent (public)   - e
    

    这里没有惊喜 . 正如所承诺的那样,它只是(n,e)对 .

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

  • 455

    首先快速回顾一下RSA密钥生成 .

    • 随机选择两个适当大小的随机可能素数(p和q) .

    • 将两个素数相乘以产生模数(n) .

    • 选择一个公共指数(e) .

    • 用素数和公共指数做一些数学运算来产生私有指数(d) .

    公钥由模数和公共指数组成 .

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

    然而:

    • 实用私钥格式几乎总是存储多于n和d .

    • e通常不是随机挑选的,而是使用少数几个众所周知的值之一 . 如果e是众所周知的 Value 之一,你知道那么通过反复试验很容易找出e .

    因此,在大多数实际的RSA实现中,您可以从私钥获取公钥 . 有可能 Build 一个基于RSA的密码系统,这是不可能的,但它不是完成的事情 .

  • 250
    openssl genrsa -out mykey.pem 1024
    

    实际上会产生一个公钥 - 私钥对 . 该对存储在生成的 mykey.pem 文件中 .

    openssl rsa -in mykey.pem -pubout > mykey.pub
    

    将提取公钥并将其打印出来 . Here是指向更好地描述此内容的页面的链接 .

    编辑:检查示例部分here . 要输出私钥的公共部分:

    openssl rsa -in key.pem -pubout -out pubkey.pem
    
  • 20
    Use the following commands:
    
    1. 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.
    
    2. If you check there will be a file created by the name : mycert.pem
    
    3. openssl rsa -in mycert.pem -pubout > mykey.txt
    writing RSA key
    
    4. If you check the same file location a new public key : mykey.txt will be created.
    

相关问题