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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |