<?php /** * Created by PhpStorm. * User: fuxia * Date: 2019/1/15 * Time: 18:16 */ //数组对象转数组 function object_array($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] =object_array($value); } } return $array; } /** * 进行3des ecb 模式加密 * * @param $message * @param string $key * @return string */ function des3_ecb_pkcs5($message,$key,$retrun_function){ $message = pkcs5Pad($message, 8); //pkcs5模式填充 还有个pkcs7填充 if (strlen($message) % 8) { $message_padded = str_pad($message, strlen($message) + 8 - strlen($message) % 8, "\0"); } return $retrun_function(openssl_encrypt($message, 'DES-EDE3', $key, OPENSSL_NO_PADDING)); //转为16进进制 或者base64 } /** * 使用pkcs5填充 * * @param $text * @param $blockSize * @return string */ function pkcs5Pad($text, $blockSize) { $pad = $blockSize - (strlen ( $text ) % $blockSize); return $text . str_repeat ( chr ( $pad ), $pad ); } function pkcs5_padding($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } /** * @param $text * @return string */ function pkcs5Pad1($text) { $pad = 8 - (strlen($text) % 8); return $text . str_repeat(chr($pad), $pad); } /* * 毫秒时间戳生成函数*/ function msectime() { list($msec, $sec) = explode(' ', microtime()); $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); return $msectime; } /** * [encrypt aes加密] * @param [type] $input [要加密的数据] * @param [type] $key [加密key] * @return [type] [加密后的数据] */ function aesEncrypt($input, $key,$method) { $data = openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING,$key); return $data; } /** * [decrypt aes解密] * @param [type] $sStr [要解密的数据] * @param [type] $sKey [加密key] * @return [type] [解密后的数据] */ function aesDecrypt($sStr, $sKey,$method) { $decrypted = openssl_decrypt($sStr, $method, $sKey, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING,$sKey); return $decrypted; } /** * 读取私钥 * @param type $PfxPath * @param type $PrivateKPASS * @return array * @throws Exception */ function getPriveKey($PfxPath,$PrivateKPASS){ try { if(!file_exists($PfxPath)) { throw new Exception("私钥文件不存在!路径:".$PfxPath); } $PKCS12 = file_get_contents($PfxPath); $PrivateKey = array(); if(openssl_pkcs12_read($PKCS12, $PrivateKey, $PrivateKPASS)){ return $PrivateKey["pkey"]; }else{ throw new Exception("私钥证书读取出错!原因[证书或密码不匹配],请检查本地证书相关信息。"); } } catch (Exception $ex) { $ex->getTrace(); } } /** * 读取公钥 * @param type $PublicPath * @return type * @throws Exception */ function getPublicKey($PublicPath){ try { if(!file_exists($PublicPath)) { throw new Exception("公钥文件不存在!路径:".$PublicPath); } $KeyFile = file_get_contents($PublicPath); $PublicKey = openssl_get_publickey($KeyFile); return $PublicKey; } catch (Exception $ex) { $ex->getTraceAsString(); } } /** * RSA公钥加密 * @param type $Data 加密数据 * @param type $PfxPath 公钥路径 * @return type * @throws Exception */ function rsaEncrypt($Data,$PublicPath,bool $base64){ try { if (!function_exists( 'bin2hex')) { throw new Exception("bin2hex PHP5.4及以上版本支持此函数,也可自行实现!"); } $KeyObj =getPublicKey($PublicPath); if($base64){ $Data = base64_encode($Data);//数据转为base64后RSA加密 } $EncryptStr = ""; $blockSize=117;//分段长度 $totalLen = strlen($Data); $EncryptSubStarLen = 0; while ($EncryptSubStarLen < $totalLen){ openssl_public_encrypt(substr($Data, $EncryptSubStarLen, $blockSize), $EncryptStr, $KeyObj); $EncryptSubStarLen += $blockSize; } return $EncryptStr; } catch (Exception $exc) { echo $exc->getTraceAsString(); } } /** * RSA私钥解密 * @param type $Data 解密数据 * @param type $PfxPath 私钥路径 * @return type * @throws Exception */ function rsaDecrypt($Data,$PfxPath,$PrivateKPASS,bool $base64){ try { if (!function_exists( 'hex2bin')) { throw new Exception("hex2bin PHP5.4及以上版本支持此函数,也可自行实现!"); } $KeyObj =getPriveKey($PfxPath,$PrivateKPASS); $DecryptRsult=""; $blockSize=256;//分段长度 $totalLen = strlen($Data); $EncryptSubStarLen = 0; $DecryptTempData=""; while ($EncryptSubStarLen < $totalLen) { openssl_private_decrypt(hex2bin(substr($Data, $EncryptSubStarLen, $blockSize)), $DecryptTempData, $KeyObj); $DecryptRsult .= $DecryptTempData; $EncryptSubStarLen += $blockSize; } if($base64){ return base64_decode($DecryptRsult); }else{ return $DecryptRsult; } } catch (Exception $exc) { echo $exc->getTraceAsString(); } } // 私钥加密 function encryptedByPrivateKey($data_content,$private_key_path,$private_key_password,bool $base64){ if($base64){ $data_content = base64_encode($data_content);//数据转为base64 } // 初始化商户私钥 $private_key=getPriveKey($private_key_path,$private_key_password); $encrypted = ""; $totalLen = strlen($data_content); $encryptPos = 0; while ($encryptPos < $totalLen){ openssl_private_encrypt(substr($data_content, $encryptPos, 32), $encryptData, $private_key); $encrypted .= bin2hex($encryptData); $encryptPos += 32; } return $encrypted; } // 公钥解密 function decryptByPublicKey($encrypted,$public_path,bool $base64){ $decrypt = ""; $totalLen = strlen($encrypted); $public_key=getPublicKey($public_path); $decryptPos = 0; while ($decryptPos < $totalLen) { openssl_public_decrypt(hex2bin(substr($encrypted, $decryptPos, 32 * 8)), $decryptData, $public_key); $decrypt .= $decryptData; $decryptPos += 32 * 8; } //openssl_public_decrypt($encrypted, $decryptData, $this->public_key); if ($base64){ $decrypt = base64_decode($decrypt); } return $decrypt; } /** *RSA签名sha1withRSA * @param type $Data 原数据 * @param type $PfxPath 私钥路径 * @param type $Pwd 私钥密码 * @return type * @throws Exception * @author 大圣 <dasheng@baofu.com> */ function rsaSign($Data,$PfxPath,$Pwd) { if (!function_exists( 'bin2hex')) { throw new Exception("bin2hex PHP5.4及以上版本支持此函数,也可自行实现!"); } if(!file_exists($PfxPath)) { throw new Exception("私钥文件不存在!"); } $PrivateKey=getPriveKey($PfxPath,$Pwd); if (openssl_sign($Data, $BinarySignature, $PrivateKey, OPENSSL_ALGO_SHA1)) { return $BinarySignature; } else { throw new Exception("加签异常!"); } } /** * RSA验证签名自己生成的是否正确 * * @param string $Data 签名的原文 * @param string $CerPath 公钥路径 * @param string $SignaTure 签名 * @return bool * @author 大圣 <dasheng@baofu.com> */ function rsaVerifySign($Data,$CerPath,$SignaTure) { if (!function_exists( 'hex2bin')) { throw new Exception("hex2bin PHP5.4及以上版本支持此函数,也可自行实现!"); } if(!file_exists($CerPath)) { throw new Exception("公钥文件不存在!路径:".$CerPath); } $Certs=getPublicKey($CerPath); $ok = openssl_verify($Data,$SignaTure, $Certs); if ($ok == 1) { return true; } return false; } /** * 排序输出k=v&k1=v1.....格式 * @param type $DArray * @return type */ function SortAndOutString($DArray) { $TempData = array(); foreach ($DArray As $Key => $Value){ if(!isBlank($Value)){ $TempData[$Key] = $Value; } } ksort($TempData);//排序 return http_build_query($TempData); } /** * 判断是否空值 * @param type $Strings * @return boolean */ function isBlank($Strings){ if(empty(trim($Strings)) || ($Strings == null) || (strlen($Strings) == 0)){ return true; }else{ return FALSE; } } /** * SHA1摘要 * @param type $Strings * @return type */ function Sha1AndHex($Strings){ return openssl_digest($Strings, "SHA1"); } //将数组中所有键名转换为大写 function array_key_upper(array $data,$method) { if ($method){ $da=array_change_key_case($data,CASE_UPPER ); }else{ $da=array_change_key_case($data); } foreach ($da as $k => $v) { if (is_array($v)) { $da[$k]=$this->array_key_upper($v); } } return $da; } /** * 价格由元转分 * @param $price 金额 * @return int */ function Yuan2fen($price){ $price = (int)bcmul(100,number_format($price,2,'.',''),'2'); return $price; }
php7常用不常用 对接java加密 解密函数 RSA AES 有需要的在下方评论
最新推荐文章于 2023-08-20 20:27:34 发布