<?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 : [email protected] //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>";
?>
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 : [email protected] //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
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 : [email protected] //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, $Position, 1);
$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_Simbolos, 1);
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 : [email protected] //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, $Position, 1);
if ($Byte_To_Be_Decoded > 0) {
$Plus_Modulo_Simbolos = $Byte_To_Be_Decoded;
$Position++;
$Byte_To_Be_Decoded = SUBSTR($Str_Message, $Position, 1);
}
//finding the position in the string
$SIMBOLOS
$Byte_Decoded = 0;
for ($SecondPosition = 0; $SecondPosition < $Len_Simbolos; $SecondPosition++) {
$Byte_To_Be_Compared = SUBSTR($SIMBOLOS, $SecondPosition, 1);
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.
sifou dztaem wrote :1905
Function beautiful and adequate, but it fails I have scripet.