openssl在官網(wǎng)中介紹如下
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(安全傳輸層協(xié)議)和ssl(安全套接層)提供健壯的怀吻、商用級別的猪瞬、全功能的工具集的開放項目阳似,它也是一個一般用途的加密工具庫。
如對123進(jìn)行加密
#md5加密
echo -n 123 | openssl md5
#sha1加密
echo -n 123 | openssl sha1
注意:這里不要少了-n宛官,簡單來說避免對回車進(jìn)行加密
,具體參見我的另外一篇博客http://www.reibang.com/p/e856d6b8b9f9。
這里的md5和sha1 可以為
sha, sha1, mdc2, ripemd160, sha224, sha256, sha384, sha512, md4, md5, blake2b, blake2s
官網(wǎng)文檔:
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
我在目錄中創(chuàng)建一個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