【问题标题】:How to use -pubin with openssl (from libressl)?如何将 -pubin 与 openssl(来自 libressl)一起使用?
【发布时间】:2020-12-25 02:58:16
【问题描述】:

https://www.keycdn.com/blog/openssl-tutorial

上面页面中的以下文字对我来说没有意义。

If that file doesn't also include the private key, you must indicate so using -pubin

前面的文字应该是指私钥而不是公钥。

The <key.pem> is the file containing the public key. 

以下命令是我想出来的。

openssl genrsa -out key.pem 1024
echo 'Hello World!' > input.txt
openssl pkeyutl -encrypt -in input.txt -inkey key.pem -out output.txt
openssl pkeyutl -decrypt -in output.txt -inkey key.pem -out output_decypt.txt

谁能告诉我一些关于如何使用 -pubin 的工作示例?谢谢。

$ openssl version -a
LibreSSL 3.2.3
built on: date not available
platform: information not available
options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx) 
compiler: information not available
OPENSSLDIR: "/usr/local/etc/libressl"

【问题讨论】:

    标签: openssl homebrew libressl


    【解决方案1】:

    Meta:这不是一个编程问题或问题,尽管过去在 openssl 命令行上有过一些问题,但社区在过去几年中对话题性变得更加严格。这两种方式我都没有强烈的感觉,但如果共识是关闭,我会删除它。

    OpenSSL(及其分支 LibreSSL,从现在开始应该被认为包含在我所有的参考资料中)支持两个“私钥”文件(实际上包含一个密钥 pair -- 私有 public,并且必须保持私有)和仅包含公钥的“公钥”文件(因此可以公开)。 pkeyutl(以及旧版rsautl)支持这两个证书文件以及(X.509v3)证书文件;证书包含一个公钥,但与公钥不同,格式也不相同。

    OpenSSL 支持的私钥文件实际上有几种变体;只要您使用 OpenSSL,它们之间的区别就无关紧要,但当您想要与其他软件交互或互操作时,它们之间的区别可能会有所不同。几乎所有执行 X.509 风格的非对称加密的软件都特别支持证书文件(PEM 和 DER)。 (不包括做 PGP、SSH、Signal 等的事情。)对单独的公钥文件的支持不太常见,虽然很多事情都支持 some 类型的私钥文件,但它并不总是与 OpenSSL 的一种类型相同。

    所有这三种文件类型都可以是 PEM 格式或“DER”格式。 (从技术上讲,这两种情况下的数据都是 ASN.1-DER 编码的,但是“DER”文件是 just DER,而 PEM 文件是 PEM 包装——base64 带有换行符和标题/尾行 - - 围绕DER。)私钥文件另外可以加密(使用密码)或不加密;公钥和证书文件从不加密。

    解密和签名需要私钥,因此仅限于密钥“所有者”。加密和验证只需要公钥,一些系统总是使用证书,但是OpenSSL有更多的选择。

    openssl genrsa 2048 >private.pem 
    # writes a private key, by default in PEM, but you can specify -outform DER
    # if you add a cipher option like -aes128 or -des3 it is encrypted, else not
    # or
    openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 >private.pem
    # ditto
    # PS: 1024-bit RSA, although not actually broken yet, since 2014 
    # is not considered to provide adequate safety for most purposes
    
    openssl rsa <private.pem -pubout >public.pem 
    # writes a public key file, again by default in PEM
    # or
    openssl pkey <private.pem -pubout >public.pem 
    
    openssl req -new -x509 -key private.pem -subj "/C=XX/ST=Utopia/O=Chaotic/CN=ReallyME" >cert.pem
    # creates a self-signed certificate _containing_ (and signed by) this keypair 
    # with specified name, and defaults for other parameters;
    # there are lots more options, see the man page or many existing Qs.
    # Self-signed cert is mostly useful for test and debug, but not trusted in production; 
    # for a real cert you need to apply to a suitable Certificate Authority 
    # which is more complicated, and much more variegated, than I can fit here.
    
    openssl pkeyutl -encrypt <data -inkey private.pem >encrypted
    openssl pkeyutl -encrypt <data -pubin -inkey public.pem >encrypted
    openssl pkeyutl -encrypt <data -certin -inkey cert.pem >encrypted
    openssl pkeyutl -decrypt <encrypted -inkey private.pem >decrypted 
    
    openssl sha256 <data -sign private.pem >sig
    # this form supports only private key file
    openssl sha256 <data -verify public.pem -signature sig
    openssl sha256 <data -prverify private.pem -signature sig 
    # and this form supports only key files but not cert
    
    openssl sha256 <data -binary | pkeyutl -sign -inkey private.pem -pkeyopt digest:sha256 >sig
    openssl sha256 <data -binary | pkeyutl -verify -inkey private.pem -pkeyopt digest:sha256 -sigfile sig 
    openssl sha256 <data -binary | pkeyutl -verify -pubin -inkey public.pem -pkeyopt digest:sha256 -sigfile sig 
    openssl sha256 <data -binary | pkeyutl -verify -certin -inkey cert.pem -pkeyopt digest:sha256 -sigfile sig 
    
    # end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      相关资源
      最近更新 更多