openssl.RSA命令
作者:三味书屋 来源: 2022/6/21 17:15:09

1、生成私钥

genrsa
功能:
用于生成RSA私钥,不会生成公钥,因为公钥提取自私钥
使用参数:
openssl genrsa [-out filename] [-passout arg] [-des] [-des3] [-idea] [numbits]

选项说明:
-out filename     :将生成的私钥保存至filename文件,若未指定输出文件,则为标准输出。

-numbits          :指定要生成的私钥的长度,默认为1024。该项必须为命令行的最后一项参数。

-des|-des3|-idea:指定加密私钥文件用的算法,这样每次使用私钥文件都将输入密码,太麻烦所以很少使用。

-passout args    :加密私钥文件时,传递密码的格式,如果要加密私钥文件时单未指定该项,则提示输入密码。传递密码的args的格式见一下格式。
  pass:password   :password表示传递的明文密码
  env:var         :从环境变量var获取密码值
  file:filename   :filename文件中的第一行为要传递的密码。若filename同时传递给"-passin"和"-passout"选项,则filename的第一行为"-passin"的值,第二行为"-passout"的值
  stdin           :从标准输入中获取要传递的密码
2、提取公钥
openssl rsa [-inform PEM|NET|DER] [-outform PEM|NET|DER] [-in filename] [-passin arg] [-out filename] [-passout arg] [-sgckey] [-des] [-des3] [-idea] [-text] [-noout] [-modulus] [-check] [-pubin] [-pubout] [-RSAPublicKey_in] [-RSAPublicKey_out] [-engine id]
       
rsa [options] <infile >outfile
where options are
 -inform arg     输入文件编码格式,只有pem和der两种
 -outform arg    输出文件编码格式,只有pem和der两种
 -in arg         input file 指明私钥文件的存放路径
 -sgckey         Use IIS SGC key format
 -passin arg     如果输入文件被对称加密过,需要指定输入文件的密码
 -out arg        output file 指明将公钥的保存路径
 -passout arg    如果输出文件也需要被对称加密,需要指定输出文件的密码
 -des            对输出结果采用对称加密 des算法
 -des3          对输出结果采用对称加密 des3算法
 -seed           encrypt PEM output with cbc seed
 -aes128, -aes192, -aes256
                 encrypt PEM output with cbc aes
 -camellia128, -camellia192, -camellia256
                 encrypt PEM output with cbc camellia
  以上几个都是对称加密算法的指定,生成私钥的时候一般会用到
 -text           print the key in text 以明文形式输出各个参数值
 -noout          don't print key out 不输出密钥到任何文件
 -modulus        输出模数值
 -check          检查输入密钥的正确性和一致性
 -pubin          指定输入文件是公钥
 -pubout         指定输出文件是公钥
 -engine e      指定三方加密库或者硬件
3、利用公钥加密、私钥解密数据
openssl rsautl [-in file] [-out file] [-inkey file] [-pubin] [-certin] [-sign] [-verify] [-encrypt] [-decrypt] [-pkcs] [-ssl] [-raw] [-hexdump] [-asn1parse]
       
openssl rsautl -h
Usage: rsautl [options]
-in file        input file 输入文件
-out file       output file 输出文件
-inkey file     input key 指定私有密钥文件,格式是RSA私有密钥文件
-keyform arg    private key format - default PEM 指定私钥格式
-pubin          input is an RSA public 指定输入的是RSA公钥
-certin         input is a certificate carrying an RSA public key 指定输入的是证书文件
-ssl            使用SSLv2的填充方式
-raw            不进行填充
-pkcs           使用V1.5的填充方式(默认)
-oaep           使用OAEP的填充方式
-sign           使用私钥做签名
-verify         使用公钥认证签名
-encrypt        使用公钥加密
-decrypt        使用私钥解密
-hexdump        以16进制打印
-engine e       指定三方库或者硬件设备
-passin arg    pass phrase source 传递密码来源

例子:

生成RSA密钥,采用des3对称加密私钥

PS C:\Program Files\OpenSSL-Win64\bin> ./openssl genrsa -des3 -passout pass:123456 -out RSA.pem

提取公钥
PS C:\Program Files\OpenSSL-Win64\bin> ./openssl rsa -in RSA.pem -passin pass:123456 -pubout -out pub.pem


不使用密码生成RSA密钥,生成的公钥和私钥可以直接复制使用
PS C:\Program Files\OpenSSL-Win64\bin> ./openssl genrsa -out RSA.pem

提取公钥
PS C:\Program Files\OpenSSL-Win64\bin> ./openssl rsa -in RSA.pem -pubout -out pub.pem


SSL3.0使用pkeyutl替代rsautl

使用RSA作为密钥进行加密,实际上使用其中的公钥进行加密

PS C:\Program Files\OpenSSL-Win64\bin> ./openssl pkeyutl -encrypt -in test.txt -inkey RSA.pem -out enc.txt

使用RSA作为密钥进行解密,实际上使用其中的私钥进行解密
PS C:\Program Files\OpenSSL-Win64\bin> ./openssl pkeyutl -decrypt -in enc.txt -inkey RSA.pem -out enc1.txt


使用公钥加密
PS C:\Program Files\OpenSSL-Win64\bin> ./openssl pkeyutl -encrypt -in test.txt -inkey pub.pem -pubin -out enc.txt

使用私钥解密
PS C:\Program Files\OpenSSL-Win64\bin> ./openssl pkeyutl -decrypt -in enc.txt -inkey RSA.pem -out enc1.txt
注意:若公钥和私钥是被加密过的,需要加上密码:-passin pass:123456

称      呼:
联系方式:
您的评论:
技术支持:l.w.dong@qq.com www.luweidong.cn
广州市   wx:lwdred
Copyright © 2014 三味书屋 All Rights Reserved
技术支持:l.w.dong@qq.com  sitemap xml  sitemap html

粤公网安备44010602011869号

粤ICP备13031080号-1