├── README.md ├── examples ├── OPENSSL.php └── PHPDES.php └── src ├── BASE64URL.php ├── OPENSSL.php ├── PHPDES.php └── PHPRSA.php /README.md: -------------------------------------------------------------------------------- 1 | ## PHP Cryptology 2 | 3 | $encrypt = PHPDES::encrypt($key, 'lcpeng'); 4 | $decrypt = PHPDES::decrypt($key, $encrypt); 5 | -------------------------------------------------------------------------------- /examples/OPENSSL.php: -------------------------------------------------------------------------------- 1 | strlen($text)) { 26 | return false; 27 | } 28 | if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) { 29 | return false; 30 | } 31 | return substr($text, 0, -1 * $pad); 32 | } 33 | 34 | public static function encrypt($key, $data) { 35 | $size = mcrypt_get_block_size('des', 'ecb'); 36 | $data = self::pkcs5_pad($data, $size); 37 | $td = mcrypt_module_open('des', '', 'ecb', ''); 38 | $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 39 | @mcrypt_generic_init($td, $key, $iv); 40 | $data = mcrypt_generic($td, $data); 41 | mcrypt_generic_deinit($td); 42 | mcrypt_module_close($td); 43 | return strtoupper(bin2hex($data)); 44 | //return base64_encode($data); 45 | // return $data; 46 | } 47 | 48 | public static function decrypt($key, $data) { 49 | $data = hex2bin(strtolower($data)); 50 | 51 | $td = mcrypt_module_open('des','','ecb',''); 52 | $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 53 | $ks = mcrypt_enc_get_key_size($td); 54 | @mcrypt_generic_init($td, $key, $iv); 55 | $decrypted = mdecrypt_generic($td, $data); 56 | mcrypt_generic_deinit($td); 57 | mcrypt_module_close($td); 58 | $result = self::pkcs5_unpad($decrypted); 59 | return $result; 60 | } 61 | } -------------------------------------------------------------------------------- /src/PHPRSA.php: -------------------------------------------------------------------------------- 1 | publicKey = $publicKey; 26 | $this->privateKey = $privateKey; 27 | } 28 | 29 | /** 30 | * 私钥加密 31 | * @param string $data 要加密的数据 32 | * @return string 加密后的字符串 33 | */ 34 | public function privateKeyEncode($data) { 35 | $encrypted = ''; 36 | $this->_needKey(2); 37 | $private_key = openssl_pkey_get_private($this->privateKey); 38 | $fstr = array(); 39 | $array_data = $this->_splitEncode($data);//把要加密的信息 base64 encode后 等长放入数组 40 | foreach ($array_data as $value) {//理论上是可以只加密数组中的第一个元素 其他的不加密 因为只要一个解密不出来 整体也就解密不出来 这里先全部加密 41 | openssl_private_encrypt($value, $encrypted, $private_key); //私钥加密 42 | $fstr[] = $encrypted;//对数组中每个加密 43 | } 44 | return base64_encode(serialize($fstr));//序列化后base64_encode 45 | } 46 | 47 | /** 48 | * 公钥加密 49 | * @param string $data 要加密的数据 50 | * @return string 加密后的字符串 51 | */ 52 | public function publicKeyEncode($data) { 53 | $encrypted = ''; 54 | $this->_needKey(1); 55 | $public_key = openssl_pkey_get_public($this->publicKey); 56 | $fstr = array(); 57 | $array_data = $this->_splitEncode($data); 58 | foreach ($array_data as $value) { 59 | openssl_public_encrypt($value, $encrypted, $public_key); //私钥加密 60 | $fstr[] = $encrypted; 61 | } 62 | return base64_encode(serialize($fstr)); 63 | } 64 | 65 | /** 66 | * 用公钥解密私钥加密内容 67 | * @param string $data 要解密的数据 68 | * @return string 解密后的字符串 69 | */ 70 | public function decodePrivateEncode($data) { 71 | $decrypted = ''; 72 | $this->_needKey(1); 73 | $public_key = openssl_pkey_get_public($this->publicKey); 74 | $array_data = $this->_toArray($data);//数据base64_decode 后 反序列化成数组 75 | $str = ''; 76 | foreach ($array_data as $value){ 77 | openssl_public_decrypt($value, $decrypted, $public_key); //私钥加密的内容通过公钥可用解密出来 78 | $str .= $decrypted;//对数组中的每个元素解密 并拼接 79 | } 80 | return base64_decode($str);//把拼接的数据base64_decode 解密还原 81 | } 82 | 83 | /** 84 | * 用私钥解密公钥加密内容 85 | * @param string $data 要解密的数据 86 | * @return string 解密后的字符串 87 | */ 88 | public function decodePublicEncode($data) { 89 | $decrypted = ''; 90 | $this->_needKey(2); 91 | $private_key = openssl_pkey_get_private($this->privateKey); 92 | $array_data = $this->_toArray($data); 93 | $str = ''; 94 | foreach ($array_data as $value){ 95 | openssl_private_decrypt($value, $decrypted, $private_key); //私钥解密 96 | $str .= $decrypted; 97 | } 98 | return base64_decode($str); 99 | } 100 | 101 | /** 102 | * 检查是否 含有所需配置文件 103 | * @param int 1 公钥 2 私钥 104 | * @return int 1 105 | * @throws Exception 106 | */ 107 | private function _needKey($type) { 108 | switch ($type) { 109 | case 1: 110 | if (empty($this->publicKey)) { 111 | throw new Exception('请配置公钥'); 112 | } 113 | break; 114 | case 2: 115 | if (empty($this->privateKey)) { 116 | throw new Exception('请配置私钥'); 117 | } 118 | break; 119 | } 120 | return 1; 121 | } 122 | 123 | /** 124 | * 125 | * @param type $data 126 | * @return type 127 | */ 128 | private function _splitEncode($data) { 129 | $data = base64_encode($data); //加上base_64 encode 便于用于 分组 130 | $total_lenth = strlen($data); 131 | $per = 96;// 能整除2 和 3 RSA每次加密不能超过100个 132 | $dy = $total_lenth % $per; 133 | $total_block = $dy ? ($total_lenth / $per) : ($total_lenth / $per - 1); 134 | for ($i = 0; $i < $total_block; $i++) { 135 | $return[] = substr($data, $i * $per, $per);//把要加密的信息base64 后 按64长分组 136 | } 137 | return $return; 138 | } 139 | 140 | /** 141 | *公钥加密并用 base64 serialize 过的 data 142 | * @param type $data base64 serialize 过的 data 143 | */ 144 | private function _toArray($data){ 145 | $data = base64_decode($data); 146 | $array_data = unserialize($data); 147 | if(!is_array($array_data)){ 148 | throw new Exception('数据加密不符'); 149 | } 150 | return $array_data; 151 | } 152 | 153 | } --------------------------------------------------------------------------------