前言
来了解一下openssl这个工具的基础命令的使用。
主要部分是根据https://wiki.openssl.org/index.php/Command_Line_Utilities这个文档来描述的。
openssl命令
命令格式
openssl command [ command_options ] [ command_arguments ]
素数生成
// 生成24位的一个素数
openssl prime -generate -bits 24
获取帮助
openssl help
openssl command -help
openssl genpkey -help
Usage: genpkey [options]
Valid options are:
-help Display this summary
-out outfile Output file
-outform PEM|DER output format (DER or PEM)
-pass val Output file pass phrase source
-paramfile infile Parameters file
-algorithm val The public key algorithm
-pkeyopt val Set the public key algorithm option as opt:value
-genparam Generate parameters, not key
-text Print the in text
-* Cipher to use to encrypt the key
-engine val Use engine, possibly a hardware device
Order of options may be important! See the documentation.
版本信息
root@keep-VirtualBox:/usr/local/lib# openssl version
GmSSL 2.5.4 - OpenSSL 1.1.0d 19 Jun 2019 # 这里显示GmSSL是因为机子上面安装了GmSSL, 默认会替换openssl程序。 GmSSL是openssl的超集,额外实现了国密算法
root@keep-VirtualBox:/usr/local/lib#
root@keep-VirtualBox:/usr/local/lib# openssl version -help
Usage: version [options]
Valid options are:
-help Display this summary
-a Show all data
-b Show build date
-d Show configuration directory
-e Show engines directory
-f Show compiler flags used
-o Show some internal datatype options
-p Show target build platform
-v Show library version
root@keep-VirtualBox:/usr/local/lib#
生成RSA 私钥
#生成私钥并且使用aes256加密
openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem
#列出可用于加密的算法
openssl list -cipher-algorithms
#读取私钥并且以文本形式显示
openssl pkey -in private-key.pem -text
#指定对称密钥 -pass pass:123456, 就可以不会手动输入了
openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private-key.pem -pass pass:123456
生成公钥
#根据私钥生成公钥
openssl pkey -in private-key.pem -out public-key.pem -pubout
#读取公钥并且以文本形式显示
openssl pkey -in public-key.pem -pubin -text
基于椭圆曲线生成key
分为两步: 1、生成参数 2、使用参数生成key
#获取支持的椭圆曲线列表
openssl ecparam -list_curves
#生成椭圆曲线参数输出到文件中
openssl ecparam -name prime256v1 -out prime256v1.pem
#读取参数文件并以文本显示
openssl ecparam -in prime256v1.pem -noout -text
#读取参数文件并以C语言格式显示
openssl ecparam -in prime256v1.pem -noout -C
#根据参数文件生成私钥文件
openssl genpkey -aes256 -paramfile prime256v1.pem -out private-key.pem
#使用genpkey基于EC直接生成私钥, 效果同上
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256
#使用genpkey直接生成私钥 并使用aes256加密
openssl genpkey -aes256 -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private-key.pem
Base64编码字符串
#编码
openssl base64 -e <<< 'Welcome to openssl wiki'
#解码
openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'
生成文件摘要
$ openssl dgst -md5 primes.dat
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664
$ openssl dgst -sha1 primes.dat
SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32
$ openssl dgst -sha384 primes.dat
SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271
#列举支持的摘要算法
openssl list -digest-algorithms
#列举支持的摘要命令
openssl list -digest-commands
$ openssl md5 primes.dat
MD5(primes.dat)= 7710839bb87d2c4c15a86c2b2c805664
$ openssl sha1 primes.dat
SHA1(primes.dat)= 5dfab70ce825591689f4a3f65910870a9022cd32
$ openssl sha384 primes.dat
SHA384(primes.dat)= 41399bdffe6850f5a44852d967f3db415654f20dc2eb6cd231772f6ea411876d85d44091ebbc6b1f4ce8673e64617271
文件加解密
#获取支持加解密的算法 命令1
openssl enc -ciphers
#获取支持加解密的算法 命令2
openssl list -cipher-algorithms
选项 -e, -d 分别表示加密和解密 选项 -iter 表示密钥派生的轮数,越大算法强度就越强 -pbkdf2 一起用。-salt随机生成因子作用于派生函数。
#使用aes-256-cbc 加密文件,密码 123456
openssl enc -aes-256-cbc -e -iter 1000 -salt -in primes.dat -out primes.enc -k 123456
#解密
openssl enc -aes-256-cbc -d -iter 1000 -in primes.enc -out primes.dec
openssl enc -aes128 -pass pass:123456 -iter 10 -d -in aes128.enc -out aes128.dec
#Des3算法:
#加密:
openssl enc -e -des3 -a -salt -in fstab -out fstab.des3
#解密:
openssl enc -d -des3 -a -salt -in fstab.des3 -out fstab
摘要、签名与验证
openssl dgst -md5 -hex file.txt
#使用rsa生成的私钥加密后 再计算签名
openssl dgst -sha256 -sign privatekey.pem -out signature.sign file.txt
#验证签名
openssl dgst -sha256 -verify publickey.pem -signature signature.sign file.txt
其他命令
使用openssl help 可以看到分组列出的命令。
也可以使用man openssl 查看帮助。
GmSSL的编译
需要一些国密算法的学习,必须要用到GmSSL库了。
git clone https://github.com/guanzhi/openssl.git
cd openssl
./config no-shared no-asm # 使用静态编译,避免openssl与系统原有的openssl相覆盖
make -j 12
make install
# no-asm 解决 EC_GFp_sm2z256_method undefined的问题
更多
今年新工作刚开始学习信息安全技术,一堆的术语算法,刚上来就整懵了我。每天花了一些时间,循序渐进地学习,慢慢的上道了,活到老学到老,不然要被淘汰掉了。一万小时定律不是吹的,功夫花在哪里,成就就在哪里。
这个文章简要整理了一些openssl常用的基础命令,后面便于翻查。
五一的时候新入手了一个27寸的dell显示屏,屏幕大看起来就是爽哈!工欲善其事必先利其器, done!
行动,才不会被动!
欢迎关注个人公众号 微信 -> 搜索 -> fishmwei,沟通交流。
博客地址: fishmwei.gitee.io/ 掘金主页: juejin.cn/user/208432…