php如何openssl_encrypt加密解密
鏈杩戝湪瀵规帴瀹㈡埛鐨凜RM绯荤粺锛岃幏鍙栦护鐗屾椂锛岃鐢―ES鏂瑰紡鍔犲瘑瑙e瘑锛岀敱浜庝箣鍓嶆病鏈夋悶閿欒繖绉嶅姞瀵嗘柟寮忥紝缁忚繃璇锋暀浜嗏滅櫨搴︹濆拰鈥滆胺姝屸濅袱涓佸笀鍚庯紝缁撳悎浜嗗绡囨枃妗e唴瀹瑰悗锛岀粓浜庡疄鐜颁簡銆
涓銆丏ES浠嬬粛
DES 鏄绉版у姞瀵嗛噷闈㈠父瑙佷竴绉嶏紝鍏ㄧО涓 Data Encryption Standard锛屽嵆鏁版嵁鍔犲瘑鏍囧噯锛屾槸涓绉嶄娇鐢ㄥ瘑閽ュ姞瀵嗙殑鍧楃畻娉曘傚瘑閽ラ暱搴︽槸64浣(bit)锛岃秴杩囦綅鏁板瘑閽ヨ蹇界暐銆傛墍璋撳绉版у姞瀵嗗嵆鍔犲瘑鍜岃В瀵嗗瘑閽ョ浉鍚岋紝瀵圭О鎬у姞瀵嗕竴鑸細鎸夌収鍥哄畾闀垮害锛屾妸寰呭姞瀵嗗瓧绗︿覆鍒嗘垚鍧楋紝涓嶈冻涓鏁村潡鎴栬呭垰濂芥渶鍚庢湁鐗规畩濉厖瀛楃銆
璺ㄨ瑷鍋 DES 鍔犲瘑瑙e瘑缁忓父浼氬嚭鐜伴棶棰橈紝寰寰鏄~鍏呮柟寮忎笉瀵广佺紪鐮佷笉涓鑷存垨鑰呭姞瀵嗚В瀵嗘ā寮忔病鏈夊搴斾笂閫犳垚銆
甯歌鐨勫~鍏呮ā寮忔湁锛 pkcs5銆乸kcs7銆乮so10126銆乤nsix923銆亃ero銆
鍔犲瘑妯″紡鏈夛細DES-ECB銆丏ES-CBC銆丏ES-CTR銆丏ES-OFB銆丏ES-CFB銆
鍔犲瘑鐢ㄥ埌鐨勬柟娉曪細鍙傛暟璇存槑锛
$data 鍔犲瘑鏄庢枃
$method 鍔犲瘑鏂规硶
$passwd 鍔犲瘑瀵嗛挜[瀵嗙爜]
$options 鏁版嵁鏍煎紡閫夐」锛堝彲閫夛級銆愰夐」鏈夛細銆
$iv 瀵嗗垵濮嬪寲鍚戦噺锛堝彲閫夛級
闇瑕佹敞鎰忥細濡傛灉method涓篋ES鈭扙CB锛屽垯
method涓篋ES鈭扙CB锛屽垯iv鏃犻渶濉啓
浜屻佽В瀵嗙敤鍒扮殑鏂规硶锛鍙傛暟璇存槑锛
$data 瑕佽В瀵嗙殑鏁版嵁
鍏朵粬鍙傛暟鍚屽姞瀵嗘柟娉
涓夈佺敤娉曟渚嬶細
鍙傛暟锛
(1) 榛樿濉厖鏂瑰紡锛
鍔犲瘑锛
缁撴灉锛
瑙e瘑
缁撴灉锛
(2) OPENSSL_RAW_DATA鏂瑰紡銆愪細鐢≒KCS#7杩涜琛ヤ綅銆
鍔犲瘑
缁撴灉锛鎴戜滑鍙互鐪嬪埌缁撴灉鏄贡鐮佺殑锛岃繖鏃舵垜浠渶瑕乥ase64涓涓
杩欐椂缁撴灉鏄
瑙e瘑
缁撴灉锛
鎴戜滑鍙互鐪嬪埌锛氶粯璁ゅ~鍏呮柟寮忎笌OPENSSL_RAW_DATA锛岃繖涓ょ鏂瑰紡鍔犲瘑缁撴灉鏄竴鏍风殑
(3) OPENSSL_ZERO_PADDING鏂瑰紡
鐪嬪瓧闈㈡剰鎬濓紝鏄敤0濉厖锛屼絾鏄祴璇曞苟涓嶈捣浣滅敤
鍔犲瘑
缁撴灉锛
瑙e瘑锛
缁撴灉锛
(4) OPENSSL_NO_PADDING銆愪笉濉厖锛岄渶瑕佹墜鍔ㄥ~鍏呫
鍦╫penssl_encrypt鍓嶅姞涓婂~鍏呰繃绋
鍔犲瘑
缁撴灉锛
鎴戜滑鍙互鐪嬪埌缁撴灉鏄姞瀵嗙殑涔辩爜锛岄渶瑕佺敤base64涓涓嬶紝灏卞彲浠ョ湅鍒扮粨鏋滀簡
瑙e瘑锛
缁撴灉锛
** 缁撳熬瑕佸幓闄ゅ~鍏呭瓧绗︹橽0鈥欏拰鈥橽a鈥欍
鈥榎a鈥欐槸涓轰簡鍏煎鐢∣PENSSL_RAW_DATA鍔犲瘑鐨勭粨鏋溿 **
鍙傜収鐨勬枃妗f湁锛
PHP 鍩虹绡 - PHP 涓 DES 鍔犺В瀵嗚瑙
https://www.jianshu.com/p/546137b8ac7a
鍏充簬mcrypt_encrypt鍜宱penssl_encrypt鍔犲瘑缁撴灉涓嶄竴鑷寸殑瑙e喅
http://www.heylc.com/fuanyuopenssl.html
鐩稿叧鐭ヨ瘑鏂囩珷
RSA瀵嗙爜浼犺緭鍔犲瘑鏂规
https://wenku.baidu.com/view/83c ... c7089d.html?re=view
iOS 瀹炵幇瀵圭О鍔犲瘑澶氱濉厖鏂瑰紡(ANSIX923銆両SO10126銆乑ero)
https://www.jianshu.com/p/7b6f5aaa7680
PHP鐢眒crypt鎵╁睍鍔犲瘑鏀逛负openssl鎵╁睍鍔犲瘑
https://www.xxling.com/blog/article/3114.aspx
PHP鐢╫penssl_encrypt浠f浛mcrypt_encrypt
https://coderlife.cn/1624.html
AES鍔犲瘑CBC妯″紡鍏煎浜掗氬洓绉嶇紪绋嬭瑷骞冲彴銆怭HP銆丣avascript銆丣ava銆丆#銆
https://my.oschina.net/Jacker/blog/86383
鍦≒HP7.1涓娇鐢╫penssl鍙栦唬mcrypt
https://swoole.app/2018/05/15/鍦╬hp7-1涓娇鐢╫penssl鍙栦唬mcrypt/
PHP openssl鍔犲瘑鎵╁睍浣跨敤鎬荤粨
https://www.bbsmax.com/A/ke5jNDe75r/
PHP 7.2+浣跨敤openssl杩涜鍔犺В瀵
https://www.lytit.com/2018/01/26/118/
PHP OpenSSL鎵╁睍 - 瀵圭О鍔犲瘑
https://www.jianshu.com/p/8f82e8fd123e
DES 鍔犺В瀵嗗伐鍏
http://tool.chacuo.net/cryptdes
RSA濉厖鏂瑰紡
https://www.jianshu.com/p/205abb4b9dc6
AES鍔犲瘑妯″紡鍜屽~鍏呮柟寮忥紝hash锛宮d5锛宑a
http://blog.sina.com.cn/s/blog_679daa6b0100zmpp.html
涓銆丏ES浠嬬粛
DES 鏄绉版у姞瀵嗛噷闈㈠父瑙佷竴绉嶏紝鍏ㄧО涓 Data Encryption Standard锛屽嵆鏁版嵁鍔犲瘑鏍囧噯锛屾槸涓绉嶄娇鐢ㄥ瘑閽ュ姞瀵嗙殑鍧楃畻娉曘傚瘑閽ラ暱搴︽槸64浣(bit)锛岃秴杩囦綅鏁板瘑閽ヨ蹇界暐銆傛墍璋撳绉版у姞瀵嗗嵆鍔犲瘑鍜岃В瀵嗗瘑閽ョ浉鍚岋紝瀵圭О鎬у姞瀵嗕竴鑸細鎸夌収鍥哄畾闀垮害锛屾妸寰呭姞瀵嗗瓧绗︿覆鍒嗘垚鍧楋紝涓嶈冻涓鏁村潡鎴栬呭垰濂芥渶鍚庢湁鐗规畩濉厖瀛楃銆
璺ㄨ瑷鍋 DES 鍔犲瘑瑙e瘑缁忓父浼氬嚭鐜伴棶棰橈紝寰寰鏄~鍏呮柟寮忎笉瀵广佺紪鐮佷笉涓鑷存垨鑰呭姞瀵嗚В瀵嗘ā寮忔病鏈夊搴斾笂閫犳垚銆
甯歌鐨勫~鍏呮ā寮忔湁锛 pkcs5銆乸kcs7銆乮so10126銆乤nsix923銆亃ero銆
鍔犲瘑妯″紡鏈夛細DES-ECB銆丏ES-CBC銆丏ES-CTR銆丏ES-OFB銆丏ES-CFB銆
鍔犲瘑鐢ㄥ埌鐨勬柟娉曪細
- openssl_encrypt($data, $method, $password, $options, $iv)
$data 鍔犲瘑鏄庢枃
$method 鍔犲瘑鏂规硶
- DES-ECB DES-CBC DES-CTR DES-OFB DES-CFB
$passwd 鍔犲瘑瀵嗛挜[瀵嗙爜]
$options 鏁版嵁鏍煎紡閫夐」锛堝彲閫夛級銆愰夐」鏈夛細銆
- 0 OPENSSL_RAW_DATA=1 OPENSSL_ZERO_PADDING=2 OPENSSL_NO_PADDING=3
$iv 瀵嗗垵濮嬪寲鍚戦噺锛堝彲閫夛級
闇瑕佹敞鎰忥細濡傛灉method涓篋ES鈭扙CB锛屽垯
method涓篋ES鈭扙CB锛屽垯iv鏃犻渶濉啓
浜屻佽В瀵嗙敤鍒扮殑鏂规硶锛
- openssl_decrypt($data, $method, $password, $options, $iv)
$data 瑕佽В瀵嗙殑鏁版嵁
鍏朵粬鍙傛暟鍚屽姞瀵嗘柟娉
涓夈佺敤娉曟渚嬶細
鍙傛暟锛
- $data = '1234567887654321';//鍔犲瘑鏄庢枃 $method = 'DES-ECB';//鍔犲瘑鏂规硶 $passwd = '12344321';//鍔犲瘑瀵嗛挜 $options = 0;//鏁版嵁鏍煎紡閫夐」锛堝彲閫夛級 $iv = '';//鍔犲瘑鍒濆鍖栧悜閲忥紙鍙夛級
(1) 榛樿濉厖鏂瑰紡锛
鍔犲瘑锛
- $result = openssl_encrypt($data, $method, $passwd, $options);var_dump($result);
缁撴灉锛
- string(32) "kQYOdswcm9I5elv2wdJucplqAgqDNqXg"
瑙e瘑
- $result = 'kQYOdswcm9I5elv2wdJucplqAgqDNqXg'; var_dump(openssl_decrypt($result, $method, $passwd, 0));
- string(16) "1234567887654321"
(2) OPENSSL_RAW_DATA鏂瑰紡銆愪細鐢≒KCS#7杩涜琛ヤ綅銆
鍔犲瘑
- $result = openssl_encrypt($data, $method, $passwd, OPENSSL_RAW_DATA);var_dump($result);
- string(24) "锟v锟锟斤拷9z[锟斤拷锟絥r锟絡 锟6锟斤拷"
- $result = openssl_encrypt($data, $method, $passwd, OPENSSL_RAW_DATA);var_dump(base64_encode($result));
- string(32) "kQYOdswcm9I5elv2wdJucplqAgqDNqXg"
- result = openssl_encrypt($data, $method, $passwd, OPENSSL_RAW_DATA);var_dump(openssl_decrypt($result, $method, $passwd,OPENSSL_RAW_DATA));
缁撴灉锛
- string(16) "1234567887654321"
鎴戜滑鍙互鐪嬪埌锛氶粯璁ゅ~鍏呮柟寮忎笌OPENSSL_RAW_DATA锛岃繖涓ょ鏂瑰紡鍔犲瘑缁撴灉鏄竴鏍风殑
(3) OPENSSL_ZERO_PADDING鏂瑰紡
鐪嬪瓧闈㈡剰鎬濓紝鏄敤0濉厖锛屼絾鏄祴璇曞苟涓嶈捣浣滅敤
鍔犲瘑
- $result = openssl_encrypt($data, $method, $passwd, OPENSSL_ZERO_PADDING);var_dump($result);
缁撴灉锛
- string(24) "kQYOdswcm9I5elv2wdJucg=="
- $result = openssl_encrypt($data, $method, $passwd, OPENSSL_ZERO_PADDING);var_dump(openssl_decrypt($result, $method, $passwd,OPENSSL_ZERO_PADDING));
缁撴灉锛
- string(16) "1234567887654321"
鍦╫penssl_encrypt鍓嶅姞涓婂~鍏呰繃绋
鍔犲瘑
- $str_padded = $data; if (strlen($str_padded) % 16) { $str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0"); } $result = openssl_encrypt($str_padded, $method, $passwd, OPENSSL_NO_PADDING); var_dump($result); echo '<br>'; var_dump( base64_encode($result));
- string(16) "锟v锟锟斤拷9z[锟斤拷锟絥r" string(24) "kQYOdswcm9I5elv2wdJucg=="
瑙e瘑锛
- //鍔犲瘑begin $str_padded = $data; if (strlen($str_padded) % 16) { $str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0"); } $result = openssl_encrypt($str_padded, $method, $passwd, OPENSSL_NO_PADDING); //鍔犲瘑end //瑙e瘑begin $str = base64_encode($result); $m = openssl_decrypt( base64_decode($str) , $method, $passwd, OPENSSL_NO_PADDING); var_dump( rtrim( rtrim( $m,chr(0) ), chr(7) ) ); //瑙e瘑 end
缁撴灉锛
- string(16) "1234567887654321"
** 缁撳熬瑕佸幓闄ゅ~鍏呭瓧绗︹橽0鈥欏拰鈥橽a鈥欍
鈥榎a鈥欐槸涓轰簡鍏煎鐢∣PENSSL_RAW_DATA鍔犲瘑鐨勭粨鏋溿 **
鍙傜収鐨勬枃妗f湁锛
PHP 鍩虹绡 - PHP 涓 DES 鍔犺В瀵嗚瑙
https://www.jianshu.com/p/546137b8ac7a
鍏充簬mcrypt_encrypt鍜宱penssl_encrypt鍔犲瘑缁撴灉涓嶄竴鑷寸殑瑙e喅
http://www.heylc.com/fuanyuopenssl.html
鐩稿叧鐭ヨ瘑鏂囩珷
RSA瀵嗙爜浼犺緭鍔犲瘑鏂规
https://wenku.baidu.com/view/83c ... c7089d.html?re=view
iOS 瀹炵幇瀵圭О鍔犲瘑澶氱濉厖鏂瑰紡(ANSIX923銆両SO10126銆乑ero)
https://www.jianshu.com/p/7b6f5aaa7680
PHP鐢眒crypt鎵╁睍鍔犲瘑鏀逛负openssl鎵╁睍鍔犲瘑
https://www.xxling.com/blog/article/3114.aspx
PHP鐢╫penssl_encrypt浠f浛mcrypt_encrypt
https://coderlife.cn/1624.html
AES鍔犲瘑CBC妯″紡鍏煎浜掗氬洓绉嶇紪绋嬭瑷骞冲彴銆怭HP銆丣avascript銆丣ava銆丆#銆
https://my.oschina.net/Jacker/blog/86383
鍦≒HP7.1涓娇鐢╫penssl鍙栦唬mcrypt
https://swoole.app/2018/05/15/鍦╬hp7-1涓娇鐢╫penssl鍙栦唬mcrypt/
PHP openssl鍔犲瘑鎵╁睍浣跨敤鎬荤粨
https://www.bbsmax.com/A/ke5jNDe75r/
PHP 7.2+浣跨敤openssl杩涜鍔犺В瀵
https://www.lytit.com/2018/01/26/118/
PHP OpenSSL鎵╁睍 - 瀵圭О鍔犲瘑
https://www.jianshu.com/p/8f82e8fd123e
DES 鍔犺В瀵嗗伐鍏
http://tool.chacuo.net/cryptdes
RSA濉厖鏂瑰紡
https://www.jianshu.com/p/205abb4b9dc6
AES鍔犲瘑妯″紡鍜屽~鍏呮柟寮忥紝hash锛宮d5锛宑a
http://blog.sina.com.cn/s/blog_679daa6b0100zmpp.html