Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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($encfalse);
    print 
    $dec PHP_EOL
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    DrDel (02-18-2014)

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for this post, but it outputs weird characters when executing the script:

    �uv�-v�-n-��v{t this is a string

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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$rangetrue);
            
    $v $range[($idx $iSize) % count($range)];
        };
        
    $aStr str_split($str);
        
    array_walk($aStr$f$aRange);
        return 
    implode($aStr);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •