/crypt.php
PHP | 55 lines | 38 code | 11 blank | 6 comment | 4 complexity | e31ba220f9920cda1f727ed0d059d15c MD5 | raw file
- <?php
- function hexit ($str) {
- return bin2hex($str);
- }
- function binit ($str){
- return pack("H*",$str);
-
- }
- /*
- Hex to bin - bin to hex conversion example
- ===========================================
- */
- #echo hexit('brown fox'); echo '</br> binit: '; echo binit('62726f776e20666f78'); echo '</br>';
- #==========================================
- function mysql_aes_encrypt($val,$ky)
- {
- $key="2c3fec85b5f6b0a1cd5a1010c67eb772";
- for($a=0;$a<strlen($ky);$a++)
- $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
- $mode=MCRYPT_MODE_ECB;
- $enc=MCRYPT_RIJNDAEL_128;
- $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
- $result = mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
- return hexit($result);
- }
- function mysql_aes_decrypt($val,$ky)
- {
- $value = binit($val);
- $key="2c3fec85b5f6b0a1cd5a1010c67eb772";
- for($a=0;$a<strlen($ky);$a++)
- $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
- $mode = MCRYPT_MODE_ECB;
- $enc = MCRYPT_RIJNDAEL_128;
- $dec = @mcrypt_decrypt($enc, $key, $value, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
- return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null));
- }
- $key = $_POST['key'];
- $crypt = $_POST['tocrypt'];
- $encrypted = mysql_aes_encrypt ($crypt,$key);
- echo $encrypted;
- if($_POST['todecrypt']!==''){
- $decrypt = $_POST['todecrypt']; $decrypted = 'x-.-x'.mysql_aes_decrypt($decrypt,$key);
- echo $decrypted; }
- else {echo 'x-.-xDecrypted text will appear here.';}
- ?>