Reputation: 21
I'm trying to implement encryption and decryption functions in PHP but it is not just working right. Here is the algorithm:
1234567890
.a-z, A-Z, 0-9, =
Here is my current code:
function tnsencrypt($master_code,$text) { //the text to be encrypted $plain_text= $text;
//letters of alphabet array $alphabet=array('0','1','2','3','4','5','6','7','8','9','=','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
//$alphabet_len = count($alphabet); //$signature_len = 17; // signature=TnpMsgE //$master_code_len = 10; $mcursor = 0;
//positions of the letters in alphabet : The array_flip() function returns an array with all the original keys as values, and all original values as keys. $flip=array_flip($alphabet); //plaintext array $plain_text=str_split($plain_text); $master_code=str_split($master_code);
$n=count($plain_text); $encrypted_text=''; for ($i=0; $i }
}
//echo $encrypted_text; return $encrypted_text; }
function tnsdecrypt($master_code,$text) { //the text to be decrypted $encrypted_text= $text;
//letters of alphabet array $alphabet=array('0','1','2','3','4','5','6','7','8','9','=','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); //positions of the letters in alphabet : The array_flip() function returns an array with all the original keys as values, and all original values as keys. $flip=array_flip($alphabet);
//plaintext array $encrypted_text=str_split($encrypted_text); $master_code=str_split($master_code);
$n=count($encrypted_text); $decrypted_text=''; for ($i=0; $i
} else {
$decrypted_text.= $encrypted_text[$i];
}
//move mcursor
$mcursor = ($mcursor+1)%10;
}
//echo $encrypted_text; return $decrypted_text;
}
if(isset($_POST["text"])) { $text = $_POST["text"]; $shifttext = $_POST["shifttext"]; echo "
Encrypted Text: ".tnsencrypt($shifttext,$text); echo "
Decrypted Text: ".tnsdecrypt($shifttext,tnsencrypt($shifttext,$text));
}
?>
Here is my result:
Original text: val1=1234567 val2=abcdef val3=ABCDEF
Encrypted text: vjt8F666666F 3hr7Dddddnn 2gq7CCCCMMM
Encrypted Text: vjt8F666666F hr7Dddddnn gq7CCCCMMM
Decrypted Text: val1=1234567 al2=abcdef al3=ABCDEF
As you can see, a couple of characters are missing on the encryption and that affected the decryption as well. The missing ones are v on line 2 and v on line 3
Any ideas why?
Upvotes: 2
Views: 2516
Reputation: 3284
Why are you doing this? There are several one- and two-way encryption solutions you could be using instead, if this is for actual use and not just an academic exercise:
One-Way: crypt()
Two-Way: mcrypt
Encryption is pretty much a solved problem.
Upvotes: 1