openssl经常使用用法

OpenSSL主要有三个组件构成:html

  • openssl: 多用途命令行工具
  • libcrypto: 加密算法库
  • libssl: 加密模块应用库,实现了ssl及tls

openssl能够实现: 对称和非对称加密,密钥证书管理git

openssl 全部命令:算法

Standard commands
asn1parse         ca                ciphers           cms               
crl               crl2pkcs7         dgst              dh                
dhparam           dsa               dsaparam          ec                
ecparam           enc               engine            errstr            
gendh             gendsa            genpkey           genrsa            
nseq              ocsp              passwd            pkcs12            
pkcs7             pkcs8             pkey              pkeyparam         
pkeyutl           prime             rand              req               
rsa               rsautl            s_client          s_server          
s_time            sess_id           smime             speed             
spkac             srp               ts                verify            
version           x509              

Message Digest commands (see the `dgst' command for more details)
md4               md5               rmd160            sha               
sha1              

Cipher commands (see the `enc' command for more details)
aes-128-cbc       aes-128-ecb       aes-192-cbc       aes-192-ecb       
aes-256-cbc       aes-256-ecb       base64            bf                
bf-cbc            bf-cfb            bf-ecb            bf-ofb            
camellia-128-cbc  camellia-128-ecb  camellia-192-cbc  camellia-192-ecb  
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc          
cast5-cbc         cast5-cfb         cast5-ecb         cast5-ofb         
des               des-cbc           des-cfb           des-ecb           
des-ede           des-ede-cbc       des-ede-cfb       des-ede-ofb       
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb      
des-ofb           des3              desx              rc2               
rc2-40-cbc        rc2-64-cbc        rc2-cbc           rc2-cfb           
rc2-ecb           rc2-ofb           rc4               rc4-40            
seed              seed-cbc          seed-cfb          seed-ecb          
seed-ofb

命令格式: openssl command [command_opts][command_args]shell

对称加密(enc)

openssl enc  -Cipher -[e/d] [-a] [-salt] [-in filename] [-out filename]

-Cipher:加密算法 -e: 加密操做 -d: 解密操做 -a: 使用base64位编码 -salt: 自动加入随机数做为文件内容加密,默认数据库

加密ubuntu

openssl enc -des3 -e -a -in ./in -out ./out
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:

解密centos

openssl enc -des3 -d -a -in ./out -out ./in1
enter des-ede3-cbc decryption password:

OpenSSL一共提供了8种对称加密算法,其中7种是分组加密算法,仅有的一种流加密算法是RC4。这7种分组加密算法分别是AES、DES、Blowfish、CAST、IDEA、RC二、RC5,都支持电子密码本模式(ECB)、加密分组连接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)四种经常使用的分组密码加密模式。其中,AES使用的加密反馈模式(CFB)和输出反馈模式(OFB)分组长度是128位,其它算法使用的则是64位。事实上,DES算法里面不单单是经常使用的DES算法,还支持三个密钥和两个密钥3DES算法。安全

信息摘要(dgst)

openssl dgst -Cipher inputfile [-out file]

获取文件md5dom

openssl dgst -md5 ./in
MD5(./in)= 2b1761bf6d399bff4c60e69da7f02d81

OpenSSL实现了5种信息摘要算法,分别是MD二、MD五、MDC二、SHA(SHA1)和RIPEMD。SHA算法事实上包括了SHA和SHA1两种信息摘要算法,此外,OpenSSL还实现了DSS标准中规定的两种信息摘要算法DSS和DSS1。ide

生成密码(passwd)

openssl passwd [options] [passwords]

where options are -crypt standard Unix password algorithm (default) -1 MD5-based password algorithm -apr1 MD5-based password algorithm, Apache variant -salt string use provided salt (随机加盐,盐值同样,获得的密码hash也同样) -in file read passwords from file -stdin read passwords from stdin -noverify never verify when reading password from terminal -quiet no warnings -table format output as table -reverse switch table columns

openssl passwd -1 -in ./in
$1$2xV8Igpw$LD01hvcMb9ThVaX4KdPko0

passwd的主要做用是用来计算密码hash的,目的是为了防止密码以明文的形式出现。

生成随机数(rand)

Usage: rand [options] num
where options are
-out file             - write to file
-engine e             - use engine e, possibly a hardware device.
-rand file:file:... - seed PRNG from files
-base64               - base64 encode output
-hex                  - hex encode output
openssl rand  10
��eO]�.� �
openssl rand  -hex 10
d692dfc3564addf698c2

openssl rand 生成指定长度的随机字符,若是不指定显示编码会看到不少乱码,因此须要指定可视化编码base64或hex

生成密钥对(genrsa)

首先须要先使用 genrsa 标准命令生成私钥,而后再使用 rsa 标准命令从私钥中提取公钥。

usage: genrsa [args] [numbits]
 -des            encrypt the generated key with DES in cbc mode
 -des3           encrypt the generated key with DES in ede cbc mode (168 bit key)
 -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
 -out file       output the key to 'file
 -passout arg    output file pass phrase source
 -f4             use F4 (0x10001) for the E value
 -3              use 3 for the E value
 -engine e       use engine e, possibly a hardware device.
 -rand file:file:...
                 load the file (or the files in the directory) into
                 the random number generator

使用genrsa生成私钥

openssl genrsa -out ./private 1024
Generating RSA private key, 1024 bit long modulus
......++++++
.........++++++
e is 65537 (0x10001)

能够经过指定加密算法-[des/des3]等对输出的私钥进行加密

openssl genrsa -out private1 -des 1024 Generating RSA private key, 1024 bit long modulus ...........................++++++ .........++++++ e is 65537 (0x10001) Enter pass phrase for private1: Verifying - Enter pass phrase for private1: openssl rsa -in private1 -out public1 -pubout Enter pass phrase for private1: writing RSA key

使用rsa提取公钥

openssl rsa -in ./private -out public -pubout
writing RSA key

私钥加密和公钥解密(rsautl

公钥加密

openssl rsautl -in ./data -out ./data_enc -inkey ./public -pubin -encrypt

私钥解密

openssl rsautl -in ./data_enc -out ./data_dec -inkey ./private -decrypt

rsautl本指令可以使用RSA算法签名,验证身份,加密/解密数据。

私有CA

咱们的目的就是要获取数字证书,数字证书内包含了拥有证书者的姓名、地址、电子邮件账号、公钥、证书有效期、发放证书的CA、CA的数字签名等信息。证书主要有三大功能:加密、签名、身份验证。用户能够经过想CA中心申请也能够自建私有的CA。不管那种咱们都须要先生成证书签署请求

生成证书签署请求

  1. 生成私钥
openssl genrsa -out ./private.key 1024
  1. 生成证书签署请求(Certificate Signing Request (CSR))
openssl req -new -key ./private.key -out ./request.csr
  1. 将生成证书请求发送给ca主机
  2. 在ca主机上签署生成证书
openssl ca -in ./request.csr -out ./cer.crt -days 365
Using configuration from /usr/lib/ssl/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jan  4 15:29:45 2017 GMT
            Not After : Jan  4 15:29:45 2018 GMT
        Subject:
            countryName               = zh
            stateOrProvinceName       = sc
            organizationName          = zj
            organizationalUnitName    = it
            commonName                = suntopo@163.com
            emailAddress              = suntopo@163.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                FA:0B:97:C5:89:47:45:95:92:65:A7:17:4E:D0:50:DD:E7:98:DC:1F
            X509v3 Authority Key Identifier: 
                keyid:56:12:F7:01:CA:F8:D8:EB:AB:80:3B:4A:B3:9C:1C:61:EE:24:4D:D7
Certificate is to be certified until Jan  4 15:29:45 2018 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
  1. 将生成的证书发送到目标主机上就可使用了

构建私有CA

  1. 为建立CA提供所需的目录及文件

ubuntu下openssl目录在/usr/lib/ssl(软连的/etc/ssl),centos默认在/etc/pki/,默认已经存在了openssl.cnf文件,须要建立目录,因为文件中使用的dir=./demoCA,须要修改为绝对目录

mkdir -pv /usr/lib/ssl/demoCA/{certs,crl,newcerts,private}
touch /usr/lib/demoCA/{serial,index,index.txt}
  1. 指明证书开始编号
echo 01 >> serial
  1. 生成私钥(注意目录和名字须要和配置文件中一致)
openssl genrsa -out /usr/lib/ssl/demoCA/private/cakey.pem 1024
  1. 生成自签名证书
openssl req -new -x509 -in /usr/lib/ssl/demoCA/private/cakey.pem -out /usr/lib/ssl/demoCA/cacert.pem -days 3650

-new:表示生成一个新证书签署请求

-x509:专用于CA生成自签证书,若是不是自签证书则不须要此项

-key:生成请求时用到的私钥文件

-out:证书的保存路径

-days:证书的有效期限,单位是day(天),默认是365天

密钥和证书管理是PKI的一个重要组成部分,OpenSSL为之提供了丰富的功能,支持多种标准。 首先,OpenSSL实现了ASN.1的证书和密钥相关标准,提供了对证书、公钥、私钥、证书请求以及CRL等数据对象的DER、PEM和BASE64的编解码功能。OpenSSL提供了产生各类公开密钥对和对称密钥的方法、函数和应用程序,同时提供了对公钥和私钥的DER编解码功能。并实现了私钥的PKCS#12和PKCS#8的编解码功能。OpenSSL在标准中提供了对私钥的加密保护功能,使得密钥能够安全地进行存储和分发。 在此基础上,OpenSSL实现了对证书的X.509标准编解码、PKCS#12格式的编解码以及PKCS#7的编解码功能。并提供了一种文本数据库,支持证书的管理功能,包括证书密钥产生、请求产生、证书签发、吊销和验证等功能。 事实上,OpenSSL提供的CA应用程序就是一个小型的证书管理中心(CA),实现了证书签发的整个流程和证书管理的大部分机制。

troubles && errors

  1. wrong number of fields on line 1 (looking for field 6, got 1, '' left)

在建立私有CA的第二步中,在serial写入了01,清空serial就能够了

  1. CA certificate and CA private key do not match

在经过req -new -x509建立自签名证书的时候,提示用户输入密码,而后使用ca签署的时候报上面的错误,下面是openssl.cnf关于req配置

[ req ]
default_bits		= 2048
default_md		= sha1
default_keyfile 	= privkey.pem
distinguished_name	= req_distinguished_name
attributes		= req_attributes
x509_extensions	= v3_ca	# The extentions to add to the self signed cert

对自签名文件使用sha1加密,可是按理说应该没问题,可是报错了,索性把这一行注释了,而后就OK了

相关文章
相关标签/搜索