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 5 of 5
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    Question Simple PHP encrypt/decrypt

    Hi guys,

    I frequently use these little encrypt/decrypt functions for various reason, alot of times the info is passed through URLS.
    PHP Code:
    function encrypt($sData$sKey='mysecretkey'){
        
    $sResult '';
        for(
    $i=0;$i<strlen($sData);$i++){
            
    $sChar    substr($sData$i1);
            
    $sKeyChar substr($sKey, ($i strlen($sKey)) - 11);
            
    $sChar    chr(ord($sChar) + ord($sKeyChar));
            
    $sResult .= $sChar;
        }
        return 
    encode_base64($sResult);
    }

    function 
    decrypt($sData$sKey='mysecretkey'){
        
    $sResult '';
        
    $sData   decode_base64($sData);
        for(
    $i=0;$i<strlen($sData);$i++){
            
    $sChar    substr($sData$i1);
            
    $sKeyChar substr($sKey, ($i strlen($sKey)) - 11);
            
    $sChar    chr(ord($sChar) - ord($sKeyChar));
            
    $sResult .= $sChar;
        }
        return 
    $sResult;
    }


    function 
    encode_base64($sData){
        
    $sBase64 base64_encode($sData);
        return 
    strtr($sBase64'+/''-_');
    }

    function 
    decode_base64($sData){
        
    $sBase64 strtr($sData'-_''+/');
        return 
    base64_decode($sBase64);

    Which will result in something like this:
    01-28-2010 gets encrypted into: YJOSpaycmGJhYQ==

    I was wondering if there would be an easy way to make it so that this function would only use alpha numeric characters (to make it more url safe).
    ...A-Z, a-z, 0-9...

    I didn't write this code and can't remember where l found it but if l was able to tweak it so that it only used alpha numeric that would be awesome.

    Any ideas, or alternate function snippets you can share?

    Thanks!

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Untested:

    Code:
    function encode_base64($sData){
    	$sBase64 = base64_encode($sData);
    	return str_replace('=', '', strtr($sBase64, '+/', '-_'));
    }
    
    function decode_base64($sData){
    	$sBase64 = strtr($sData, '-_', '+/');
    	return base64_decode($sBase64.'==');
    }
    Or this one, which uses substr instead of str_replace:

    Code:
    function encode_base64($sData){
            $sBase64 = base64_encode($sData);
            return substr(strtr($sBase64, '+/', '-_'), 0, -2);
    }
    
    function decode_base64($sData){
            $sBase64 = strtr($sData, '-_', '+/');
            return base64_decode($sBase64.'==');
    }
    Last edited by MattF; 01-28-2010 at 07:03 PM.

  • Users who have thanked MattF for this post:

    Coastal Web (01-28-2010)

  • #3
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    #2 with substr works like a charm, thanks man; l didn't know that the == was the only non alphanumeric thing in there.

    >> edit:

    I just realized that somtimes it doesn't have == at the end, but just = ... what would the deal w/that be? It seems that it doesn't matter how many = symbols there are on the end what is the purpose of these.
    Last edited by Coastal Web; 01-28-2010 at 07:22 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Use the first example, (the one using str_replace), if there can be one or two = on the end of the string. The substr method will bugger the hash up if there's only one.

    I'm not sure if the equals signs are actually necessary on the string or whether they are just a marker, to be honest. Can't say as I've ever checked.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by MattF View Post
    I'm not sure if the equals signs are actually necessary on the string or whether they are just a marker, to be honest. Can't say as I've ever checked.
    the equal signs are used to pad the base64 string to match the 6bit/8bit conversion difference. see RFC 4648


    … honestly
    Last edited by Dormilich; 01-28-2010 at 07:47 PM.


  •  

    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
    •