Encrypt Decrypt Function
Posted: Sun Dec 23, 2007 4:08 am
This Encrypt/Decrypt function results in an encrypted code with non-alphanumeric chars. Is anybody able to modify this php code slightly to only include alphanumeric chars ?
Results:
Encrypted: Qmc{v/|q}dazt/}j|x? ## 7>4&?"?
Decrypted: Alpha numeric only,34,23.55,50
Code: Select all
<?php
// String EnCrypt + DeCrypt function
// Author: halojoy, July 2006
// Modified and commented by: laserlight, August 2006
function convert($text, $key = '') {
// return text unaltered if the key is blank
if ($key == '') {
return $text;
}
// remove the spaces in the key
$key = str_replace(' ', '', $key);
if (strlen($key) < {
exit('key error');
}
// set key length to be no more than 32 characters
$key_len = strlen($key);
if ($key_len > 32) {
$key_len = 32;
}
$k = array(); // key array
// fill key array with the bitwise AND of the ith key character and 0x1F
for ($i = 0; $i < $key_len; ++$i) {
$k[$i] = ord($key{$i}) & 0x1F;
}
// perform encryption/decryption
for ($i = 0; $i < strlen($text); ++$i) {
$e = ord($text{$i});
// if the bitwise AND of this character and 0xE0 is non-zero
// set this character to the bitwise XOR of itself
// and the ith key element, wrapping around key length
// else leave this character alone
if ($e & 0xE0) {
$text{$i} = chr($e ^ $k[$i % $key_len]);
}
}
return $text;
}
?>Code: Select all
<?php
$encrypted = convert("Alpha numeric only,34,23.55,50", $key="password");
print $encrypted;
print convert($encrypted, $key="password");
?>Encrypted: Qmc{v/|q}dazt/}j|x? ## 7>4&?"?
Decrypted: Alpha numeric only,34,23.55,50