Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: encrypt and decrypt text

by jbrugger (Parson)
on Feb 08, 2005 at 05:51 UTC ( [id://428938]=note: print w/replies, xml ) Need Help??


in reply to encrypt and decrypt text

You also could have a look at Data::Serializer, it provides a unified interface to the various serializing modules currently available. Adds the functionality of both compression and encryption.
example from cpan:
use Data::Serializer; $obj = Data::Serializer->new(); $obj = Data::Serializer->new( serializer => 'Storable', digester => 'MD5', cipher => 'DES', secret => 'my secret', compress => 1, ); $serialized = $obj->serialize({a => [1,2,3],b => 5}); $deserialized = $obj->deserialize($serialized); print "$deserialized->{b}\n";

Drawback may be 'spaces' to store sometimes go wrong. This can be solved by putting them in an array first, and store the array using Data::Serializer.

Replies are listed 'Best First'.
Re^2: encrypt and decrypt text
by pearlie (Sexton) on Feb 08, 2005 at 05:53 UTC
    I want simple encryption techniques. Can someone explain ROT-13 technique in a little more detail?

      There's not much to explain. The English alphabet consists of 26 letters (A through Z). You just rotate the alphabet 13 places, and now you've got N through Z followed by A through N. The most common way to do that in Perl is to use the tr/// operator to rotate the alphabet through the magic of transliteration. Try this:

      my $string = "Cleartext."; print $string, "\n"; $string = rot13( $string ); print $string, "\n"; $string = rot13( $string ); print $string, "\n"; sub rot13 { my $text = shift; $text = tr/A-Za-z/N-ZA-Mn-za-m/; return $text; }

      Not exactly strong encryption. Most decent newsreaders will be able to rot-13 text. Its original use on the Net was to prevent children and employers from accidentally seeing a curse word in some Usenet post. But the concept has been around since 3rd grade 200 years ago when Cracker Jacks first started putting decoder rings in their popcorn boxes. ;)


      Dave

        It has been around a lot longer than that. :-) rot-13 is a form of Caesar cipher (which is named after for Julius Caesar) a good write up on it is at http://en.wikipedia.org/wiki/Caesar_cipher
      Or, to be a little more "comprehensive" (and slightly less conventional, though still not seriously hard to "decrypt"), you could try "rot-23" -- here it is expressed with hex character codes (because hex is just easier for me, somehow):
      perl -pe 'tr{\x20-\x7d}{\x4f-\x7d\x20-\x4e}' clear.txt > cipher.txt
      And of course, doing the same thing on "cipher.txt" will turn it back into the original "clear.txt". This affects not just the alphabet letters, but all "printable" ascii characters from space through close-curly-brace (it leaves tildes alone, because you need to involve an even number of characters, and mutating spaces does a lot to help obscure the cleartext -- though it won't take someone long to figure out which "coded" character represents a space).

        There is something uniquely wonderful and perverse about this upgrade to the security of ROT13. It brought me a smile with my morning cuppa.

        Now consider the vast improvement in security unicode will bring us, what a keyspace, ROT2147483648, ROT4294967295...

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!

        And in order to also include the tilde character which was omitted for some reason:

        perl -pe 'tr{\x20-\x7e}{\x50-\x7e\x4f\x20-\x4e}' clear.txt > cipher.txt

        The expression is a bit more difficult to read/parse/write that way (essentially because an uneven number of characters in the initial alphabet needs a pivot element in the middle that is not rotated at all), but provides a bit more coverage.

      I've had a bit more than a passing thought about placing this under the "Snippets" or "Code" areas, but have shyed away so far. I had a similar need just last week, and took some Rot-13 and other similar code to roll my own into the following:

      >less -x4 obf.pl

      #!/usr/bin/perl ###################################################################### +###### # This utility can modify a "string file" as defined below to obfuscat +e # those strings using a modified rot-13 type algorithm. String inform +ation # is to be supplied via filenames, or piped from a file/process into t +his # script. All output is to stdout, suitable for piping into a resulta +nt # file. # # The string file is to contain passwords or snmp community strings # ### # # Credits: Ideas taken from # http://www.perlmonks.org/index.pl?node_id=385552 # Thanks to TZapper and Tachyon's posts # http://search.cpan.org/author/JUERD/Crypt-Caesar-0.01/Caesar.pm # http://search.cpan.org/author/AYRNIEU/Crypt-Rot13-0.6/Rot13.pm # http://www.perlmonks.org/index.pl?node_id=421114 # Thanks to Tanktalus #
      So, for example, you could have a file test.dat like so:
      > cat test.dat <p> public public private private our-read 0uR3ad our-write 0wR1t3 their-read th31read their-write theirwrong blah-blah blah-bl@h foo-bar barfoobar
      Then run the obf.pl script against it, with a rotation of 13:
      > obf.pl 13 test.dat > t.1 > cat t.1 # Rotational distance was undefined, assuming 0 13 public }$oyvp private }!v%n#r our-read =$_@nq our-write =&_>#@ their-read #u@>!rnq their-write #urv!&!|{t blah-blah oynu:oyMu foo-bar on!s||on!
      Using this code in actual expect or snmp scripts should be trival, though I've not gotten to that part in my own stuff yet :-)

      -Scott L. Miller

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found