Thursday, May 28, 2009

Encryption and Decryption function in 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 : Sumit Joshi
//Email : joshisumitnet@yahoo.com
//Date : 28-May-2009
$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

//Usage of above function

$Str_Test="This is a sample text which shows an Encryped and Decrypted code.";
ECHO $Str_Test."
";
$Str_Test2 = ENCRYPT_DECRYPT($Str_Test);
ECHO $Str_Test2."

";
$Str_Test3 = ENCRYPT_DECRYPT($Str_Test2);
ECHO "
".$Str_Test3."
";
?>

Convert Digits to Word in C#.NET 2008

//This methos converts Digits to Words
//Implementation is to just copy this method and use it.
//Pass any number and will get string in return.
//Example: 120345
//Output is :One Lac(s) Twenty Thousands Fourty Five Only

private string DigitToWord(long n)
{
string[] list = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" };
string[] pos = new string[] { "Arab", "Crore", "Lac(s)", "Thousand", "Hundred" };
int[] arr = new int[10];
int i = 0, j, k, count, opt = 1;
string strWord = "";
count = 0;
if (n == 0)
strWord = "zero";
else
{
j = 0;
while (n != 0)
{
arr[j] = (int)n % 10;
n = n / 10;
count++;
j++;
}
for (i = count - 1; i >= 0; i--)
{
k = (9 - i) / 2;
j = arr[i];
if (j == 0)
continue;
else
{
if (i > 2)
{
if (i % 2 != 0)
{
strWord += " " + list[j - 1];
strWord += " " + pos[k];
}
if (i % 2 == 0)
{
if (j < 2)
arr[i - 1] = (j * 10) + arr[i - 1];
else if (j != 1)
strWord += " " + list[17 + j];
}
}
else
{
if (i == 2)
strWord += " " + list[j - 1] + " hundred ";
else if (i == 1)
{
if (j == 1)
arr[i - 1] = (j * 10) + arr[i - 1];
else if (j != 1)
strWord += " " + list[17 + j];
}
else if (i == 0)
strWord += " " + list[j - 1];
}
}
}
}
return (strWord + " Only");
}
//End of Method