# Trying to encrypt then decrypt s 4 digit sequence

• 02-07-2014, 04:24 PM
DrDel
Trying to encrypt then decrypt s 4 digit sequence
I tried googling this but cant find the answer.

I have a series of 4 digits numbers (8735, 2345, 9021, etc)

how can i encrypt these into a 4 digit sequence of letters and numbers and then at a later time decrypt it back to the same 4 digit sequence with a different function in a simple way?

thanks
• 02-07-2014, 08:29 PM
Fou-Lu
It won't give you character to character conversions, but if you're needing encryption you can look at the mcrypt library. It takes some work to learn to use it properly, but just google for some php mcrypt tutorials and I'm sure you'll find lots of step by step usages.

Of course if you want to just keep the 1:1, then you can do something simple like ROT13 style. Not really secure or anything, but rotating the characters by 13 would make the 1:1 chars work:
PHP Code:

``` function enrot13(&\$itm){    \$itm = chr(ord(\$itm) + 13);}function derot13(&\$itm){    \$itm = chr(ord(\$itm) - 13);}function rot13(\$str, \$encrypt = true){    \$a = str_split(\$str);    array_walk(\$a, \$encrypt ? 'enrot13' : 'derot13');    return implode("", \$a);}\$s = 'this is a string';\$enc = rot13(\$s);print \$enc . PHP_EOL;\$dec = rot13(\$enc, false);print \$dec . PHP_EOL;  ```
• 02-18-2014, 03:07 PM
DrDel
Thank you for this post, but it outputs weird characters when executing the script:

�uv�-v�-n-��v{t this is a string
• 02-18-2014, 03:14 PM
DrDel
turns out it is already buit in for characters (PHP 5):
http://us3.php.net/manual/en/function.str-rot13.php

is there a rot5 in PHP for numbers? I need a combination of rot13 and rot5 for a cipher to encrypt and decrypt numbers and letters... can anyone help? rot5 isn't built into PHP5 it turns out
• 02-18-2014, 03:25 PM
DrDel
Turns out it is called rot18 -- rot18 is a cipher for both numbers and letters

Does anyone have an encrypt/decrypt function using rot 18

I cant find one online and my coding skills are not good enough to come up with one on my own

thanks
• 02-18-2014, 07:16 PM
felgall
rot18 is not an encryption routine - it is an obfuscating routine. It prevents being able to read the content at a casual glance but is trivial to decode.
• 02-18-2014, 07:40 PM
Fou-Lu
Yes indeed.
Also, I wouldn't recommend 18; you cannot tell if its rot13 or rot18 since the numbers only cycle between other numbers. As mentioned (and me prior as well), its not at all secure.

You could use rot47 though. I just winged this, it seems to work, but I can guarantee it wouldn't be the quickest route:
PHP Code:

``` function rot47(\$str){    static \$aRange;    \$aRange = \$aRange == null ? range('!', '~') : \$aRange;    \$f = function(&\$v, \$k, array \$range)    {        \$iSize = ceil(count(\$range) / 2);        \$idx = array_search(\$v, \$range, true);        \$v = \$range[(\$idx + \$iSize) % count(\$range)];    };    \$aStr = str_split(\$str);    array_walk(\$aStr, \$f, \$aRange);    return implode(\$aStr);}  ```
• 02-18-2014, 07:57 PM
felgall
The shortest encryption script I have seen is over 4k in size (when coded in a scripting language).

With encryption you can have both the encrypted text and the encrypt/decrypt code and still not be able to actually decrypt it because you don't have the decryption key.