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

Does anyone here have a simple encryption mechanism that need not use modules?

I am aware of the existence of tons of Crypt modules for encrypting data, but I just need simple encryption that encrypts or encodes a character into something different and with the capability to decrypt or decode this into its original value. I don't need a full-proof mechanism. I just wanted to have a text file represented in a non-readable way before storing it in a database.

I had made a similar implementation on Visual Basic, but I just need to have another opinion in Perl.

Thanks, soon_j
  • Comment on Anyone has simple encryption mechanism?

Replies are listed 'Best First'.
Re: Anyone has simple encryption mechanism?
by Zaxo (Archbishop) on Jan 10, 2004 at 03:28 UTC

    Here's a cheap way to get a non-repeating xor pattern of the proper length. It works by seeding rand with the key and relying on repeatability. The key is a single perl integer. The symmetric property* of xor encryption is handy.

    # Usage: cypher KEY, LIST; sub cypher { srand shift; my $string = join '', @_; my $here = 0; while ( 2*$here < length( $string)) { vec( $string, $here, 16) ^= int rand 1<<16; $here++; } $string; } my $encoded = cypher 12345, <<'EOT'; Listen, my children, and you shall hear Of the midnight ride of Paul Revere. EOT print map {sprintf '%x ', $_ } unpack 'C*', $encoded; print $/, cypher 12345, $encoded;
    All the map ... sprintf .... unpack stuff is just to coerce the binary encoded string into ascii, to avoid frying a tty's tiny brain.

    This is better than rot13, but probably is still weak crypto. The keyspace is small. Decryption depends on having the same particular implementation of rand. Regularities of rand can be exploited. But it is certainly enough to discourage casual readers.

    * the same procedure is used for both encryption and decryption.

    After Compline,
    Zaxo

Re: Anyone has simple encryption mechanism?
by bart (Canon) on Jan 10, 2004 at 12:07 UTC
    Whenever anybody feels the need to ask "Can I do X without using any modules?", the first thing to do is check the existence of modules in Pure Perl. You can always just lift the code out of the modules, and paste it into your script. There's nothing magic about modules, except maybe that they are "run" at compile time — taking care of proper initialisation. So maybe you'll have to tweak the code a little to make it work right: move it around a little, add BEGIN blocks, ...

    The conventional nomenclatura for such modules, if one exists in XS, is to add "PurePerl" or "PP" to the name So, looking for crypt PP, I found these likely candidates:

    I don't know if they really all are encryption+decryption modules, but it looks like enough choice to get you started.
Re: Anyone has simple encryption mechanism?
by exussum0 (Vicar) on Jan 09, 2004 at 23:15 UTC
    The simples thing you can do is XOR your data, block for block, but it's REALLY REALLY WEAK. You are better off using RC5, RSA,..

    If it's not particularly important data, that's cool, but be careful never to use the rational, "No one will figure it out." Someone will always be curious enough to see if they can do it, or just are melicious enough to do it for kicks.


    Play that funky music white boy..
      The data is not sensitive, I can do away with any encryption. I just wanted to have something like a plastice cover on a notebook. Thanks for the simple suggestion.

Re: Anyone has simple encryption mechanism?
by Anonymous Monk on Jan 10, 2004 at 00:34 UTC
    It's not perfect, but it's simple... you might try CipherSaber. Once you get past the political posturing, the basic idea is:
    we should emulate the Jedi masters by learning how to build strong cryptography programs all by ourselves.
    And then it goes on to describe a simple algorithm (basically RC4) and give some helpful test vectors. There's also a perl implementation you can refer to.
Re: Anyone has simple encryption mechanism?
by zentara (Cardinal) on Jan 10, 2004 at 18:25 UTC
    Here is one:
    #!/usr/bin/perl -w use strict; my $i = "abcdefghijkl m nopqrst uvxwyz"; print "$i\n"; my $j = mess_it($i); print "$j\n"; print fix_it($j),"\n"; sub fix_it{ my $s = shift; $s =~ s#(.)#chr(ord($1)/2)#ge; return $s; } sub mess_it{ my $s = shift; $s =~ s/(.)/chr(ord($1)*2)/ge; return $s; }