Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Encryption/Decryption Program:

by trizen (Friar)
on May 07, 2014 at 10:03 UTC ( #1085294=note: print w/ replies, xml ) Need Help??


in reply to Encryption/Decryption Program:

Here is a piece of code that I wrote about two years ago which does this (and more). Feel free to adjust it for your needs:

use 5.010; use strict; use warnings; sub encode_decode ($$) { my ($encode, $text) = @_; my $i = 1; my $output = ''; LOOP_1: foreach my $c (map ord, split //, $text) { foreach my $o ([32, 121]) { if ($c > $o->[0] && $c <= $o->[1]) { my $ord = $encode ? $c + ($i % 2 ? $i : -$i) : $c - ($i % 2 ? $i : -$i); if ($ord > $o->[1]) { $ord = $o->[0] + ($ord - $o->[1]); } elsif ($ord <= $o->[0]) { $ord = $o->[1] - ($o->[0] - $ord); } $output .= chr $ord; ++$i; next LOOP_1; } } $output .= chr($c); $i = 1; } return $output; } my $enc = encode_decode(1, q{this is a test}); my $dec = encode_decode(0, $enc); say "Enc: ", $enc; say "Dec: ", $dec; __END__ Enc: uflo jq b ucvp Dec: this is a test
Also, please see: http://rosettacode.org/wiki/Caesar_cipher#Perl


Comment on Re: Encryption/Decryption Program:
Download Code
Re^2: Encryption/Decryption Program:
by tobyink (Abbot) on May 07, 2014 at 12:41 UTC

    Simpler:

    use v5.14; sub encode_decode { shift =~ tr/A-Za-z/ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgf +edcba/r; } my $enc = encode_decode("this is a test"); my $dec = encode_decode($enc); say "Enc: ", $enc; say "Dec: ", $dec; __END__ Enc: gsrh rh z gvhg Dec: this is a test
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-09-24 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (246 votes), past polls