├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── demo └── index.php ├── nbproject ├── project.properties └── project.xml └── src └── HemiFrame └── Lib └── AES.php /.gitignore: -------------------------------------------------------------------------------- 1 | /nbproject/private/ 2 | /vendor/ 3 | /composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Martin Bratvanov 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP class for encrypt and decrypt data with AES algorithm 2 | 3 | ## Supports 4 | - custom iv 5 | - auto generate iv 6 | - encode and decode data 7 | 8 | ## Requirements 9 | PHP >= 7.1 10 | 11 | ## Installation 12 | 13 | Enter in composer.json 14 | ```json 15 | { 16 | "require": { 17 | "hemiframe/php-aes": "~1.2" 18 | } 19 | } 20 | ``` 21 | 22 | or run in console: `php composer.phar require hemiframe/php-aes` 23 | 24 | 25 | ## Example 26 | 27 | ```php 28 | require_once('vendor/autoload.php'); 29 | 30 | $aes = new \HemiFrame\Lib\AES(); 31 | $aes->setKey("testkey"); 32 | $aes->setData("testdata"); 33 | 34 | $ivString = $aes->generateIv(); 35 | $aes->setIv($ivString); 36 | 37 | $encryptedString = $aes->encrypt(); 38 | $aes->setData($encryptedString); 39 | $decryptedString = $aes->decrypt(); 40 | 41 | echo "Available methods: " . implode(",", \HemiFrame\Lib\AES::getAvailableMethods()) . "

" . PHP_EOL; 42 | echo "IV string: " . $ivString . "
" . PHP_EOL; 43 | echo "Encrypted string: " . $encryptedString . "
" . PHP_EOL; 44 | echo "Decrypted string: " . $decryptedString . "
" . PHP_EOL; 45 | ``` 46 | 47 | ## Documentation 48 | 49 | ### Methods 50 | 51 | ```php 52 | /** 53 | * Get encrypt/decrypt key 54 | * @return string|null 55 | */ 56 | public function getKey(): string 57 | 58 | /** 59 | * Return encoded or decoded string 60 | * @return mixed 61 | */ 62 | public function getData(); 63 | 64 | /** 65 | * Get selected cipher method 66 | * @return string 67 | */ 68 | public function getMethod(): string 69 | 70 | /** 71 | * Get available cipher methods 72 | * @return array 73 | */ 74 | public function getAvailableMethods(bool $aliases = false): array 75 | 76 | /** 77 | * Gets selected iv string 78 | * @return string 79 | */ 80 | public function getIv(): string 81 | 82 | /** 83 | * Generate a iv string for selected method 84 | * @return string|bool 85 | */ 86 | public function generateIv(); 87 | 88 | /** 89 | * Gets the cipher iv length 90 | * @return int|bool 91 | */ 92 | public function getIvLength(); 93 | 94 | /** 95 | * Set encrypt/decrypt key 96 | * @param string $key 97 | * @return $this 98 | * @throws \InvalidArgumentException 99 | */ 100 | public function setKey(string $key): self 101 | 102 | /** 103 | * Set encrypt/decrypt method 104 | * @param string $method 105 | * @return $this 106 | * @throws \InvalidArgumentException 107 | */ 108 | public function setMethod(string $method); 109 | 110 | /** 111 | * Set iv string 112 | * @param string $iv 113 | * @return $this 114 | * @throws \InvalidArgumentException 115 | */ 116 | public function setIv(string $iv): self 117 | 118 | /** 119 | * Set encrypt/decrypt data 120 | * @param mixed $data 121 | * @return $this 122 | * @throws \InvalidArgumentException 123 | */ 124 | public function setData($data): self 125 | 126 | /** 127 | * Returns the encrypted string on success or FALSE on failure. 128 | * @return string|bool 129 | * @throws \InvalidArgumentException 130 | */ 131 | public function encrypt(); 132 | 133 | /** 134 | * The decrypted string on success or FALSE on failure. 135 | * @return mixed 136 | * @throws \InvalidArgumentException 137 | */ 138 | public function decrypt(); 139 | ``` 140 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hemiframe/php-aes", 3 | "description": "PHP class for encrypt and decrypt data with AES algorithm", 4 | "version": "1.2.1", 5 | "keywords": [ 6 | "encrypt", 7 | "decrypt", 8 | "AES", 9 | "PHP", 10 | "openssl" 11 | ], 12 | "license": "MIT", 13 | "authors": [{ 14 | "name": "Martin", 15 | "email": "heminei@heminei.com", 16 | "role": "Developer" 17 | }], 18 | "support": { 19 | "issues": "https://github.com/heminei/php-aes/issues" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "HemiFrame\\": "src/HemiFrame" 24 | } 25 | }, 26 | "require": { 27 | "php": ">=7.1" 28 | } 29 | } -------------------------------------------------------------------------------- /demo/index.php: -------------------------------------------------------------------------------- 1 | setKey("testkey"); 7 | $aes->setData("testdata"); 8 | 9 | $ivString = $aes->generateIv(); 10 | $aes->setIv($ivString); 11 | 12 | $encryptedString = $aes->encrypt(); 13 | $aes->setData($encryptedString); 14 | $decryptedString = $aes->decrypt(); 15 | 16 | echo "Available methods: " . implode(",", \HemiFrame\Lib\AES::getAvailableMethods()) . "

" . PHP_EOL; 17 | echo "IV string: " . $ivString . "
" . PHP_EOL; 18 | echo "Encrypted string: " . $encryptedString . "
" . PHP_EOL; 19 | echo "Decrypted string: " . $decryptedString . "
" . PHP_EOL; 20 | -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | include.path=${php.global.include.path} 2 | php.version=PHP_70 3 | source.encoding=UTF-8 4 | src.dir=. 5 | tags.asp=false 6 | tags.short=false 7 | web.root=. 8 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.php.project 4 | 5 | 6 | php-aes 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/HemiFrame/Lib/AES.php: -------------------------------------------------------------------------------- 1 | setData($data); 23 | } 24 | if ($key !== null) { 25 | $this->setKey($key); 26 | } 27 | $this->setMethod($method); 28 | } 29 | 30 | /** 31 | * Get encrypt/decrypt key 32 | * @return string|null 33 | */ 34 | public function getKey(): ?string 35 | { 36 | return $this->key; 37 | } 38 | 39 | /** 40 | * Return encoded or decoded string 41 | * @return mixed 42 | */ 43 | public function getData() 44 | { 45 | return $this->data; 46 | } 47 | 48 | /** 49 | * Get selected cipher method 50 | * @return string 51 | */ 52 | public function getMethod(): string 53 | { 54 | return $this->method; 55 | } 56 | 57 | /** 58 | * Get available cipher methods 59 | * @return array 60 | */ 61 | public static function getAvailableMethods(bool $aliases = false): array 62 | { 63 | return openssl_get_cipher_methods($aliases); 64 | } 65 | 66 | /** 67 | * Gets selected iv string 68 | * @return string 69 | */ 70 | public function getIv(): string 71 | { 72 | return $this->iv; 73 | } 74 | 75 | /** 76 | * Generate a iv string for selected method 77 | * @return string|bool 78 | */ 79 | public function generateIv() 80 | { 81 | return openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method)); 82 | } 83 | 84 | /** 85 | * Gets the cipher iv length 86 | * @return int|bool 87 | */ 88 | public function getIvLength() 89 | { 90 | return openssl_cipher_iv_length($this->method); 91 | } 92 | 93 | /** 94 | * Set encrypt/decrypt key 95 | * @param string $key 96 | * @return $this 97 | * @throws \InvalidArgumentException 98 | */ 99 | public function setKey(string $key): self 100 | { 101 | if (empty($key)) { 102 | throw new \InvalidArgumentException("Key is empty."); 103 | } 104 | $this->key = $key; 105 | return $this; 106 | } 107 | 108 | /** 109 | * Set encrypt/decrypt method 110 | * @param string $method 111 | * @return $this 112 | * @throws \InvalidArgumentException 113 | */ 114 | public function setMethod(string $method): self 115 | { 116 | $method = strtolower($method); 117 | 118 | if (!in_array($method, $this->getAvailableMethods())) { 119 | throw new \InvalidArgumentException("The method is not available"); 120 | } 121 | $this->method = $method; 122 | return $this; 123 | } 124 | 125 | /** 126 | * Set iv string 127 | * @param string $iv 128 | * @return $this 129 | * @throws \InvalidArgumentException 130 | */ 131 | public function setIv(string $iv): self 132 | { 133 | if (strlen($this->getIv()) != $this->getIvLength()) { 134 | throw new \InvalidArgumentException("Iv lenght must be " . $this->getIvLength()); 135 | } 136 | $this->iv = $iv; 137 | 138 | return $this; 139 | } 140 | 141 | /** 142 | * Set encrypt/decrypt data 143 | * @param mixed $data 144 | * @return $this 145 | * @throws \InvalidArgumentException 146 | */ 147 | public function setData($data): self 148 | { 149 | if (empty($data)) { 150 | throw new \InvalidArgumentException("Data is empty."); 151 | } 152 | $this->data = $data; 153 | return $this; 154 | } 155 | 156 | /** 157 | * Returns the encrypted string on success or FALSE on failure. 158 | * @return string|bool 159 | * @throws \InvalidArgumentException 160 | */ 161 | public function encrypt() 162 | { 163 | if (empty($this->getKey())) { 164 | throw new \InvalidArgumentException("Please set key."); 165 | } 166 | if (empty($this->getData())) { 167 | throw new \InvalidArgumentException("Please set data."); 168 | } 169 | return openssl_encrypt($this->data, $this->method, $this->key, 0, $this->getIv()); 170 | } 171 | 172 | /** 173 | * The decrypted string on success or FALSE on failure. 174 | * @return mixed 175 | * @throws \InvalidArgumentException 176 | */ 177 | public function decrypt() 178 | { 179 | if (empty($this->getKey())) { 180 | throw new \InvalidArgumentException("Please set key."); 181 | } 182 | if (empty($this->getData())) { 183 | throw new \InvalidArgumentException("Please set data."); 184 | } 185 | return openssl_decrypt($this->data, $this->method, $this->key, 0, $this->getIv()); 186 | } 187 | } 188 | --------------------------------------------------------------------------------