i am trying to obfuscate/encrypt a number into string and then again want to de-ubfuscate/decrypt that string to get a number. 

i cant use base64 or any other public available algorithmbs as the generated hash is gonna be personal and specific to each user and cant be shared with other users, its for creating personal urls which will contains the personal hash.

basically i want to generate hash from the userid as userid is unique and doesnt changes.

so far i have created custom function to encrypt the userid and get hash from it.

```
<?php
$base_encryption_array = array(
'0'=>'b76',
'1'=>'d75',
'2'=>'f74',
'3'=>'h73',
'4'=>'j72',
'5'=>'l71',
'6'=>'n70',
'7'=>'p69',
'8'=>'r68',
'9'=>'t67',
'a'=>'v66',
'b'=>'x65',
'c'=>'z64',
'd'=>'a63',
'e'=>'d62',
'f'=>'e61',
'g'=>'h60',
'h'=>'i59',
'i'=>'j58',
'j'=>'g57',
'k'=>'f56',
'l'=>'c55',
'm'=>'b54',
'n'=>'y53',
'o'=>'w52',
'p'=>'u51',
'q'=>'s50',
'r'=>'q49',
's'=>'o48',
't'=>'m47',
'u'=>'k46',
'v'=>'i45',
'w'=>'g44',
'x'=>'e43',
'y'=>'c42',
'z'=>'a41'
);

function my_custom_encode($string){
global $base_encryption_array ;
$string = (string)$string;
$length = strlen($string);
$hash = '';
    for ($i=0; $i<$length; $i++) {
        if(isset($string[$i])){
            $hash .= $base_encryption_array[$string[$i]];
        }
    }
return $hash;
}

function my_custom_decode($hash){
global $base_encryption_array ;
/* this makes keys as values and values as keys */
$base_encryption_array = array_flip($base_encryption_array);

$hash = (string)$hash;
$length = strlen($hash);
$string = '';

    for ($i=0; $i<$length; $i=$i+3) {
        if(isset($hash[$i]) && isset($hash[$i+1]) && isset($hash[$i+2]) && isset($base_encryption_array[$hash[$i].$hash[$i+1].$hash[$i+2]])){
            $string .= $base_encryption_array[$hash[$i].$hash[$i+1].$hash[$i+2]];
        }
    }
return $string;
}?>

```

so far everything works fine.
encryption works fine for multiple levels / iterations.

bu the decryption of multiple iterated hash doesn't work, it only works for single encrypted value and not for 2 times encrypted values.

for eg.
my php function to create multiple iterated hash from userid, i am using 2 times encryption like this.

```
<?php
function userid_to_hash($userid){
    for($i = 1; $i <= 2; $i++) {
        $userid = my_custom_encode($userid);
    }

/* it returns hash*/
return $userid;
}?>

```

and my multiple iterations decode function

---

lets say userid = 41;

so when i do this 

```
<?php
$userid = 41;
echo userid_to_hash($userid)."<br>";
?>

```

i get output 

**g57p69f74a63p69l71**

but when i want to decrypt the hash and get userid i get blank space. like this 

```
<?php
$hash = 'g57p69f74a63p69l71';
echo hash_to_userid($hash);
?>

```

i get blank white page . but if i set the $hash to single level decryption like this and use the **custom function my\_custom\_decode** then it works fine for single iteration.

```
<?php
$hash = 't67';
echo my_custom_decode($hash);
?>

```

gives correct output .
**9**