***** infoCopter.com/perl *****

My::Crypt - Simple Data Encryption

Out-of-the-box symmetric Data Encryption and Decryption with My::Crypt

[ Up ] - [ Download My::Crypt ]


The My::Crypt algorithm is rather optimized to encrypt a string than a text or a binary file, although you could work around with the following script to encrypt text files.


#!/usr/bin/perl -w
use strict;

# Usage:
#
# ./crypt.pl <file_to_encrypt> <encrypted_file>

use My::Crypt;

my $crypt = My::Crypt->new();

my $infile  = $ARGV[0];
my $outfile = $ARGV[1];

print "Encrypting '$infile'...\n";

&encrypt_file(
	in     => $infile , 
	out    => $outfile ,
	cipher => 'yourpass'
);


&decrypt_file(
	in     => $outfile , 
	out    => "$infile\.back" ,
	cipher => 'yourpass'
);

print "done\n";


####################################################
sub encrypt_file (%) {
####################################################
	my %args = @_;

	local $/ = undef;

	open(IN, $args{'in'}) or print $!;
	binmode IN unless $args{'in'} =~ /\.txt$/;
	 my $input = <IN>;
	close IN;

	my $encrypted =  $crypt->encrypt($input, $args{'cipher'});

	open(OUT, ">$args{'out'}") or print $!;
	 print OUT $encrypted;
	close OUT;
	
}

####################################################
sub decrypt_file (%) {
####################################################
	my %args = @_;

	local $/ = undef;

	open(IN, $args{'in'}) or print $";
	 my $input = <IN>;
	close IN;
	
	my $decrypted = $crypt->decrypt($input, $args{'cipher'});

	open(OUT, ">$args{'out'}") or print $!;
	binmode OUT unless $args{'out'} =~ /\.txt$/;
	 print OUT $decrypted;
	close OUT;
	
}
© reto :)