PHP openssl 与 JS 信息交互
PHP
1 private function _decode( $data, $key ) 2 { 3 $iv = substr( $data, -16 ); 4 $data = substr( $data, 0, strlen( $data ) - 16 ); 5 6 $string = ''; 7 $retlen = strlen( $data ); 8 9 for( $i = 0; $i < $retlen; $i += 2 ) { 10 $tmp = $data[$i]; 11 if( $data[$i+1] != 'g' ) 12 $tmp .= $data[$i+1]; 13 $string .= chr( hexdec( $tmp ) ); 14 } 15 16 $ret = openssl_decrypt( $string, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv );//Pkcs7 17 if( !$ret ){ 18 var_dump( openssl_error_string() ); 19 return false; 20 } 21 return base64_decode( $ret ); 22 } 23 24 private function _encode( $string, $key ) 25 { 26 $iv = substr( str_shuffle( md5( $key ) ), 0, 16 ); 27 28 $encrypt = openssl_encrypt( base64_encode( $string ), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv ); 29 if( !$encrypt ) 30 return false; 31 //字符串转16进制。 36-38要注意下,有些特殊字符转换后只有一个字符,加一个不存在的16进制字符后缀,以区分出来,同时保持格式正确。 32 $ret = ''; 33 $lenght = strlen( $encrypt ); 34 for( $i = 0; $i < $lenght; $i++ ) { 35 $tmpHex = dechex( ord( $encrypt[$i] ) ); 36 if( strlen( $tmpHex ) != 2 ) { 37 $tmpHex .= "g"; 38 } 39 40 $ret .= $tmpHex; 41 } 42 return $ret . $iv; 43 }
JS (包:crypto-js【https://www.npmjs.com/package/crypto-js】)
1 encryption: function (str) { 2 var pass = "xxxxx" //与php保持一致,256 -> 32位 3 var iv = this.strShuffle(pass).slice(16); 4 //console.log(cryptoJs.enc.Base64.stringify( cryptoJs.enc.Utf8.parse(str) )); 5 var _waStr = cryptoJs.enc.Utf8.parse(cryptoJs.enc.Base64.stringify(cryptoJs.enc.Utf8.parse(str)) ); 6 // cryptoJs.pad.Pkcs7.pad(_waStr,8); 7 var _waPass = cryptoJs.enc.Utf8.parse( pass ); 8 var _waIv = cryptoJs.enc.Utf8.parse( iv ); 9 10 var encrypt = cryptoJs.AES.encrypt( 11 _waStr, 12 _waPass, 13 { 14 iv: _waIv, 15 } 16 ).ciphertext 17 // console.log(cryptoJs.enc.Base64.stringify(encrypt)); 18 console.log("finally == ", encrypt.toString() + iv ); 19 return encrypt.toString() + iv; 20 }, 21 decryption: function ( str ) { 22 var pass = "xxxxxx" 23 var iv = str.slice( -16 ); 24 var data = str.substr( 0, str.length - 16 ); 25 //console.log( data); 26 var _waData = cryptoJs.enc.Hex.parse(data); 27 // cryptoJs.pad.Pkcs7.unpad(_waData, 8); 28 var _waIv = cryptoJs.enc.Utf8.parse(iv); 29 var _waPass = cryptoJs.enc.Utf8.parse(pass); 30 31 var decrypt = cryptoJs.AES.decrypt( 32 cryptoJs.lib.CipherParams.create({ ciphertext: _waData }), 33 _waPass, 34 { 35 iv: _waIv, 36 } 37 ) 38 // cryptoJs.pad.Pkcs7.unpad(decrypt, 8); 39 console.log("obj = ", cryptoJs.enc.Utf8.stringify(cryptoJs.enc.Base64.parse( cryptoJs.enc.Utf8.stringify( decrypt) ) )); 40 return cryptoJs.enc.Utf8.stringify(cryptoJs.enc.Base64.parse(cryptoJs.enc.Utf8.stringify(decrypt))); 41 // var test = '{"key":"key","ssid":"ssid"}'; 42 // console.log("22 = ", cryptoJs.enc.Base64.stringify( cryptoJs.enc.Utf8.parse(test) ) ); 43 44 // var test = 'eyJrZXkiOiJrZXkiLCJzc2lkIjoic3NpZCJ9'; 45 // console.log("22 = ", cryptoJs.enc.Utf8.stringify(cryptoJs.enc.Base64.parse(test)) ); 46 }, 47 strShuffle: function (str) { 48 return (str.split("").sort( 49 function () { 50 return Math.random() - 0.5 51 } 52 )).join(""); 53 }