openssl rsautl和openssl pkeyutl(文件的非對稱加密)

openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.htmlhtml


rsautl是rsa的工具,至關於rsa、dgst的部分功能集合,可用於生成數字簽名、驗證數字簽名、加密和解密文件工具

pkeyutl是非對稱加密的通用工具,大致上和rsautl的用法差很少,因此此處只解釋rsautl。加密

openssl rsautl [-in file] [-out file] [-inkey file] [-pubin] [-certin] [-passin arg] [-sign] [-verify] [-encrypt] [-decrypt] [-hexdump]

openssl pkeyutl [-in file] [-out file] [-sigfile file] [-inkey file] [-passin arg] [-pubin] [-certin] [-sign] [-verify] [-encrypt] [-decrypt] [-hexdump]

共同的選項說明:
-in file:指定輸入文件
-out file:指定輸出文件
-inkey file:指定密鑰輸入文件,默認是私鑰文件,指定了"-pubin"則表示爲公鑰文件,使用"-certin"則表示爲包含公鑰的證書文件
-pubin:指定"-inkey file"的file是公鑰文件
-certin:使用該選項時,表示"-inkey file"的file是包含公鑰的證書文件
-passin arg:傳遞解密密碼。若驗證簽名時實用的公鑰或私鑰文件是被加密過的,則須要傳遞密碼來解密。密碼的格式見"openssl 密碼格式"
【功能選項:】
-sign:簽名並輸出簽名結果,注意,該選項須要提供RSA私鑰文件
-verify:使用驗證簽名文件
-encrypt:使用公鑰加密文件
-decrypt:使用私鑰解密文件
【輸出格式選項:】
-hexdump:以hex方式輸出

 
openssl pkeyutl選項說明:
sigfile file:待驗證的簽名文件

rsautl命令的用法和rsa、dgst不太同樣。首先,它的前提是已經有非對稱密鑰,全部的命令操做都用到公鑰或私鑰來處理;再者,該命令使用-in選項來指定輸入文件,而不像dgst同樣能夠把輸入文件放在命令的結尾;最後,該命令使用的密鑰文件、簽名文件、證書文件都經過-inkey選項指定,再經過各功能的選項搭配來實現對應的功能。spa

注意rsautl和pkeyutl的缺陷是默認只能對短小的文件進行操做,不然將報相似以下的錯誤信息。code

140341340976968:error:0406C06E:rsa routines:RSA_padding_add_PKCS1_type_1:data too large for key size:rsa_pk1.c:73:

由於這兩個工具簽名和驗證簽名的功能和openssl dgst命令差很少,且自身又有缺陷,因此就不舉例說明。此處僅給出對短小文件的非對稱加密和解密示例。htm

(1).使用公鑰加密b.txt文件,注意待加密文件b.txt必須是短小文件,且不建議使用-hexdump輸出,不然解密時可能超出文件的長度。blog

[root@xuexi tmp]# openssl genrsa -out genrsa.pri   # 生成私鑰

[root@xuexi tmp]# openssl rsa -in genrsa.pri -pubout -out rsa.pub   # 從私鑰中提取公鑰

[root@xuexi tmp]# openssl rsautl -encrypt -in b.txt -out b_crypt.txt -inkey rsa.pub -pubin

查看非對稱加密後的文件b_crypt.txt。ssl

[root@xuexi tmp]# cat b_crypt.txt
H[1]=p?I,:=)Iڪ;Yx٩,vbot@xuexi tmp]#

(2).使用私鑰解密b_crypt.txt文件。get

[root@xuexi tmp]# openssl rsautl -decrypt -in b_crypt.txt -out b_decrypt.txt -inkey genrsa.pri

[root@xuexi tmp]# cat b_decrypt.txt
UUID=d505113c-daa6-4c17-8b03-b3551ced2305 swap                    swap    defaults        0 0
相關文章
相關標籤/搜索