pphillips
09-17-2004, 05:04 PM
Does anyone know a simple encryption/decryption algorithm (in PHP) that encrypts plaintext into printable characters and where the encrypted string isn't really long? I've been looking everywhere and can't find what I need. RC4, Blowfish, and other mcrypt algorithms do not encrypt into 100% printable characters. The output of md5() presents a nice looking, short, printable hash, but of course it isn't reversable.
xelav
09-17-2004, 05:28 PM
Simple use UUE encoding for output text
pphillips
09-17-2004, 06:00 PM
Is this the same as PHP functions convert_uuencode and convert_uudecode? If so, is there anyway to provide a salt to it so that nobody could unencrypt it without the key?
marshallm
09-17-2004, 11:36 PM
You can have a look at the functions I use to encrypt URL variables. Have a look here (http://www.webhostingtalk.com/showthread.php?s=&threadid=316246&perpage=15&pagenumber=1) and maybe you'll get some ideas. I modified the code a few times, so make sure if you're going to try it you scroll all the way to the end.
With the mcrypt stuff, as with any type of encryption, the math is done at the binary level, so the file is non ASCII when it's encrypted. You if you want it to be ASCII you need to do a BIN2HEX() on it. But then of course you need to do a HEX2BIN() to get it back... but of course there is no HEX2BIN. But you'll find one that you can use in the code I posted in the thread above. You'll also find some routines that should help get you started with encrypting and decrypting.
Good luck, and let me know if you have any questions about my code.
Mark
pphillips
09-18-2004, 04:38 AM
Thanks for the code sample! Unfortunately most PHP installations dont have mcrypt installed, but I'm going to play around with your idea of using bin2hex with an RC4 algorithm and I'll take a look at your hex2bin for the reversal! I think that will work wonders. Thanks!
pphillips
09-18-2004, 04:55 AM
It works!
Encrypted: �%T��y4�mBYIr���E�V��^���
Encrypted bin2hex: 9a2554bcdb7934a86d0742594972c4f884fd89458756a0e95e8401aa86
Decrypted hex2bin: �%T��y4�mBYIr���E�V��^���
Decrypted Final: this is my plain text string!
Below is my code. I know someone will come by this thread and wish I posted it sometime in the future, so here it is. I know its messy but it works. The abcdefg in the second line of the RC4 function is the key.
function RC4($data) {
$pwd = implode('', array("abcdefg"));
$pwd_length = strlen($pwd);
for ($i = 0; $i < 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
$counter[$i] = $i;
}
for ($i = 0; $i < 255; $i++) {
$x = ($x + $counter[$i] + $key[$i]) % 256;
$temp_swap = $counter[$i];
$counter[$i] = $counter[$x];
$counter[$x] = $temp_swap;
}
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $counter[$a]) % 256;
$temp = $counter[$a];
$counter[$a] = $counter[$j];
$counter[$j] = $temp;
$k = $counter[(($counter[$a] + $counter[$j]) % 256)];
$Zcipher = ord(substr($data, $i, 1)) ^ $k;
$Ztest .= "$Zcipher " . chr($Zcipher) . " ";
$Zcrypt .= chr($Zcipher);
}
return $Zcrypt;
}
function hex2bin($data) {
$len = strlen($data);
return pack("H" . $len, $data);
}
$encrypted = RC4("this is my plain text string!");
echo "Encrypted: $encrypted<br>";
$encryptedbin2hex = bin2hex($encrypted);
echo "Encrypted bin2hex: $encryptedbin2hex<br>";
$decryptedhex2bin = hex2bin($encryptedbin2hex);
echo "Decrypted hex2bin: $decryptedhex2bin<br>";
$decrypted = RC4($decryptedhex2bin);
echo "Decrypted Final: $decrypted<br>";
sea otter
09-18-2004, 05:43 AM
Thanks for posting the code! I've been following the thread, and I know I've found this useful. I've added it to my code snippet library (with proper attribution to you and marshallm).
I've used blowfish in the past, but I like the printable character aspect of this one.
mwaseem
09-18-2004, 06:29 AM
Try http://sourceforge.net. Hopefully you'll find more great snippets there.
sea otter
09-18-2004, 06:41 AM
Originally posted by mwaseem
Try http://sourceforge.net. Hopefully you'll find more great snippets there.
Oh yes, I know it well. :cool:
But it's also nice to find something useful staring you in the face on WHT, where I seem to be spending way too much time lately ;)
marshallm
09-18-2004, 09:32 PM
Happy I was able to help! :)
The only PHP installations I've ever worked on are ones that I've set up myself, so I guess I never realized that mcrypt is not an option for a lot of developers. :(