SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    looking for a simple encryption/decryption algorithm

    Hi

    I am looking for an alogrithm that will allow me to perform encryption/decryption
    based on a hidden key.
    I am preferring one that comes built in in PHP and not one that I need to start including
    extensions for

    any suggestions?

  2. #2
    SitePoint Enthusiast gundari's Avatar
    Join Date
    Apr 2005
    Location
    Santa Fe, Argentina
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the mcrypt or mhash libraries, if they are available. Otherwise you can use an XOR routine, which, if programmed correctly, is incredibly secure.

  4. #4
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with mcrypt is that it requires re-compiling PHP with --with-mcrypt[=DIR] parameter

    I am looking for a solution that is built in

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only built-in encryption in php is str_rot13. This function is rather humorous.

    There are some simple but reliable crypts that can be easily ported to php (for example RC4).

    Quote Originally Posted by Codecfault
    Otherwise you can use an XOR routine, which, if programmed correctly, is incredibly secure.
    Care to post an example?

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: Realised xor is actually a keyword in php, so the function name changed.

    PHP Code:


    function XORcrypt($string$key)
    {
        
    $padChar "_";
        
    $stringLength strlen($string);
        
    $keyLength strlen($key);
            
        if(
    $keyLength != $stringLength)
        {
            if(
    $keyLength $stringLength)
            {
                while(
    $keyLength $stringLength)
                {
                    
    $keyLength .= $padChar;
                }
            }
            
            else 
    //$stringLength < $keyLength
            
    {
                while(
    $stringLength $keyLength)
                {
                    
    $stringLength .= $padChar;
                }
            }
        }
        
        else
        {
            
    $output "";
            
            for(
    $i 0$i $stringLength$i++)
            {
                
    $output .= ($string[$i] ^ $key[$i]);
            }
                    
                    
    $output str_replace($padChar""$output);
            
        }
        
        return 
    $output;

    Same function will encrypt and decrypt. Choose a $padChar that isnt likely to be in the text. It hasn't been tested but it should work, then again it is late. Anyways good luck
    Last edited by Codecfault; Jan 19, 2006 at 06:27.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    works like a charm?

  9. #9
    SitePoint Enthusiast duckax's Avatar
    Join Date
    Aug 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Codecfault. Been looking for something like this as well.

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No prob. Ask and yee shall receive


Bookmarks

Posting Permissions

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