Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Simple perl encoder

by Anonymous Monk
on Sep 27, 2011 at 20:25 UTC ( [id://928177]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

All: I'm trying to create a "multiplayer" game. Multiplayer meaning we both have the same script on our computer and we send a txt file back and forth to record the events.

I need a way to save data as something other than text. I don't need full blown encryption but something that a human can't read and cheat because the game will then read that text file. A few years ago I saw a code that converted everything to 1s and 0s and it was easy to encode and decode from that but I can't find it.

The data will be strings of text, not just integers. Can someone help me find a simple way to make the text unreadable by human eyes but easy enough the script can do it on the fly?

Replies are listed 'Best First'.
Re: Simple perl encoder
by CountZero (Bishop) on Sep 27, 2011 at 20:40 UTC
    Crypt::Lite seems to fit your requirements.

    From its docs:

    Sometimes it's necessary to protect some certain data against plain reading or you intend to send information through the Internet. Another reason might be to assure users cannot modify their previously entered data in a follow-up step of a long Web transaction where you don't want to deal with server-side session data. The goal of Crypt::Lite was to have a pretty simple way to encrypt and decrypt data without the need to install and compile huge packages with lots of dependencies.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Simple perl encoder
by BrowserUk (Patriarch) on Sep 28, 2011 at 00:25 UTC
    A few years ago I saw a code that converted everything to 1s and 0s and it was easy to encode and decode from that but I can't find it.

    Bit-wise not (~) applied to a string does exactly that very efficiently:

    $s = 'The quick brown fox jumps over the lazy dog';; print unpack 'C*', $s;; 84 104 101 32 113 117 105 99 107 32 98 114 111 119 110 32 102 111 120 +32 106 117 109 112 115 32 111 118 101 114 32 116 104 101 32 108 97 12 +2 121 32 100 111 103 $t = ~$s;; print unpack 'C*', $t;; 171 151 154 223 142 138 150 156 148 223 157 141 144 136 145 223 153 14 +4 135 223 149 138 146 143 140 223 144 137 154 141 223 139 151 154 223 + 147 158 133 134 223 155 144 152 $u = ~$t;; print unpack 'C*', $u;; 84 104 101 32 113 117 105 99 107 32 98 114 111 119 110 32 102 111 120 +32 106 117 109 112 115 32 111 118 101 114 32 116 104 101 32 108 97 12 +2 121 32 100 111 103 print $u;; The quick brown fox jumps over the lazy dog

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Simple perl encoder
by ikegami (Patriarch) on Sep 27, 2011 at 23:45 UTC
Re: Simple perl encoder
by Tux (Canon) on Sep 28, 2011 at 06:29 UTC

    If it is a combination of predefined values consisting of both numeric and string values nicely stashed up in a structure, I'd suggest using a combination of a serializer like Storable and a simple binary-text converter like builtin pack.

    use Storable qw( nfreeze thaw ); my $game_data = { score => 1425, hits => 3, name => "Supernova", accuracy => 0.34, : : }; my $enc = unpack "H*" => nfreeze ({ game_data => $game_data }); # $enc now contains something like # 05080300000001040300000004088300000004686974730a0953757065726e6f7661 +000000046e616d6509000005910000000573636f72650a04302e33340000000861636 +375726163790000000967616d655f64617461

    On the receiving side, other way round ...

    use Storable qw( nfreeze thaw ); my $game_data = thaw (pack "H*" => $enc)->{game_data};

    It is up to you to decide how <c>$enc gets transported between the games.


    Enjoy, Have FUN! H.Merijn
Re: Simple perl encoder
by onelesd (Pilgrim) on Sep 27, 2011 at 21:24 UTC

    Encryption is encryption, full blown or not. If you want encryption then use what CountZero recommended.

    If you want something a little easier to work with (and for humans to encode/decode) use MIME::Base64.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://928177]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found