...

View Full Version : Bidirectional Encryption/Compressing a string



Keleth
10-20-2009, 10:11 AM
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.

abduraooft
10-20-2009, 10:17 AM
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?

Phil Jackson
10-20-2009, 10:34 AM
use this to encrpt:



<?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
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");
}
?>

Keleth
10-20-2009, 06:10 PM
Interesting technique. I'm not sure I understand it completely, but I'll take a further look.

Keleth
10-20-2009, 06:11 PM
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.

Phil Jackson
10-20-2009, 08:36 PM
why would you want to compress it in the first place?

BWiz
10-21-2009, 01:46 AM
serialize() (http://www.php.net/manual/en/function.serialize.php)?

Keleth
10-21-2009, 06:42 AM
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.

Phil Jackson
10-21-2009, 06:54 AM
Wouldn't using a database be more sufficient?

Keleth
10-21-2009, 10:13 PM
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.

Phil Jackson
10-21-2009, 11:27 PM
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.

Keleth
10-21-2009, 11:45 PM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum