irwan

PHP Function to EncryptDecrypt a string without a known key

Apr 17th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. <?php
  2.  
  3. //The string itself has his own different key for every character
  4. FUNCTION ENCRYPT_DECRYPT($Str_Message) {
  5. //Function : encrypt/decrypt a string message v.1.0  without a known key
  6.     $Len_Str_Message=STRLEN($Str_Message);
  7.     $Str_Encrypted_Message="";
  8.     FOR ($Position = 0;$Position<$Len_Str_Message;$Position++){
  9.         // long code of the function to explain the algoritm
  10.         //this function can be tailored by the programmer modifyng the formula
  11.         //to calculate the key to use for every character in the string.
  12.         $Key_To_Use = (($Len_Str_Message+$Position)+1); // (+5 or *3 or ^2)
  13.         //after that we need a module division because can´t be greater than 255
  14.         $Key_To_Use = (255+$Key_To_Use) % 255;
  15.         $Byte_To_Be_Encrypted = SUBSTR($Str_Message, $Position, 1);
  16.         $Ascii_Num_Byte_To_Encrypt = ORD($Byte_To_Be_Encrypted);
  17.         $Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use;  //xor operation
  18.         $Encrypted_Byte = CHR($Xored_Byte);
  19.         $Str_Encrypted_Message .= $Encrypted_Byte;
  20.          
  21.         //short code of  the function once explained
  22.         //$str_encrypted_message .= chr((ord(substr($str_message, $position, 1))) ^ ((255+(($len_str_message+$position)+1)) % 255));
  23.     }
  24.     RETURN $Str_Encrypted_Message;
  25. } //end function
  26.  
  27. //sample use of the function
  28. $Str_Test="This function is free software; you can redistribute it and/or
  29. modify it under the terms of the GNU General Public License
  30. as published by the Free Software Foundation in any version
  31. of the License."."<br>"."This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."."<br>"."Hello Aitor, Wellcome Home"."<br>";
  34. ECHO $Str_Test."<br>";
  35. $Str_Test2 = ENCRYPT_DECRYPT($Str_Test);
  36. ECHO $Str_Test2."<br><br>";
  37. $Str_Test3 = ENCRYPT_DECRYPT($Str_Test2);
  38. ECHO "<br>".$Str_Test3."<br>";
  39.  
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment