WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character.
Categories : PHP, Algorithms, Security, Authentication, Encryption
aitor solozabal
Date : Mar 27th 2005
Grade : 4 of 5 (graded 9 times)
Viewed : 37163
File : No file for this code example.
Images : No Images for this code example.
Search : More code by aitor solozabal
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?PHP
/*
Description : A function with a very simple but powerful xor method to encrypt
              and/or decrypt a string with an unknown key. Implicitly the key is
              defined by the string itself in a character by character way.
              There are 4 items to compose the unknown key for the character
              in the algorithm
              1.- The ascii code of every character of the string itself
              2.- The position in the string of the character to encrypt
              3.- The length of the string that include the character
              4.- Any special formula added by the programmer to the algorithm
                  to calculate the key to use
*/
FUNCTION ENCRYPT_DECRYPT($Str_Message) {
//Function : encrypt/decrypt a string message v.1.0  without a known key
//Author   : Aitor Solozabal Merino (spain)
//Email    : aitor-3@euskalnet.net
//Date     : 01-04-2005
   
$Len_Str_Message=STRLEN($Str_Message);
   
$Str_Encrypted_Message="";
    FOR (
$Position = 0;$Position<$Len_Str_Message;$Position++){
       
// long code of the function to explain the algoritm
        //this function can be tailored by the programmer modifyng the formula
        //to calculate the key to use for every character in the string.
       
$Key_To_Use = (($Len_Str_Message+$Position)+1); // (+5 or *3 or ^2)
        //after that we need a module division because can´t be greater than 255
       
$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);
       
$Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use//xor operation
       
$Encrypted_Byte = CHR($Xored_Byte);
       
$Str_Encrypted_Message .= $Encrypted_Byte;
       
       
//short code of  the function once explained
        //$str_encrypted_message .= chr((ord(substr($str_message, $position, 1))) ^ ((255+(($len_str_message+$position)+1)) % 255));
   
}
    RETURN
$Str_Encrypted_Message;
}
//end function
?>



sample use of the function
$Str_Test="This function is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation in any version
of the License."."<br>"."This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."."<br>"."Hello Aitor, Wellcome Home"."<br>";
ECHO $Str_Test."<br>";
$Str_Test2 = ENCRYPT_DECRYPT($Str_Test);
ECHO $Str_Test2."<br><br>";
$Str_Test3 = ENCRYPT_DECRYPT($Str_Test2);
ECHO "<br>".$Str_Test3."<br>";
?>



MD5 secured login
Categories : PHP, Java Script, Authentication, Security
Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags.
Categories : PHP, PHP Classes, Algorithms, Security
Simple Password example
Categories : PHP, Authentication, Security, HTTP
A PHP function to encrypt and decrypt a number or string or a combination of the two.
Categories : PHP, Encryption, Security
A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security
SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge response login algorithms over an insecure connection.
Categories : Algorithms, PHP, Security
A simple PHP login script that you can modify to suite your needs. It use a session to store data in a session file submited by the page.
Categories : PHP, Sessions, Security, Authentication
Form Security - Match A Value For Success
Categories : PHP, Authentication, HTML and PHP, Sessions, Security
Scramble Eggs - php class to scramble/encode
Categories : PHP, PHP Classes, Security, Encryption
Password using php, Javascript, and html form
Categories : Security, PHP, Authentication, Java Script
what salt do I have to feed the crypt function with to make it work like the htpasswd command of apache?
Categories : Algorithms, PHP, Authentication
Authenticator for Exchange Server LDAP
Categories : PHP, Authentication, LDAP, Security, Sessions
phpSecurePages is a PHP module to secures pages with a loginname and password. It handles multiple user groups (each has own viewing rights), store data in a MySQL database or a configuration file, and can be used to identify Web site viewers.
Categories : PHP, Security, Authentication
Use of bitmasks to represent permissions
Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes
Password protection for Phorum 3.1.x with userlevels and log.
Categories : PHP, MySQL, Authentication, Security
 aitor solozabal wrote : 1299
This function is very useful when you need to send any
encrypted message to other application built with other 
programming language or viceversa, via ascii txt files or 
any other way because the algorithm is very easy to 
implement in other programming language than PHP (Visual 
Basic, C++, Pascal, etc..) and it is not necessary to send 
the key to decrypt the message apart.
 
 aitor solozabal merino wrote : 1719
In conjunction with the previous sample there is a visual basic counterpart to send 
encrypted message between two applications in different computer languages

Function Encrypt_Decrypt(Str_Message As String) As String
    Dim Len_Str_Message As Long
    Dim Position As Long
    Dim Key_To_Use As Long
    Dim Byte_To_Be_Encrypted As String
    Dim Ascii_Num_Byte_To_Encrypt As Integer
    Dim Xored_Byte As String
    Dim Encrypted_Byte As String
    Dim Str_Encrypted_Message As String
    Dim SIMBOLOS As String
   On Error GoTo Encrypt_Decrypt_Error

    SIMBOLOS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Len_Str_Message = Len(Str_Message)
    Str_Encrypted_Message = ""
    For Position = 1 To Len_Str_Message
        Key_To_Use = ((Len_Str_Message + Position) + 1)   ` (+5 or *3 or ^2)
        `after that we need a module division because can´t be greater than 255
        Key_To_Use = ((255 + Key_To_Use) Mod 255)
        Byte_To_Be_Encrypted = Mid(Str_Message, Position, 1)
        Ascii_Num_Byte_To_Encrypt = Asc(Byte_To_Be_Encrypted)
        Xored_Byte = Ascii_Num_Byte_To_Encrypt Xor Key_To_Use  `xor operation
        `Encrypted_Byte = Chr(Xored_Byte)
        Xored_Byte = Xored_Byte Mod Len(SIMBOLOS)
        If Xored_Byte <> 0 Then
            Encrypted_Byte = Mid(SIMBOLOS, Xored_Byte, 1)
        Else
            Encrypted_Byte = "0"
        End If
        Str_Encrypted_Message = Str_Encrypted_Message + Encrypted_Byte
    Next Position

    Encrypt_Decrypt = UCase(Str_Encrypted_Message)

   On Error GoTo 0
   Exit Function

Encrypt_Decrypt_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Encrypt_Decrypt of Módulo XXXXXXXX"

End Function
 
 aitor solozabal merino wrote : 1720
In conjunction with the previous sample there is a visual 
basic counterpart to send encrypted message between two 
applications in different computer languages

Function Encrypt_Decrypt(Str_Message As String) As String
    Dim Len_Str_Message As Long
    Dim Position As Long
    Dim Key_To_Use As Long
    Dim Byte_To_Be_Encrypted As String
    Dim Ascii_Num_Byte_To_Encrypt As Integer
    Dim Xored_Byte As String
    Dim Encrypted_Byte As String
    Dim Str_Encrypted_Message As String
    Dim SIMBOLOS As String
   On Error GoTo Encrypt_Decrypt_Error

    SIMBOLOS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Len_Str_Message = Len(Str_Message)
    Str_Encrypted_Message = ""
    For Position = 1 To Len_Str_Message
        Key_To_Use = ((Len_Str_Message + Position) + 1)   ` (+5 or *3 or ^2)
        `after that we need a module division because can´t be greater than 255
        Key_To_Use = ((255 + Key_To_Use) Mod 255)
        Byte_To_Be_Encrypted = Mid(Str_Message, Position, 1)
        Ascii_Num_Byte_To_Encrypt = Asc(Byte_To_Be_Encrypted)
        Xored_Byte = Ascii_Num_Byte_To_Encrypt Xor Key_To_Use  `xor operation
        `Encrypted_Byte = Chr(Xored_Byte)
        Xored_Byte = Xored_Byte Mod Len(SIMBOLOS)
        If Xored_Byte <> 0 Then
            Encrypted_Byte = Mid(SIMBOLOS, Xored_Byte, 1)
        Else
            Encrypted_Byte = "0"
        End If
        Str_Encrypted_Message = Str_Encrypted_Message + Encrypted_Byte
    Next Position

    Encrypt_Decrypt = UCase(Str_Encrypted_Message)

   On Error GoTo 0
   Exit Function

Encrypt_Decrypt_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Encrypt_Decrypt of Módulo XXXXXXXX"

End Function
 
 aitor solozabal merino wrote :1721
Please, you forget the last 2 comments because they contain an error in the example.
On the other hand you can use the last one who sent you corrected.
To excuse for any disadvantage that it had caused. 

<?PHP 
    
/* 
    Description : A function with a very simple but powerful xor method to encrypt and/or decrypt a string 
    with an unknown key. Implicitly the key is defined by the string itself in a character by character way. 
    There are 4 items to compose the unknown key for the character in the algorithm 1.- The ascii code of every 
    character of the string itself 2.- The position in the string of the character to encrypt 3.- The length of 
    the string that include the character 4.- Any special formula added by the programmer to the algorithm to 
    calculate the key to use 
    */  
    
function ENCRYPT_DECRYPT($Str_Message) {     
        
//Function : encrypt/decrypt a     string message v.1.0  without a known key     
        //Author   : Aitor Solozabal Merino (spain)     
        //Email    : aitor-3@euskalnet.net     
        //Date     : 01-04-2005     
        
$Len_Str_Message STRLEN($Str_Message);     
        
$Str_Encrypted_Message "";     
        for (
$Position 0$Position $Len_Str_Message$Position++) {         
            
// long code of the function to  explain the algoritm         
            //this function can be tailored by the programmer modifyng the formula         
            //to calculate the key to use for every character in the string.         
            
$Key_To_Use = (($Len_Str_Message $Position) + 1); 
            
// (+5 or *3 or ^2)         
            //after that we need a module division because can´t be greater than 255         
            
$Key_To_Use = (255 $Key_To_Use) % 255;         
            
$Byte_To_Be_Encrypted SUBSTR($Str_Message$Position1);         
            
$Ascii_Num_Byte_To_Encrypt ORD($Byte_To_Be_Encrypted);         
            
$Xored_Byte $Ascii_Num_Byte_To_Encrypt $Key_To_Use
            
//xor operation         
            
$Encrypted_Byte CHR($Xored_Byte);         
            
$Str_Encrypted_Message .= $Encrypted_Byte;          
            
//short code of  the function once explained         
            //$str_encrypted_message .= chr((ord(substr($str_message, $position, 1))) ^ (255+(($len_str_message+$position)+1)) % 255));     
        
}     
        return 
$Str_Encrypted_Message
    } 
//end function  
    
    
function ENCODE($Str_Message,$SIMBOLOS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") {     
        
//Functions : encode/decode a string message v.1.0     
        // use in conjunction with encrypt/decrypt     
        // There is a string $SIMBOLOS defined by default but     
        // $SIMBOLOS can be defined on the fly by the function caller     
        // the three conditions for the string $SIMBOLOS are     
        // 1.- can not contain numbers 0,1,2,3,4,5,6,7,8,9 because are part of the algorithm     
        // 2.- the length of string must be greater than 25 bytes     
        // 3.- The string must be the same to encode or to decode     
        //Author   : Aitor Solozabal Merino (spain)     
        //Email    : aitor-3@euskalnet.net     
        //Date     : 07-10-2007      $Len_Simbolos = strlen($SIMBOLOS); 
         
if $Len_Simbolos <26 ERROR     $Len_Str_Message strlen($Str_Message);     
        
$Str_Encoded_Message "";     
        for (
$Position 0$Position $Len_Str_Message$Position++) {         
            
$Byte_To_Be_Encoded SUBSTR($Str_Message$Position1);         
            
$Ascii_Num_Byte_To_Encode ORD($Byte_To_Be_Encoded);         
            
$Rest_Modulo_Simbolos = ($Ascii_Num_Byte_To_Encode $Len_Simbolos) % $Len_Simbolos;         
            
$Plus_Modulo_Simbolos = (int)($Ascii_Num_Byte_To_Encode $Len_Simbolos);         
            
$Encoded_Byte SUBSTR($SIMBOLOS$Rest_Modulo_Simbolos1);         
            if (
$Plus_Modulo_Simbolos == 0) {             
                
$Str_Encoded_Message .= $Encoded_Byte;         
            }else {             
                
$Str_Encoded_Message .= $Plus_Modulo_Simbolos $Encoded_Byte;         
            }     
        }     
        return 
$Str_Encoded_Message
    } 
//end function  
    
function DECODE($Str_Message,$SIMBOLOS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ) {     
        
//Functions : encode/decode a string message v.1.0     
        // use in conjunction with encrypt/decrypt     
        // There is a string $SIMBOLOS defined by default but     
        // $SIMBOLOS can be defined on the fly by the function caller     
        // the three conditions for the string $SIMBOLOS are     
        // 1.- can not contain numbers 0,1,2,3,4,5,6,7,8,9 because are part of the algorithm     
        // 2.- the length of string must be greater than 25 bytes     
        // 3.- The string must be the same to encode or to decode     
        //Author   : Aitor Solozabal Merino (spain)     
        //Email    : aitor-3@euskalnet.net     
        //Date     : 07-10-2007      $Len_Simbolos = strlen($SIMBOLOS);  
         
if $Len_Simbolos <26 ERROR     $Len_Str_Message strlen($Str_Message);     
        
$Str_Decoded_Message "";     
        for (
$Position 0$Position $Len_Str_Message$Position++) {         
            
$Plus_Modulo_Simbolos 0;         
            
$Byte_To_Be_Decoded SUBSTR($Str_Message$Position1);         
            if (
$Byte_To_Be_Decoded 0) {             
                
$Plus_Modulo_Simbolos $Byte_To_Be_Decoded;             
                
$Position++;             
                
$Byte_To_Be_Decoded SUBSTR($Str_Message$Position1);         
            }         
            
//finding the position in the string 
            
$SIMBOLOS         
            $Byte_Decoded 
0;         
            for (
$SecondPosition 0$SecondPosition $Len_Simbolos$SecondPosition++) {             
                
$Byte_To_Be_Compared SUBSTR($SIMBOLOS$SecondPosition1);             
                if (
$Byte_To_Be_Decoded == $Byte_To_Be_Compared) {                 
                    
$Byte_Decoded $SecondPosition;             
                }         
            }         
            
$Byte_Decoded = ($Plus_Modulo_Simbolos $Len_Simbolos) + $Byte_Decoded;         
            
$Ascii_Num_Byte_To_Decode CHR($Byte_Decoded);         
            
$Str_Decoded_Message .= $Ascii_Num_Byte_To_Decode;     
        }     
        return 
$Str_Decoded_Message
    } 
//end function 
    
    //sample use of the function 
    
ECHO "BEGIN OF PROCESS <br>";     
    echo 
"------------------------------------------------------------------------------------------<BR>"
    
$Str_Test "This function is free software; you can redistribute it and/or modify it under the 
    terms of the GNU General Public License as published by the Free Software Foundation in any version 
    of the License." 
"<br>" .     "This program is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
    PARTICULAR PURPOSE." 
"<br>" .     "Hello Aitor, Wellcome Home" "<br>"
    echo 
$Str_Test "<br>"
    echo 
"-------------------------------------------------------------------------------------------<BR>"
    
$Str_Test2 ENCRYPT_DECRYPT($Str_Test); 
    echo 
$Str_Test2 "<br>"
    echo 
"<br>---------------------------------------------------------------------------------------<BR>"
    
$Str_Test3 ENCODE($Str_Test2); // or ENCODE($Str_Test2,"ABCDEFGHIJKLMNOPQRSTUVWXYZ") 
    
echo $Str_Test3 "<br>"
    echo 
"-------------------------------------------------------------------------------------------<BR>"
    
$Str_Test4 DECODE($Str_Test3); // or DECODE($Str_Test3,"ABCDEFGHIJKLMNOPQRSTUVWXYZ") 
    
echo $Str_Test4 "<br>"
    echo 
"<br>---------------------------------------------------------------------------------------<BR>"
    
$Str_Test5 ENCRYPT_DECRYPT($Str_Test4); 
    echo 
$Str_Test5 "<br>"
    echo 
"-------------------------------------------------------------------------------------------<BR>"
    echo 
"END OF PROCESS <BR>";  
    
?>  
    That's all.