Commit ff7bb145 authored by marcus-tun's avatar marcus-tun
Browse files

added short perl example

parent a6f77a1f
#!/usr/bin/env perl
use strict;
use Getopt::Long qw(:config bundling);
use Crypt::CBC;
my %opts = ();
my $verbose=0;
my $iv='0000000000000000';
my $key='0000000000000000';
my $plaintext='hello world, today is a good day to have long strings';
# Get options
GetOptions(\%opts, 'help|h',
'verbose|v',
'iv|i=s',
'key|k=s',
'plaintext|p=s',
) or exit;
if (exists $opts{help}) {
print ("Usage: interop.pl [arguments]
-v, --verbose
-i, --iv
-k, --key
-p, --plaintext\n");
exit (0);
}
if (exists $opts{verbose}) {
$verbose = 1;
}
if (exists $opts{iv}) {
$iv = trim($opts{iv});
}
if (exists $opts{key}) {
$key = trim($opts{key});
}
if (exists $opts{plaintext}) {
$plaintext = trim($opts{plaintext});
}
## Debug output
print ("plaintext: ".$plaintext."\n") if $verbose;
print ("iv: ".$iv."\n") if $verbose;
print ("key: ".$key."\n") if $verbose;
## ENCRYPTION
my $encryption_algorithm='Rijndael';
my $cipher = new Crypt::CBC ($key, $encryption_algorithm, -iv=>$iv, -header=>'none');
my $ciphertext= $cipher->encrypt($plaintext);
my $perl_iv = $cipher->iv();
#my $perl_pass = unpack('H*', $cipher->key());
my $perl_pass = $cipher->key();
print ('perl_iv: '.$perl_iv."\n") if $verbose;
print ('perl_pass: '.$perl_pass."\n") if $verbose;
print ("\n");
## DECRYPTION
my $de_cipher = new Crypt::CBC ($key, $encryption_algorithm, -iv=>$iv, -header=>'asdf');
my $new_plaintext = $de_cipher->decrypt($ciphertext);
print ("decrypted: ".$new_plaintext."\n") if $verbose;
print ("worked \n") if ($plaintext eq $new_plaintext);
#########################################################################
# subroutine: trim($str) #
# parameter : $str - a string to trim spaces from. #
# returns : the passed-in string with leading and trailing spaces #
# removed. #
# this subroutine removes leading and trailing spaces from the #
# passed-in string. note that the original string is not modified. #
# rather, a new string without leading/trailing spaces is returned. #
#########################################################################
sub trim
{
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
return $str;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment