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 12 of 12
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Bidirectional Encryption/Compressing a string

    I was curious if anyone knew any stock methods of bidirectional encryption/string compression I could use? Basically some way that I could take a string created from an imploded array, encode it for storage or to be passed via a get variable, then be able to get back the original data to explode again when necessary.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    I was curious if anyone knew any stock methods of bidirectional encryption/string compression I could use?
    May I ask, what's your ultimate aim?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    use this to encrpt:

    PHP Code:
    <?php 
    function convert($str,$ky=''){ if($ky=='')return $str;$ky=str_replace(chr(32),'',$ky);if(strlen($ky)<8)exit('key error');$kl=strlen($ky)<32?strlen($ky):32;$k=array();for($i=0;$i<$kl;$i++){$k[$i]=ord($ky{$i})&0x1F;}$j=0;for($i=0;$i<strlen($str);$i++){$e=ord($str{$i});$str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);$j++;$j=$j==$kl?0:$j;}return $str;}

    convert(urlencode("data bla bla"), "keyOf8-16Bytes");

    ?>
    and to decode:
    PHP Code:
    <?php 
    function convert($str,$ky=''){ if($ky=='')return $str;$ky=str_replace(chr(32),'',$ky);if(strlen($ky)<8)exit('key error');$kl=strlen($ky)<32?strlen($ky):32;$k=array();for($i=0;$i<$kl;$i++){$k[$i]=ord($ky{$i})&0x1F;}$j=0;for($i=0;$i<strlen($str);$i++){$e=ord($str{$i});$str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);$j++;$j=$j==$kl?0:$j;}return $str;}

    if(isset(
    $_GET['varName']))
    {
        
    convert(urldecode($_GET['varName']), "keyOf8-16Bytes");
    }
    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Interesting technique. I'm not sure I understand it completely, but I'll take a further look.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by abduraooft View Post
    May I ask, what's your ultimate aim?
    As I mentioned, the goal is data compression. A method of taking an array turned string via implode, and storing it or passing it via GET variables.

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    why would you want to compress it in the first place?
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Unless I'm mistaken, serialize is used more for safe data storage into a database.

    As for why, one example is on my site, I have need/use of virtualizing a deck of cards, pull cards, then be able to store the deck for later use. Right now, I basically make a 52 index array with the numbers shuffled (I need to be able to know the orders the cards will be drawn in, more for future reference), then for storage, I implode the array with ~ as the delimiter. Just explode on ~ and I get back my array. To save a deck though, you have to be logged in. What I'd like to do is make it so visitors can save a URL with a get variable attacked to return to a deck. A point of reference is, for those of you who play WoW, a lot of the skill tree sites online figure out what skills are saved via the get variable in the URL. Just pass on the URL and share the skill tree with others. And obviously, the data in the URL isn't raw data, its compressed or encoded.

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Wouldn't using a database be more sufficient?
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Phil Jackson View Post
    Wouldn't using a database be more sufficient?
    Storing the data into the database is no problem, I already do that. But if the user isn't logged in, what am I supposed to be storing? And regardless, that deck still can't be shared. The goal is to have something that can be passed along via URL.

  • #11
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Hmm problem using the url to store such data is that the deck could quite easily be minipulated. Why not post the data. Also, why not create instant tables with the users ip address laced into the name? you dont have to be logged in for that.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Phil Jackson View Post
    Hmm problem using the url to store such data is that the deck could quite easily be minipulated. Why not post the data. Also, why not create instant tables with the users ip address laced into the name? you dont have to be logged in for that.
    That couldn't be shared. Manipulation isn't a big deal, nor is security.

    But maybe the table scheme is a good way to go about it. Store the deck, then reference it. But in this case, I'd still like to figure out a way of avoiding easy manipulation (change an ID number, get someone else's deck?), so now maybe I should be looking for a short encryption technique (turn 1 character into a 4-6 digit hex or base 36 string).


  •  

    Posting Permissions

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