0

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"
4

1 に答える 1

0

メタ: これはプログラミングの質問でも問題でもありません。openssl コマンドラインに関する過去の質問はありますが、コミュニティはここ数年で話題性についてより厳しくなっています。どちらも強く感じませんが、コンセンサスがまとまる場合は削除します。

OpenSSL (およびそのフォークである LibreSSL は、今後のすべての参照に含まれていると見なされる必要があります) は、「秘密鍵」ファイル (実際には秘密鍵と公開鍵のペアが含まれており、秘密にしておく必要があります) と「公開鍵」の両方をサポートしています。 ' 公開鍵のみを含む (したがって公開できる) ファイル。(およびレガシーも)これらの両方と(X.509v3)証明書ファイルもサポートします。証明書には公開鍵が含まれていますが、公開鍵と同じではなく、同じ形式でもありません。pkeyutlrsautl

実際には、OpenSSL でサポートされている秘密鍵ファイルにはいくつかのバリエーションがあります。それらの違いは、OpenSSL を使用している限り問題ではありませんが、他のソフトウェアとのインターフェイスや相互運用を行う場合には問題になる可能性があります。特に証明書ファイル (PEM と DER の両方) は、X.509 スタイルの非対称暗号化を行うほぼすべてのソフトウェアでサポートされています。(これは、PGP、SSH、Signal などを実行するものを除外します。) 個別の公開鍵ファイルのサポートはあまり一般的ではなく、多くのものがある種の秘密鍵ファイルをサポートしいますが、OpenSSL の種類の 1 つと常に同じであるとは限りません。

これら 3 つのファイル タイプはすべて、PEM 形式または「DER」形式にすることができます。(技術的には、データはどちらの場合も ASN.1-DER でエンコードされますが、「DER」ファイルは単なる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
于 2020-12-25T06:34:15.837 に答える