使用OpenSSL生成非對稱密鑰
非對稱加密算法也叫<公開密鑰算法>,不同于對稱加密算法,非對稱加密算法加密密鑰與解密密鑰不是一把密鑰、非對稱密鑰對中公鑰是可以公開的。
這種算法如果使用公鑰進行加密,則隻能使用相應的私鑰解密;如使用私鑰加密,也隻能使用相應的公鑰解密。而要想通過公鑰推導出私鑰幾乎是不可能的(這樣的算法可以使用一些不可逆函數實作)!
OpenSSL支援RSA、DSA、DH三種不同的非對稱加密算法。下面我們将介紹如何使用OpenSSL生成并學習使用RSA密鑰對。
RSA非對稱加密:
一、生成RSA密鑰對(opensll genrsa):
1.文法說明:
usage: genrsa [args] [numbits]
-des encrypt the generated key with DES in cbc mode(使用des加密算法加密RSA密鑰對)
-des3 encrypt the generated key with DES in ede cbc mode (168 bit key) (使用des3加密算法加密RSA密鑰對)
-aes128, -aes192, -aes256
encrypt PEM output with cbc aes (使用aes加密算法加密RSA密鑰對)
-out file output the key to 'file(輸出密鑰對檔案)
-passout arg output file pass phrase source (輸出密鑰檔案的密碼)
-f4 use F4 (0x10001) for the E value (使用F4作為公鑰的E參數,預設為該選項)
-3 use 3 for the E value (使用3作為公鑰的E參數)
-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
2.案例:
(1)openssl genrsa -out rsakey.pem 2048
生成一個2048位的密鑰對,注:沒有對密鑰進行加密操作。
(2)openssl genrsa -aes128 -out rsakey.pem -passout pass:111111 2048
生成一份2048位的密鑰對,并對密鑰對使用aes算法加密,密鑰密碼為111111
說明:openssl生成的公鑰與私鑰預設被存放在一個檔案中,當需要提取公鑰或私鑰時需要使用rsa指令,她可以用來管理密鑰檔案。
二、管理密鑰檔案
rsa [options] <infile >outfile
where options are
-inform arg input format - one of DER NET PEM (輸入的密鑰編碼格式,可以是DER、NET、PEM中的一個,預設為PEM)
-outform arg output format - one of DER NET PEM (輸出的密鑰編碼格式)
-in arg input file (輸入的密鑰檔案名稱)
-sgckey Use IIS SGC key format (使用SGC格式,一般很少用到)
-passin arg input file pass phrase source (輸入密鑰檔案時提供的密鑰檔案密碼)
-out arg output file (輸出的密鑰檔案名)
-passout arg output file pass phrase source (輸出密鑰檔案時的密碼)
-des encrypt PEM output with cbc des (使用des算法對密鑰進行加密)
-des3 encrypt PEM output with ede cbc des using 168 bit key (使用des3算法對密鑰進行加密)
encrypt PEM output with cbc aes (使用aes算法對密鑰進行加密)
-text print the key in text (明文輸出密鑰參數)
-noout don't print key out (不輸出密鑰到檔案)
-modulus print the RSA key modulus (輸出RSA密鑰模值)
-check verify key consistency
-pubin expect a public key in input file (預設輸入的是密鑰對的私鑰,使用pubin輸入的是密鑰的公鑰)
-pubout output a public key (輸出公鑰到檔案)
-engine e use engine e, possibly a hardware device. (使用第三方裝置)
openssl rsa -in <filename> -out <filename> -pubout -des3
輸入一個密鑰檔案,提起其中的公鑰檔案,并使用des3算法加密公鑰檔案。
(注:filename為密鑰檔案名,實際使用時使用自己的檔案名替換,而且實際應用中不會對公鑰進行加密,這裡僅為示範案例)
三、使用密鑰
Usage: rsautl [options]
-in file input file (輸入準備要進行加密或解密的檔案)
-out file output file (輸出加密或解密後的檔案)
-inkey file input key (輸入密鑰檔案)
-keyform arg private key format - default PEM (指定密鑰檔案格式)
-pubin input is an RSA public (說明輸入的是公鑰)
-certin input is a certificate carrying an RSA public key (指明輸入的是一個證書)
-ssl use SSL v2 padding (資料補齊方式,預設是pkcs)
-raw use no padding (資料補齊方式,預設是pkcs)
-pkcs use PKCS#1 v1.5 padding (default) (資料補齊方式,預設是pkcs)
-oaep use PKCS#1 OAEP (資料補齊方式,預設是pkcs)
-sign sign with private key (進行數字簽名,即使有私鑰加密)
-verify verify with public key (進行數字簽名驗證,即使有公鑰解密)
-encrypt encrypt with public key (進行解密操作)
-decrypt decrypt with private key (進行解密操作)
-hexdump hex dump output (輸出十六進制格式)
-engine e use engine e, possibly a hardware device.
-passin arg pass phrase source (密碼參數)
注:使用RSA進行加密操作時,要求被加密的資料長度與RSA密鑰長度一緻,但資料大小是不定的,是以當檔案資料小于RSA密鑰長度時會采用資料補齊,當被加密的檔案大于密鑰長度時,會被先切割為等長大小的資料塊,進行加密操作。
1.openssl rsautl -in install.log -out bak.log inkey rsakey.pem -pubin -encrypt
使用我們上面案例中生成的rsakey.pem密鑰對<install.log>這個檔案進行加密,因為使用了pubin選項,是以使用的是公鑰加密。
2.openssl rsautl -in bak.log -out install.log -inkey rsakey.pem -decrypt
使用rsakey.pem密鑰中的私鑰對<bak.log>檔案進行解密。
注:實際應用中很少會使用非對稱加密算法對大的檔案進行加密操作,而是使用對稱加密算法加密檔案,然後再使用<非對稱加密算法>對<對稱算法的密鑰>進行加密。
本文轉自丁丁曆險51CTO部落格,原文連結:http://blog.51cto.com/manual/788681 ,如需轉載請自行聯系原作者