Given an input string (will actually be an integer value) and an encryption key, I need to encrypt the input string in such a way that the resulting string is:
- URL safe (my permitted URI characters is currently: a-z 0-9~%.:_-)
- Filename safe (meaning, it only uses valid directory/filename characters)
- FTP account username safe
- Email account username safe
Then, given the encrypted string and the same encryption key, decrypt the the string into its unencrypted form.
This is not intended to be a security measure. Implementation needs to be in PHP. Thanks.
EDIT 1 (the decoding still includes undesirable characters; plus, there is no way to make sure the resulting encrypted string is within a certain length):
function decrypt($string, $encryption_key)
{
assert(isset($string) === TRUE);
assert(isset($encryption_key) === TRUE);
$result = '';
$string = base64_decode($string);
for ($i = 0; $i < strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($encryption_key, ($i % strlen($encryption_key)) - 1, 1);
$char = chr(ord($char) - ord($keychar));
$result .= $char;
}
return $result;
}
function encrypt($string, $encryption_key)
{
assert(isset($string) === TRUE);
assert(isset($encryption_key) === TRUE);
$string = (string) $string;
$result = '';
for ($i = 0; $i < strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($encryption_key, ($i % strlen($encryption_key)) - 1, 1);
$char = chr(ord($char) + ord($keychar));
$result .= $char;
}
return base64_encode($result);
}