├── LICENSE ├── README.md ├── bootstrap.php ├── composer.json ├── phpunit.xml ├── src └── Mcrypt │ ├── DesEncryptor.php │ ├── Exception.php │ └── String.php ├── test.php └── tests └── DesEncryptorTest.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 https://github.com/lizhibin205 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | lizhibin/php-mcrypt - Library 2 | ============= 3 | 4 | "PHP加密解密类库" 5 | 6 | Install via "composer require" 7 | ====== 8 | ```shell 9 | composer require lizhibin/php-mcrypt dev-master 10 | ``` 11 | 12 | Usage: 13 | ====== 14 | ```php 15 | $desEncrypt = new \Mcrypt\DesEncryptor("key", "iv"); 16 | ``` 17 | 18 | Example 1: (3Des Encrypt and Decrypt) 19 | ```php 20 | use Mcrypt\DesEncryptor; 21 | use Mcrypt\String; 22 | 23 | require 'bootstrap.php'; 24 | 25 | $desEncryptor = new DesEncryptor("1234567890123456", "12345678"); 26 | //设置加密key长度 27 | $desEncryptor->setKeySize(192); 28 | //设置加密方式,目前仅支持MCRYPT_MODE_CBC 29 | $desEncryptor->setMode(MCRYPT_MODE_CBC); 30 | //字符串补码,可设置为String::zeroPadding,String::pkcs5Padding,String::pkcs7Padding 31 | $desEncryptor->setPaddingMode(String::pkcs7Padding); 32 | 33 | $mcryptResult = $desEncryptor->encrypt3DES("hello world!"); 34 | echo base64_encode($mcryptResult), PHP_EOL; 35 | echo $desEncryptor->decrypt3DES($mcryptResult); 36 | //output: 37 | //tpD3+3PSR/Tx0saMLxJVUg== 38 | //hello world! 39 | ``` -------------------------------------------------------------------------------- /bootstrap.php: -------------------------------------------------------------------------------- 1 | =5.3.0" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "~4.0" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Mcrypt\\": "src/Mcrypt/" 26 | } 27 | }, 28 | "extra": { 29 | "branch-alias": { 30 | "dev-master": "1.0.0-dev" 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/Mcrypt/DesEncryptor.php: -------------------------------------------------------------------------------- 1 | key = $key; 53 | $this->iv = $iv; 54 | } 55 | 56 | /** 57 | * 设置算法模式 58 | * @param int $keySize 设置密钥长度,可选择的值有 59 | * MCRYPT_MODE_CBC 60 | */ 61 | public function setMode($mode) 62 | { 63 | $this->mode = $mode; 64 | } 65 | 66 | /** 67 | * 设置密钥长度 68 | * @param int $keySize 设置密钥长度 69 | */ 70 | public function setKeySize($keySize) 71 | { 72 | $this->keySize = $keySize; 73 | } 74 | 75 | /** 76 | * 设置补码方式 77 | * @param int $keySize 设置密钥长度 78 | */ 79 | public function setPaddingMode($paddingMode) 80 | { 81 | $this->paddingMode = $paddingMode; 82 | } 83 | 84 | 85 | /** 86 | * 对字符串进行3DES加密 87 | * @param string 要加密的字符串 88 | * @return mixed 加密成功返回加密后的字符串,否则返回false 89 | * @Exception Mcrypt\Exception 90 | */ 91 | public function encrypt3DES($str) 92 | { 93 | if (!in_array($this->mode, [MCRYPT_MODE_CBC], true)) { 94 | throw new Exception("加密模式不支持!"); 95 | } 96 | $td = mcrypt_module_open(MCRYPT_3DES, "", $this->mode, ""); 97 | if ($td === false) { 98 | throw new Exception("打开算法和模式对应的模块失败!"); 99 | } 100 | 101 | //检查加密key的长度是否符合算法要求 102 | $keyMaxSize = mcrypt_enc_get_key_size($td); 103 | if (strlen($this->key) > $keyMaxSize) { 104 | throw new Exception("Key长度不符合规范,必须小于{$keyMaxSize}字节!"); 105 | } 106 | $key = String::padding($this->key, $keyMaxSize, '0'); 107 | 108 | //检查加密iv的长度是否符合算法要求 109 | $ivMaxSize = mcrypt_enc_get_iv_size($td); 110 | if (strlen($this->iv) > $ivMaxSize) { 111 | throw new Exception("IV长度不符合规范,必须小于{$ivMaxSize}字节!"); 112 | } 113 | $iv = String::padding($this->iv, $ivMaxSize, '0'); 114 | 115 | //初始化加密所需的缓冲区 116 | if (mcrypt_generic_init($td, $key, $this->iv) !== 0) { 117 | throw new Exception("初始化加密所需的缓冲区失败!"); 118 | } 119 | 120 | //对$str进行分组处理 121 | $blockSize = mcrypt_enc_get_block_size($td); 122 | switch ($this->paddingMode) { 123 | case String::pkcs5Padding: 124 | $str = String::pkcs5Padding($str); 125 | break; 126 | case String::pkcs7Padding: 127 | $str = String::pkcs7Padding($str, $blockSize); 128 | break; 129 | default: 130 | $str = String::zeroPadding($str, $blockSize); 131 | } 132 | 133 | $result = mcrypt_generic($td, $str); 134 | mcrypt_generic_deinit($td); 135 | mcrypt_module_close($td); 136 | 137 | return $result; 138 | } 139 | 140 | /** 141 | * 对加密的字符串进行3DES解密 142 | * @param string 要解密的字符串 143 | * @return mixed 加密成功返回加密后的字符串,否则返回false 144 | */ 145 | public function decrypt3DES($str) 146 | { 147 | if (!in_array($this->mode, [MCRYPT_MODE_CBC], true)) { 148 | throw new Exception("加密模式不支持!"); 149 | } 150 | $td = mcrypt_module_open(MCRYPT_3DES, "", $this->mode, ""); 151 | if ($td === false) { 152 | throw new Exception("打开算法和模式对应的模块失败!"); 153 | } 154 | 155 | //检查加密key的长度是否符合算法要求 156 | $keyMaxSize = mcrypt_enc_get_key_size($td); 157 | if (strlen($this->key) > $keyMaxSize) { 158 | throw new Exception("Key长度不符合规范,必须小于{$keyMaxSize}字节!"); 159 | } 160 | $key = String::padding($this->key, $keyMaxSize, '0'); 161 | 162 | //检查加密iv的长度是否符合算法要求 163 | $ivMaxSize = mcrypt_enc_get_iv_size($td); 164 | if (strlen($this->iv) > $ivMaxSize) { 165 | throw new Exception("IV长度不符合规范,必须小于{$ivMaxSize}字节!"); 166 | } 167 | $iv = String::padding($this->iv, $ivMaxSize, '0'); 168 | 169 | if (mcrypt_generic_init($td, $key, $iv) !== 0) { 170 | throw new Exception("初始化加密所需的缓冲区失败!"); 171 | } 172 | 173 | $result = mdecrypt_generic($td, $str); 174 | $blockSize = mcrypt_enc_get_block_size($td); 175 | mcrypt_generic_deinit($td); 176 | mcrypt_module_close($td); 177 | 178 | switch ($this->paddingMode) { 179 | case String::pkcs5Padding: 180 | $result = String::unPkcs5Padding($result); 181 | break; 182 | case String::pkcs7Padding: 183 | $result = String::unPkcs7Padding($result, $blockSize); 184 | break; 185 | default: 186 | $result = rtrim($result, chr(0)); 187 | } 188 | 189 | return $result; 190 | } 191 | } -------------------------------------------------------------------------------- /src/Mcrypt/Exception.php: -------------------------------------------------------------------------------- 1 | $length) { 14 | return substr($string, 0, $length); 15 | } else if($strLength < $length) { 16 | return str_pad($string, $length, $char); 17 | } 18 | return $string; 19 | } 20 | 21 | /** 22 | * 使用0补齐字符串 23 | * @param string $string 24 | * @param int $blockSize 25 | * @return string 26 | */ 27 | public static function zeroPadding($string, $blockSize) 28 | { 29 | $strLength = strlen($string); 30 | $padding = $blockSize - ($strLength % $blockSize); 31 | return self::padding($string, $strLength + $padding, chr(0)); 32 | } 33 | 34 | /** 35 | * 使用pkcs5Padding补齐字符串,块固定是8位 36 | * @param string $string 37 | * @return string 38 | */ 39 | public static function pkcs5Padding($string) 40 | { 41 | $strLength = strlen($string); 42 | $padding = 8 - ($strLength % 8); 43 | return self::padding($string, $strLength + $padding, chr($padding)); 44 | } 45 | 46 | /** 47 | * 删除pkcs5Padding补齐字符串,块固定是8位 48 | * @param string $string 49 | * @return string 50 | */ 51 | public static function unPkcs5Padding($string) 52 | { 53 | $padding = ord($string{strlen($string) - 1}); 54 | return substr($string, 0, -1 * $padding); 55 | } 56 | 57 | /** 58 | * 使用pkcs7Padding补齐字符串,块固定是8位 59 | * @param string $string 60 | * @param int $blockSize 61 | * @return string 62 | */ 63 | public static function pkcs7Padding($string, $blockSize) 64 | { 65 | $strLength = strlen($string); 66 | $padding = $blockSize - ($strLength % $blockSize); 67 | return self::padding($string, $strLength + $padding, chr($padding)); 68 | } 69 | 70 | /** 71 | * 删除pkcs7Padding补齐字符串 72 | * @param string $string 73 | * @return string 74 | */ 75 | public static function unPkcs7Padding($string) 76 | { 77 | $padding = ord($string{strlen($string) - 1}); 78 | return substr($string, 0, -1 * $padding); 79 | } 80 | } -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | setKeySize(192); 9 | $desEncryptor->setMode(MCRYPT_MODE_CBC); 10 | $desEncryptor->setPaddingMode(String::pkcs7Padding); 11 | $mcryptResult = $desEncryptor->encrypt3DES("hello world!"); 12 | 13 | echo base64_encode($mcryptResult), PHP_EOL; 14 | 15 | echo $desEncryptor->decrypt3DES($mcryptResult); -------------------------------------------------------------------------------- /tests/DesEncryptorTest.php: -------------------------------------------------------------------------------- 1 | setKeySize($keySize); 11 | $desEncryptor->setMode($mode); 12 | $desEncryptor->setPaddingMode($paddingMode); 13 | $mcryptResult = $desEncryptor->encrypt3DES($str); 14 | $this->assertEquals(base64_encode($mcryptResult), $base64Result); 15 | 16 | $this->assertEquals($str, $desEncryptor->decrypt3DES($mcryptResult)); 17 | } 18 | 19 | public function additionEncryptProvider() 20 | { 21 | //array(key, iv, key_length, mode, paddingMode, string, result of 3des mcrypt and base64_encode) 22 | return [ 23 | ["1234567890123456", "12345678", 192, MCRYPT_MODE_CBC, Mcrypt\String::pkcs5Padding, "hello world!", "tpD3+3PSR/Tx0saMLxJVUg=="], 24 | ]; 25 | } 26 | } --------------------------------------------------------------------------------