2007-04-08

 

Perl Encryption

I discovered that the Perl Crypt::Blowfish library could only encrypt 8-byte long strings. This is obviously not good in any practical way. So, I experimented, and came up with the following concept program. At the end, it also uses MIME::Base64::URLSafe to make the encrypted string portable - especially for the web.



#!/usr/bin/perl

use strict;

use MIME::Base64::URLSafe; # Used to encode our parameter string
use Crypt::Blowfish; # All our parameters and cookies will be blowfish encrypted


my $crypt_key = pack("H16", "1234567890ABCDEF"); # min. 8 bytes
my $str = $ARGV[0]; # Get out str to encrypt and encode


my $encoded_str = encrypt_long_str( $crypt_key, $str );
print "\n-----------------------------------------------\n$encoded_str\n-----------------------------------------------\n\n";


my $decoded_str = decrypt_long_str( $crypt_key, $encoded_str );
print "\n-----------------------------------------------\n$decoded_str\n-----------------------------------------------\n\n";


exit;

#######################################
#
# S U B S
#
#######################################

sub encrypt_long_str {

my( $key, $str ) = @_;

my $strlen = length( $str );
my $encrypted = "";
my $encoded = "";
my $parts = 0;

my $cipher = new Crypt::Blowfish $key;

# calculate the parts
while( length( $str ) ) {

if( length( $str ) < style="color: rgb(68, 68, 255);">) {

while( length( $str ) < style="color: rgb(68, 68, 255);">) {

$str .= " ";

}

my $raw = $cipher->encrypt( $str );
my $encoded1 = urlsafe_b64encode( $raw );

print STDERR "\tdebug: encrypt_long_str(): encoded_part=$encoded1\n";

$encoded .= $encoded1;

$str = "";
$parts++;

} else {

my $tstr = substr( $str, 0, 8 );
my $raw = $cipher->encrypt( $tstr );
my $encoded1 = urlsafe_b64encode( $raw );

print STDERR "\tdebug: encrypt_long_str(): encoded_part=$encoded1\n";

$encoded .= $encoded1;

$str =~ s/^$tstr//;
$parts++;

}

}

if( $parts < 10 ) { $encoded = "000" . $parts . $encoded; }

elsif( $parts > 9 && $parts < 100 ) { $encoded = "00" . $parts . $encoded; }

elsif( $parts > 99 && $parts < 1000 ) { $encoded = "0" . $parts . $encoded; }

else { $encoded = $parts . $encoded; }

return $encoded;


}

sub decrypt_long_str {

my( $key, $str ) = @_;
my $cipher = new Crypt::Blowfish $key;

my $retval = "";

$str =~ s/^(\d\d\d\d)//;
my $parts = $1;
$parts =~ s/^0+//;

print STDERR "\tdebug: decrypt_long_str(): parts=$parts\n";

my $steps = length( $str ) / $parts;

print STDERR "\tdebug: decrypt_long_str(): steps_length=$steps\n";

for( my $step = 0; $step < $parts; $step++ ) {

my $substr = substr( $str, ( $step * $steps ), $steps );
my $dec = urlsafe_b64decode( $substr );

print STDERR "\tdebug: decrypt_long_str(): substr=$substr\n";

my $dstr = $cipher->decrypt( $dec );

$retval .= $dstr;

print STDERR "\tdebug: decrypt_long_str(): decrypted part $step :: $dstr\n";

}

# Remove any trailing spaces
$retval =~ s/\s+$//;

return $retval;

}


I hope you find it usefull


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?