openssl的简单使用

openssl在官网中介绍如下

OpenSSL is an open source project that provides a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library.
openssl 是一个为tls(安全传输层协议)和ssl(安全套接层)提供健壮的、商用级别的、全功能的工具集的开放项目,它也是一个一般用途的加密工具库。

如对123进行加密

#md5加密
echo -n 123 | openssl md5
#sha1加密
echo -n 123 | openssl sha1

注意:这里不要少了-n,简单来说避免对回车进行加密
,具体参见我的另外一篇博客//www.greatytc.com/p/e856d6b8b9f9

这里的md5和sha1 可以为
sha, sha1, mdc2, ripemd160, sha224, sha256, sha384, sha512, md4, md5, blake2b, blake2s
官网文档:
https://www.openssl.org/docs/man1.1.0/apps/md5.html

rsa加密

生成一个密钥:
openssl genrsa -out test.key 1024
这里-out指定生成文件的。需要注意的是这个文件包含了公钥和密钥两部分,也就是说这个文件即可用来加密也可以用来解密。后面的1024是生成密钥的长度。

openssl可以将这个文件中的公钥提取出来:

openssl rsa -in test.key -pubout -out test_pub.key

我在目录中创建一个hello的文本文件,然后利用此前生成的公钥加密文件:

openssl rsautl -encrypt -in hello -inkey test_pub.key -pubin -out hello.en
-in指定要加密的文件,-inkey指定密钥,-pubin表明是用纯公钥文件加密,-out为加密后的文件。

解密文件:

openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de
-in指定被加密的文件,-inkey指定私钥文件,-out为解密后的文件。

至此,一次加密解密的过程告终。在实际使用中还可能包括证书。

参考文章:http://www.cnblogs.com/aLittleBitCool/archive/2011/09/22/2185418.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容