當前位置:首頁 » 操作系統 » openssl演算法

openssl演算法

發布時間: 2022-10-01 12:03:19

『壹』 OpenSSL的RSA演算法,怎麼自己設置N 和 D

復制代碼 代碼如下: <?php /** * 使用openssl實現非對稱加密 * @since 2010-07-08 */ class Rsa { /** * private key */ private $_privKey; /** * public key */ private $_pubKey; /** * the keys saving path */ private $_keyPath; /** * the construtor,the param $path is the keys saving path */ public function __construct($path) { if(empty($path) !is_dir($path)){ throw new Exception('Must set the keys save path'); } $this->_keyPath = $path; } /** * create the key pair,save the key to $this->_keyPath */ public function createKey() { $r = openssl_pkey_new(); openssl_pkey_export($r, $privKey); file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); $this->_privKey = openssl_pkey_get_public($privKey); $rp = openssl_pkey_get_details($r); $pubKey = $rp['key']; file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); $this->_pubKey = openssl_pkey_get_public($pubKey); } /** * setup the private key */ public function setupPrivKey() { if(is_resource($this->_privKey)){ return true; } $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'; $prk = file_get_contents($file); $this->_privKey = openssl_pkey_get_private($prk); return true; } /** * setup the public key */ public function setupPubKey() { if(is_resource($this->_pubKey)){ return true; } $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'; $puk = file_get_contents($file); $this->_pubKey = openssl_pkey_get_public($puk); return true; } /** * encrypt with the private key */ public function privEncrypt($data) { if(!is_string($data)){ return null; } $this->setupPrivKey(); $r = openssl_private_encrypt($data, $encrypted, $this->_privKey); if($r){ return base64_encode($encrypted); } return null; } /** * decrypt with the private key */ public function privDecrypt($encrypted) { if(!is_string($encrypted)){ return null; } $this->setupPrivKey(); $encrypted = base64_decode($encrypted); $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); if($r){ return $decrypted; } return null; } /** * encrypt with public key */ public function pubEncrypt($data) { if(!is_string($data)){ return null; } $this->setupPubKey(); $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey); if($r){ return base64_encode($encrypted); } return null; } /** * decrypt with the public key */ public function pubDecrypt($crypted) { if(!is_string($crypted)){ return null; } $this->setupPubKey(); $crypted = base64_decode($crypted); $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); if($r){ return $decrypted; } return null; } public function __destruct() { @ fclose($this->_privKey); @ fclose($this->_pubKey); } } //以下是一個簡單的測試demo,如果不需要請刪除 $rsa = new Rsa('ssl-key'); //私鑰加密,公鑰解密 echo 'source:我是老鱉<br />'; $pre = $rsa->privEncrypt('我是老鱉'); echo 'private encrypted:<br />' . $pre . '<br />'; $pud = $rsa->pubDecrypt($pre); echo 'public decrypted:' . $pud . '<br />'; //公鑰加密,私鑰解密 echo 'source:干IT的<br />'; $pue = $rsa->pubEncrypt('干IT的'); echo 'public encrypt:<br />' . $pue . '<br />'; $prd = $rsa->privDecrypt($pue); echo 'private decrypt:' . $prd; ?> 需要注意的是apache要支持OpenSSL

『貳』 Linux裡面openssl作用是什麼

主要是用來安全的。
openssl命令 – 加密演算法
openSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。對應的命令就是openssl命令,用於加密演算法。《Linux就該這么學》
語法格式:openssl [參數]
舉例子:
用SHA1演算法計算文件file.txt的哈西值,輸出到stdout:
# openssl dgst -sha1 file.txt

『叄』 如何向 openssl 加入 自定義 對稱演算法

openssl具備良好的擴展機制,可以加入自定義的演算法,通過修改編譯腳本,可以使實現自己的演算法與openssl原有的演算法無縫集成,渾然一體。
其過程主要還在於觀測ms/do_ms的過程
通過修改util/mkfiles.pl 文件,可以加入自定義演算法的目錄,演算法目錄中包含Makefile文件,會根據makefile中的步驟進行編譯。
修改util/mkdef.pl文件,在裡面加入自己的頭文件,可以在DLL中導出你的頭文件中自定義演算法函數。
修改util/libeay.num,在裡面增加導出函數

『肆』 OpenSSL詳解

OpenSSL初接觸的人恐怕最難的在於先理解各種概念

公鑰/私鑰/簽名/驗證簽名/加密/解密/非對稱加密

我們一般的加密是用一個密碼加密文件,然後解密也用同樣的密碼.這很好理解,這個是對稱加密.而有些加密時,加密用的一個密碼,而解密用另外一組密碼,這個叫非對稱加密,意思就是加密解密的密碼不一樣.初次接觸的人恐怕無論如何都理解不了.其實這是數學上的一個素數積求因子的原理的應用,如果你一定要搞懂,網路有大把大把的資料可以看,其結果就是用這一組密鑰中的一個來加密數據,可以用另一個解開.是的沒錯,公鑰和私鑰都可以用來加密數據,相反用另一個解開,公鑰加密數據,然後私鑰解密的情況被稱為加密解密,私鑰加密數據,公鑰解密一般被稱為簽名和驗證簽名.

因為公鑰加密的數據只有它相對應的私鑰可以解開,所以你可以把公鑰給人和人,讓他加密他想要傳送給你的數據,這個數據只有到了有私鑰的你這里,才可以解開成有用的數據,其他人就是得到了,也看懂內容.同理,如果你用你的私鑰對數據進行簽名,那這個數據就只有配對的公鑰可以解開,有這個私鑰的只有你,所以如果配對的公鑰解開了數據,就說明這數據是你發的,相反,則不是.這個被稱為簽名.

實際應用中,一般都是和對方交換公鑰,然後你要發給對方的數據,用他的公鑰加密,他得到後用他的私鑰解密,他要發給你的數據,用你的公鑰加密,你得到後用你的私鑰解密,這樣最大程度保證了安全性.

RSA/DSA/SHA/MD5

非對稱加密的演算法有很多,比較著名的有RSA/DSA ,不同的是RSA可以用於加/解密,也可以用於簽名驗簽,DSA則只能用於簽名.至於SHA則是一種和md5相同的演算法,它不是用於加密解密或者簽名的,它被稱為摘要演算法.就是通過一種演算法,依據數據內容生成一種固定長度的摘要,這串摘要值與原數據存在對應關系,就是原數據會生成這個摘要,但是,這個摘要是不能還原成原數據的,嗯....,正常情況下是這樣的,這個演算法起的作用就是,如果你把原數據修改一點點,那麼生成的摘要都會不同,傳輸過程中把原數據給你再給你一個摘要,你把得到的原數據同樣做一次摘要演算法,與給你的摘要相比較就可以知道這個數據有沒有在傳輸過程中被修改了.

實際應用過程中,因為需要加密的數據可能會很大,進行加密費時費力,所以一般都會把原數據先進行摘要,然後對這個摘要值進行加密,將原數據的明文和加密後的摘要值一起傳給你.這樣你解開加密後的摘要值,再和你得到的數據進行的摘要值對應一下就可以知道數據有沒有被修改了,而且,因為私鑰只有你有,只有你能解密摘要值,所以別人就算把原數據做了修改,然後生成一個假的摘要給你也是不行的,你這邊用密鑰也根本解不開.

CA/PEM/DER/X509/PKCS

一般的公鑰不會用明文傳輸給別人的,正常情況下都會生成一個文件,這個文件就是公鑰文件,然後這個文件可以交給其他人用於加密,但是傳輸過程中如果有人惡意破壞,將你的公鑰換成了他的公鑰,然後得到公鑰的一方加密數據,不是他就可以用他自己的密鑰解密看到數據了嗎,為了解決這個問題,需要一個公證方來做這個事,任何人都可以找它來確認公鑰是誰發的.這就是CA,CA確認公鑰的原理也很簡單,它將它自己的公鑰發布給所有人,然後一個想要發布自己公鑰的人可以將自己的公鑰和一些身份信息發給CA,CA用自己的密鑰進行加密,這里也可以稱為簽名.然後這個包含了你的公鑰和你的信息的文件就可以稱為證書文件了.這樣一來所有得到一些公鑰文件的人,通過CA的公鑰解密了文件,如果正常解密那麼機密後裡面的信息一定是真的,因為加密方只可能是CA,其他人沒它的密鑰啊.這樣你解開公鑰文件,看看裡面的信息就知道這個是不是那個你需要用來加密的公鑰了.

實際應用中,一般人都不會找CA去簽名,因為那是收錢的,所以可以自己做一個自簽名的證書文件,就是自己生成一對密鑰,然後再用自己生成的另外一對密鑰對這對密鑰進行簽名,這個只用於真正需要簽名證書的人,普通的加密解密數據,直接用公鑰和私鑰來做就可以了.

密鑰文件的格式用OpenSSL生成的就只有PEM和DER兩種格式,PEM的是將密鑰用base64編碼表示出來的,直接打開你能看到一串的英文字母,DER格式是二進制的密鑰文件,直接打開,你可以看到........你什麼也看不懂!.X509是通用的證書文件格式定義.pkcs的一系列標準是指定的存放密鑰的文件標准,你只要知道PEM DER X509 PKCS這幾種格式是可以互相轉化的.

== End http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==

為了方便理解,我畫了一個圖,如下:

使用 openssl 生成證書(含openssl詳解)
一、openssl 簡介
openssl 是目前最流行的 SSL 密碼庫工具,其提供了一個通用、健壯、功能完備的工具套件,用以支持SSL/TLS 協議的實現。
官網: https://www.openssl.org/source/

構成部分
密碼演算法庫

密鑰和證書封裝管理功能

SSL通信API介面

用途
建立 RSA、DH、DSA key 參數

建立 X.509 證書、證書簽名請求(CSR)和CRLs(證書回收列表)

計算消息摘要

使用各種 Cipher加密/解密

SSL/TLS 客戶端以及伺服器的測試

處理S/MIME 或者加密郵件

二、RSA密鑰操作
默認情況下,openssl 輸出格式為 PKCS#1-PEM

生成RSA私鑰(無加密)

openssl genrsa -out rsa_private.key 2048

生成RSA公鑰

openssl rsa -in rsa_private.key -pubout -out rsa_public.key

生成RSA私鑰(使用aes256加密)

openssl genrsa -aes256 -passout pass:111111 -out rsa_aes_private.key 2048

其中 passout 代替shell 進行密碼輸入,否則會提示輸入密碼;
生成加密後的內容如:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,
Base64 Encoded
Data-----END RSA PRIVATE KEY-----

此時若生成公鑰,需要提供密碼

openssl rsa -in rsa_aes_private.key -passin pass:111111 -pubout -out rsa_public.key

其中 passout 代替shell 進行密碼輸入,否則會提示輸入密碼;

轉換命令
私鑰轉非加密

openssl rsa -in rsa_aes_private.key -passin pass:111111 -out rsa_private.key

私鑰轉加密

openssl rsa -in rsa_private.key -aes256 -passout pass:111111 -out rsa_aes_private.key

私鑰PEM轉DER

openssl rsa -in rsa_private.key -outform der-out rsa_aes_private.der

-inform和-outform 參數制定輸入輸出格式,由der轉pem格式同理

查看私鑰明細

openssl rsa -in rsa_private.key -noout -text

使用-pubin參數可查看公鑰明細

私鑰PKCS#1轉PKCS#8

openssl pkcs8 -topk8 -in rsa_private.key -passout pass:111111 -out pkcs8_private.key

其中-passout指定了密碼,輸出的pkcs8格式密鑰為加密形式,pkcs8默認採用des3 加密演算法,內容如下:

-----BEGIN ENCRYPTED PRIVATE KEY-----
Base64 Encoded Data
-----END ENCRYPTED PRIVATE KEY-----

使用-nocrypt參數可以輸出無加密的pkcs8密鑰,如下:

-----BEGIN PRIVATE KEY-----
Base64 Encoded Data
-----END PRIVATE KEY-----

三、生成CA自簽名證書和RSA私鑰(測試場景步驟)
測試場景步驟1:生成 RSA 私鑰和自簽名證書:

openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt

注釋:

操作步驟如下:提示填寫過程中如果想刪除填寫的內容,用ctrl+Backspace刪除前面的字元

[root@szxelab01-web-100 cert]# openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt
Generating a 2048 bit RSA private key
.............+++
........................+++
writing new private key to 'rsa_private.key'

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GuangDong
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:SunFoBank
Organizational Unit Name (eg, section) []:IT Dept
Common Name (eg, your name or your server's hostname) []:sunfobank.com
Email Address [] :[email protected]

[root@szxjdwins01-web-27 cert]# ll
total 8
-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt
-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key

openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt -subj "/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN= sunfobank.com/[email protected] "

openssl req -new -x509 -days 36500 -key rsa_private.key -out cert.crt

四、生成伺服器簽名請求文件及CA 簽名頒發伺服器證書()
server.key建議不要加密碼,如果加密碼,重啟nginx的時候每次都需要密碼才可以啟動nginx影響效率。
nginx配置只需要server.key和server.crt兩個文件。

openssl genrsa -aes256 -passout pass:111111 -out server.key 2048
openssl req -new -key server.key -out server.csr

[root@szxjdwins01-web-27 cert]# openssl genrsa -aes256 -passout pass:111111 -out server.key 2048
Generating RSA private key, 2048 bit long molus
............................+++
.......+++
e is 65537 (0x10001)

[root@szxjdwins01-web-27 cert]# openssl genrsa -aes256 -out server.key 2048
Generating RSA private key, 2048 bit long molus
.............................................+++
........................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key: 111111手動輸入密碼
Verifying - Enter pass phrase for server.key: 111111手動輸入密碼

[root@szxelab01-web-27 cert]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GuangDong
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:SunFoBank
Organizational Unit Name (eg, section) []:IT Dept
Common Name (eg, your name or your server's hostname) []:sunfobank.com
Email Address [] :[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 不輸入密碼
An optional company name []: 不輸入密碼

此後輸入密碼、server證書信息完成,也可以命令行指定各類參數

openssl req -new -key server.key -passin pass:111111 -out server.csr -subj "/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN= sunfobank.com/[email protected] "

*** 此時生成的 csr簽名請求文件可提交至 CA進行簽發 ***

cat server.csr
-----BEGIN CERTIFICATE REQUEST-----
Base64EncodedData
-----END CERTIFICATE REQUEST-----

openssl req -noout -text -in server.csr

openssl x509 -req -days 365000 -in server.csr -CA cert.crt -CAkey rsa_private.key -passin pass:111111 -CAcreateserial -out server.crt

[root@szxelab01-web-27 cert]# openssl x509 -req -days 365000 -in server.csr -CA cert.crt -CAkey rsa_private.key -passin pass:111111 -CAcreateserial -out server.crt
Signature ok
subject=/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN= sunfobank.com/[email protected]
Getting CA Private Key

其中 CAxxx 選項用於指定CA 參數輸入

[root@szxelab01-web-27 cert]# ll
total 24
-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt
-rw-r--r--. 1 root root 17 Jun 22 15:07 cert.srl
-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key
-rw-r--r--. 1 root root 1334 Jun 22 15:07 server.crt
-rw-r--r--. 1 root root 1070 Jun 22 15:04 server.csr
-rw-r--r--. 1 root root 1766 Jun 22 14:54 server.key

此時對nginx任何操作,都需要提示輸入server.key的密碼才可以執行。
[root@szxelab01-web-27 nginx]# /application/nginx/sbin/nginx -t
Enter PEM pass phrase: 輸入密碼111111
nginx: the configuration file /application/nginx-1.12.2//conf/nginx.conf syntax is ok

為例不輸入密碼,需要把加密server.key轉換成不加密的server.key

[root@szxelab01-web-27 cert]# openssl rsa -in server.key -passin pass:111111 -out server.key
writing RSA key

此時nginx操作就不提示輸入密碼了:
[root@szxelab01-web-27 cert]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.12.2//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2//conf/nginx.conf test is successful

證書位置:
[root@szxelab01-web-27 cert]# pwd
/application/nginx/cert
[root@szxelab01-web-27 cert]# ll
total 24
-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt
-rw-r--r--. 1 root root 17 Jun 22 15:07 cert.srl
-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key
-rw-r--r--. 1 root root 1334 Jun 22 15:07 server.crt
-rw-r--r--. 1 root root 1070 Jun 22 15:04 server.csr
-rw-r--r--. 1 root root 1679 Jun 22 15:19 server.key

至此測試場景私有證書配置完成
五、證書查看及轉換
查看證書細節

openssl x509 -in cert.crt -noout -text

轉換證書編碼格式

openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem

合成 pkcs#12 證書(含私鑰)

** 將 pem 證書和私鑰轉 pkcs#12 證書 **

openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 -password pass:111111 -out server.p12

其中-export指導出pkcs#12 證書,-inkey 指定了私鑰文件,-passin 為私鑰(文件)密碼(nodes為無加密),-password 指定 p12文件的密碼(導入導出)

** 將 pem 證書和私鑰/CA 證書 合成pkcs#12 證書**

openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 \ -chain -CAfile ca.crt -password pass:111111 -out server-all.p12

其中-chain指示同時添加證書鏈,-CAfile 指定了CA證書,導出的p12文件將包含多個證書。(其他選項:-name可用於指定server證書別名;-caname用於指定ca證書別名)

** pcks#12 提取PEM文件(含私鑰) **

openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -out out/server.pem

其中-password 指定 p12文件的密碼(導入導出),-passout指輸出私鑰的加密密碼(nodes為無加密)
導出的文件為pem格式,同時包含證書和私鑰(pkcs#8):

Bag Attributes
localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 subject=/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN= vihoo.com/[email protected]
issuer=/C=CN/ST=GD/L=SZ/O=viroot/OU=dev/CN= viroot.com/[email protected] CERTIFICATE-----MIIDazCCAlMCCQCIOlA9/
1LpQCA+2B6dn4scZwaCD-----END CERTIFICATE-----Bag Attributes
localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 Key Attributes: <No Attributes>
-----BEGIN ENCRYPTED PRIVATE KEY-----/6rAc1YaPRNf
K9ZLHbyBTKVaxehjxzJHHw==
-----END ENCRYPTED PRIVATE KEY-----

僅提取私鑰

openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -nocerts -out out/key.pem

僅提取證書(所有證書)

openssl pkcs12 -in server.p12 -password pass:111111 -nokeys -out out/key.pem

僅提取ca證書

openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -cacerts -out out/cacert.pem

僅提取server證書

openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -clcerts -out out/cert.pem

六、openssl 命令參考

『伍』 openssl 對稱加密有哪些

OpenSSL一共提供了8種對稱加密演算法,其中7種是分組加密演算法,僅有的一種流加密演算法是RC4。這7種分組加密演算法分別是AES、DES、Blowfish、CAST、IDEA、RC2、RC5。
參考網路。

熱點內容
跳轉頁源碼 發布:2024-09-17 03:13:05 瀏覽:543
html文件上傳表單 發布:2024-09-17 03:08:02 瀏覽:784
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:726
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688
兩個安卓手機照片怎麼同步 發布:2024-09-17 01:51:53 瀏覽:207
cf編譯後沒有黑框跳出來 發布:2024-09-17 01:46:54 瀏覽:249
安卓怎麼禁用應用讀取列表 發布:2024-09-17 01:46:45 瀏覽:524
win10設密碼在哪裡 發布:2024-09-17 01:33:32 瀏覽:662
情逢敵手迅雷下載ftp 發布:2024-09-17 01:32:35 瀏覽:337
安卓如何讓軟體按照步驟自動運行 發布:2024-09-17 01:28:27 瀏覽:197