I want to make a personal algorithm for hashing texts in PHP. The letter 'a' crypt in 'xyz', 'b' in '256' and some more. How it's possible this?
            Asked
            
        
        
            Active
            
        
            Viewed 297 times
        
    2 Answers
1
            
            
        It's possible by simple create a function that make characters substitution, like this:
function myEncrypt ($text)
{
    $text = str_replace(array('a', 'b'), array('xby', '256'), $text);
    // ... others
    return $text;
}
version with two arrays "search" and "replaceWith" passed as arguments:
function myEncrypt ($text, $search=array(), $replaceWith=array())
{
    return str_replace($search, $replaceWith, $text);   
}
WARNING: That way isn't a correct solution to encrypt a text, there are a lot of better ways to do a secure encryption with PHP (see for example this post).
 
    
    
        user2342558
        
- 5,567
- 5
- 33
- 54
0
            I'm bored at work so I thought i'd give this a crack. This isn't secure at all. The crypt must be hard coded and the crypted character must have a size of 3.
<?php
//define our character->crypted text
$cryptArray = array( "a"=>"xyz","b"=>"256");
//This is our input
$string = "aab";
//Function to crypt the string
function cryptit($string,$cryptArray){    
    //create a temp string
    $temp = "";     
    //pull the length of the input
    $length = strlen($string);       
    //loop thru the characters of the input
    for($i=0; $i<$length; $i++){       
        //match our key inside the crypt array and store the contents in the temp array, this builds the crypted output
        $temp .= $cryptArray[$string[$i]];
    }
    //returns the string
    return $temp;
} 
//function to decrypt
function decryptit($string,$cryptArray){
    $temp = "";
    $length = strlen($string);    
    //Swap the keys with data
    $cryptArray = array_flip($cryptArray);     
    //since our character->crypt is count of 3 we must $i+3 to get the next set to decrypt
    for($i =0; $i<$length; $i = $i+3){       
        //read from the key
        $temp .= $cryptArray[$string[$i].$string[$i+1].$string[$i+2]];
    }
    return $temp;
}
$crypted =  cryptit($string,$cryptArray);
echo $crypted;
$decrypted = decryptit($crypted,$cryptArray);
echo $decrypted;     
The input was : aab 
The output is:
xyzxyz256
aab
Here's the 3v4l link:
https://3v4l.org/chR2A
 
    
    
        IsThisJavascript
        
- 1,726
- 2
- 16
- 25
