Contact Us

Scripts: PHP :: PHP security and encryption >> Library Article #4

Developer's Section

Encrypt a string in PHP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2691)

Learn a simple way to encrypt a string using php.

In this article you will learn how to encrypt a string in php. The following algorithm uses the xor operator to encrypt every character in a string. Finally, It decrypts the encrypted string by taking the encrypted string and xor it with the bytes characters the other way around.

 Code Snippet 1

    function encryptDecrypt($Str_Message)

    FOR ($Position = 0;$Position<$Len_Str_Message;$Position++){
        $Key_To_Use = (($Len_Str_Message+$Position)+1); 
        $Key_To_Use = (255+$Key_To_Use) % 255;
        $Byte_To_Be_Encrypted = SUBSTR($Str_Message, $Position, 1);
        $Ascii_Num_Byte_To_Encrypt = ORD($Byte_To_Be_Encrypted);
        //uses xor operator
        $Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use;  
        $Encrypted_Byte = CHR($Xored_Byte);
        $Str_Encrypted_Message .= $Encrypted_Byte;

    return $Str_Encrypted_Message; 

See other Scripts in PHP security and encryption

Submit Your Scripts:

If you would like to have your PHP scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]