ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/WEBTOOLS/SecurityModule/crypttest/crypttest.pl
Revision: 1.1
Committed: Wed Feb 7 11:46:11 2007 UTC (18 years, 2 months ago) by dfeichti
Content type: application/x-perl
Branch: MAIN
CVS Tags: PHEDEX-WebSite-2_5_3_1, PHEDEX_WebSite_2_5_3, forPhedex_test, V01-00-11, V01-00-10, V01-00-09, V01-00-08, V01-00-07, V01-00-06, V01-00-05, V01-00-04, V01-00-03, V01-00-02, V01-00-01, WEBTOOLS_1_0_0_pre1, V1_00_00, V00-09-08, V00-09-07, V00-09-06, V00-09-05, V00-09-04, V00-09-03, V00-09-02, V00-09-01, V00-09-00
Log Message:
- first version of the web security module

File Contents

# User Rev Content
1 dfeichti 1.1 #!/usr/bin/perl
2    
3     ################################################################
4     # crypttest.pl
5     #
6     # Version info: $Id: crypttest.pl,v 1.2 2007/02/01 23:44:38 feichtinger Exp $
7     ################################################################
8    
9     use Crypt::CBC;
10     use Carp;
11     use Getopt::Std;
12     use MIME::Base64;
13     use strict;
14    
15    
16     #####################################################
17     # PARAMETERS
18     #
19     # we use a 56 byte key
20     my $key = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabde';
21     #####################################################
22    
23    
24     sub usage {
25     print <<"EOF";
26     usage: crypttest.pl -d/-e [-i iv-vector] "message"
27     options:
28     -e : encrypt
29     -d : decrypt
30     -i iv-vector : provide a 8 byte init vector for encryption
31     EOF
32    
33     }
34    
35     my $iv;
36     my $mode='e';
37    
38     # OPTION PARSING
39     my %option=();
40     getopts("edhi:",\%option);
41    
42     if (defined $option{h}) {
43     usage();
44     exit(0);
45     }
46    
47     if (defined $option{e}) {
48     $mode='e';
49     }
50    
51     if (defined $option{d}) {
52     $mode='d';
53     }
54    
55     if (defined $option{i}) {
56     $iv = $option{i};
57     } else {
58     $iv = Crypt::CBC->random_bytes(8);
59     }
60    
61     my $text = shift;
62    
63     die "Error: No text given" if ! $text;
64     die "Error: IV needs to have length = 8 " if length($iv) != 8;
65    
66    
67     my $cipher = Crypt::CBC->new( -cipher => 'Blowfish',
68     -literal_key => 1,
69     -key => $key,
70     -iv => $iv,
71     -header => 'none',
72     -blocksize => 8,
73     -keysize => 56
74     -padding => 'standard'
75     );
76    
77    
78     if ($mode eq 'e') {
79     my $encr = encrypt($text);
80     print ">$encr<\n";
81     #print "Control check: Decrypted: " . decrypt($encr) . "\n";
82     } else {
83     print ">" . decrypt($text) . "<\n";
84     }
85    
86     exit 0;
87    
88     # we prepend the iv to the encryption
89     sub encrypt {
90     my $plain = shift;
91    
92     my $encr = $iv . $cipher->encrypt($plain);
93     my $encr64 = encode_base64($encr);
94    
95     chomp($encr64);
96     return $encr64
97     }
98    
99     sub decrypt {
100     my $encr64 = shift;
101    
102     my $encr = decode_base64($encr64);
103     my $iv = substr($encr,0,8,"");
104     $cipher->set_initialization_vector($iv);
105     my $decr = $cipher->decrypt($encr);
106    
107     return $decr;
108     }