Stay at home discount

PHP Encryption For Generating Fixed Length Encryption Strings

September 13, 2020 - By 

Let’s start with the actual PHP function:

1. function XOREncryption($InputString, $KeyPhrase){


3.     $KeyPhraseLength = strlen($KeyPhrase);


5.     // Loop trough input string

6.     for ($i = 0; $i //SALT

$salt = ‘my_special_phrase’;


$crypted = base64_encode(XOREncryption(‘my string’, $salt));

echo “Encrypted: ” . $crypted . “

 The variable ‘ $salt ‘ plays a crucial role here.  It is essentially the ‘key’ to your encrypted string. The salt value will always generate the same values when the same mathematics are applied to it.  It provides the constant needed to generate randomness.  This goes to say that if someone uses the salt that was used to generate the encryption and applies the same mathematics they can undo or reverse the encrypted string back into the original string text.

In order to decrypt an encrypted string that was generated by using this function use the invoccation code below:


$salt = ‘my_special_phrase’;


$decrypted  = XOREncryption(base64_decode($crypted), $salt);

echo “Decrypted: ” . $decrypted;

As long as I use the same salt I can reverse an encrypted string that was encrypted with this function.

Notify of
Inline Feedbacks
View all comments

Leave a Reply

Do NOT follow this link or you will be banned from the site!

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

I accept I decline Privacy Center
Would love your thoughts, please comment.x