首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用-pubin和openssl (来自libressl)?

如何使用-pubin和openssl (来自libressl)?
EN

Stack Overflow用户
提问于 2020-12-25 10:58:17
回答 1查看 115关注 0票数 0

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

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

代码语言:javascript
复制
If that file doesn't also include the private key, you must indicate so using -pubin

前面的文本应该是私钥,而不是公钥。

代码语言:javascript
复制
The <key.pem> is the file containing the public key. 

下面的命令是我想出来的。

代码语言:javascript
复制
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的工作示例?谢谢。

代码语言:javascript
复制
$ 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"
EN

回答 1

Stack Overflow用户

发布于 2020-12-25 14:34:16

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

OpenSSL (以及它的分支LibreSSL,从现在开始应该被认为包含在我的所有参考中)既支持“私钥”文件(实际上包含一个密钥对--私钥和公钥,并且必须是私有的),也支持只包含公钥的“公钥”文件(因此可以设置为公钥)。pkeyutl (以及传统的rsautl)既支持这两种文件,也支持(X.509v3)证书文件;证书包含公钥,但与公钥不同,格式也不同。

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

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

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65444880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档