Hello david_lyon,
I don't know if you need this now, but maybe you or some other monk may need to encrypt something using 'Crypt::OpenSSL::AES'. I added some comments to help understand the process. If anything doesn't make sense, I'll try to explain in detail.
#!/usr/local/bin/pyrperl -w
use strict;
use warnings;
use Time::HiRes qw( gettimeofday );
my ( %Account ); # Where you keep persistent values
my ( $plaintext ); # Text to be en/decrypted
my ( $CLOG ); # Log information
open ( $CLOG, ">", "./Cipherlogs" ) || Die_Rtn(" ! open-40 $!");
# my $len=length($Account{Cipher}); # Cipher mus
+t be 32 bytes in size
# print $CLOG "Cipher: |$len|$Account{Cipher}|\n";
# print $CLOG "$value ",length($plaintext),$plaintext,"\n"; # Sh
+ows what the text looks like
# my $cipher = GetCipher( $Account{Cipher} ); # $Accoun
+t{Cipher} is saved seed for the cipher
my $seed = ""; my $stime = gettimeofday;
for ( 1..32 ) { $seed .= chr(rand(255)); }
print length($seed),"\t$seed\n";
my $cipher = GetCipher( $seed ); # Must get the
+ cipher from the saved $seed
print $CLOG " Loop Time Size of text Total calls\n";
+ # If you want to verify it's doing work!
my ( $no, $value, $total ) = ( 1, 0, 0 );
if ( $cipher )
{ while ( 1 )
{
my $plaintext = chr($value) x $no; # this is th
+e text to be encrypted
my $encrypted = Encrypt( $cipher, $plaintext );
my $decrypted = Decrypt( $cipher, $encrypted );
if ( $decrypted ne $plaintext )
{ print "NoGood! |$decrypted|$plaintext|\n"; }
$value++; $total++;
if ( $value > 255 )
{ $stime = gettimeofday - $stime;
print $CLOG "$stime \t$no \t\t$total\n"; #
+ If you want to verify it's doing work!
$value = 0; $no++;
if ( $no > 2**16 ) { last; }
$stime = gettimeofday;
}
}
}
exit; # Optional
## my $cipher = GetCipher( $key );
sub GetCipher
{ require Crypt::OpenSSL::AES;
my $key = shift;
if ( length($key) != 32 ) { return 0; }
my $cipher = new Crypt::OpenSSL::AES($key);
return $cipher;
}
## my $encrypt = Encrypt( $cipher, $data );
sub Encrypt
{ require Crypt::OpenSSL::AES;
my ( $cipher, $data ) = @_; my $encrypted = "";
my $len = length($data); $data = pack("N",$len) . $data;
$len = length($data) % 16;
if ( $len ) { $data .= "\0" x ( 16 - $len ); }
while ( $data )
{ $encrypted .= $cipher->encrypt(substr($data,0,16,"") );
}
return $encrypted;
}
## my $decrypt = Decrypt( $cipher, $encrypted );
sub Decrypt
{ require Crypt::OpenSSL::AES;
my ( $cipher, $encrypted ) = @_; my $decrypted = "";
while ( $encrypted )
{ $decrypted .= $cipher->decrypt(substr($encrypted,0,16,"") );
}
my $len = unpack("N", substr($decrypted,0,4,"") );
return substr($decrypted,0,$len);
}
1;
__END__
I started to respond earlier, but was called away. The value of this technique is that all character from chr(0) through chr(255) can be en/decrypted. The bigger the record the longer it will take to en/decrypted. Hope this helps.
Good Luck ... Ed
"Well done is better than well said." - Benjamin Franklin
|