For "Whatever shifting algorithm" I'll use rot13 which by nature with English text is self-reversing.

Here's a basic one-liner that requires you to deal with input and output redirection.:

perl -pe 'tr/a-zA-Z/n-za-mN-ZA-M/;'

Here's a bit more complex example. It doesn't take fancy arguments. It figures out what to do based on the end of the filename (the "extension"). It has some error handling but that could be improved.

#!/usr/bin/perl use strict; use warnings; die "Run with argument." unless defined $ARGV[0]; my $file = $ARGV[0]; if ( $file =~ /\.enc/ ) { decrypt( $file ); } else { encrypt( $file ); } exit; sub encrypt { my $f = shift; my $enc = $f . '.enc'; files( $f, $enc ); } sub decrypt { my $f = shift; my $dec = $f; $dec =~ s/\.enc$/.dec/; files( $f, $dec ); } sub files { my ( $in_file, $out_file ) = @_; open my $in, '<', $in_file or die "Cannot read input file $in_file + : $!\n"; open my $out, '>', $out_file or die "Cannot write to output file $ +out_file : $!\n"; rotate( $in, $out ); close $in; close $out or die "Failed to complete write to output file $out_fi +le : $!\n"; } sub rotate { my ( $fh_in, $fh_out ) = @_; while ( my $text = <$fh_in> ) { $text =~ tr/a-zA-Z/n-za-mN-ZA-M/; print { $fh_out } $text; } }

I used the program to encode itself and then decode that, and there's no difference.:

$ perl foo foo $ perl foo foo.enc $ diff foo foo.dec $

Substitution ciphers are pretty simple and are not secure. For example rot13 is built into some newsgroup and mail client software, because it's really only good to obscure spoilers. It might fool some simplistic address harvesters, too.

By the way, I'd be remiss not to mention archival storage in this context. That is almost as popular as my Tux the penguin with an AR-15 on the other arm.


In reply to Re: Encryption/Decryption Program: by mr_mischief
in thread Encryption/Decryption Program: by Perl Beginner01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.