use strict; use warnings; use Tk; use Crypt::CBC; use Crypt::DES; my($key); my $mw = new MainWindow; $mw->geometry('800x600'); my $menu = $mw->Menu(-menuitems => &menubar_menuitems() ); $mw->configure(-menu => $menu); my $tpfrme = $mw->Frame; my $mdfrme = $mw->Frame(-height => '60'); my $bmfrme = $mw->Frame; my($text1) = $tpfrme->Scrolled('TextUndo', -height => '1', -width => '1', -scrollbars => 'osoe', ); my $encrypt_button = $mdfrme->Button(-text=>"Encrypt", -height=>'1', -command=>[\&OnEncrypt] ); my $get_key_button = $mdfrme->Button(-text=>"Set Encryption Key", -height=>'1', -command=>[\&get_key] ); my $decrypt_button = $mdfrme->Button(-text=>"Decrypt", -height=>'1', -command=>[\&OnDecrypt] ); my($text2) = $bmfrme->Scrolled('TextUndo', -height => '1', -width => '1', -scrollbars => 'osoe', ); $mw->bind('Tk::TextUndo', '', sub { OnFileLoad(); } ); $mw->bind('Tk::TextUndo', '', sub { OnFileSave(); } ); $mw->bind('', sub { OnExit(); } ); $mw->bind('Tk::TextUndo', '', sub { OnEncrypt(); } ); $mw->bind('Tk::TextUndo', '', sub { OnDecrypt(); } ); $mw->bind('', sub { OnAbout(); } ); $tpfrme ->pack(qw/-side top -fill both -expand 1 /); $mdfrme ->pack(qw/-side top -fill both -expand 0 /); $bmfrme ->pack(qw/-side bottom -fill both -expand 1 /); $text1 ->pack(qw/-side top -fill both -expand 1 /); $encrypt_button->pack(qw/-side left /); $get_key_button->pack(qw/ -anchor center/); $decrypt_button->pack(qw/-side right /); $text2 ->pack(qw/-side top -fill both -expand 1 /); MainLoop; sub menubar_menuitems{ return [ map ['cascade', $_->[0], -tearoff=> 0, -menuitems => $_->[1]], ['~File', &file_menuitems() ], ['~Actions', &actions_menuitems()], ['~Help', &help_menuitems() ], ]; } sub file_menuitems { return [ [qw/command ~Open -accelerator Ctrl-o/, -command=>[\&OnFileLoad]], [qw/command ~Save -accelerator Ctrl-s/, -command=>[\&OnFileSave]], '', [qw/command ~Exit -accelerator Ctrl-q/, -command=>[\&OnExit]] ]; } sub actions_menuitems { return [ [qw/command ~Encrypt -accelerator Ctrl-e/, -command=>[\&OnEncrypt]], [qw/command ~Decrypt -accelerator Ctrl-d/, -command=>[\&OnDecrypt]], ]; } sub help_menuitems { return [ [qw/command ~About -accelerator Ctrl-a/, -command=>[\&OnAbout] ] ]; } sub OnFileLoad { $text1->FileLoadPopup(); } sub OnFileSave { $text2->FileSaveAsPopup(); } sub OnExit { exit 0; } sub OnEncrypt { my $text = $text1->get(0.1, 'end') ; my $encrypted_data = encrypt_string($text) ; my $widget = $text2->Subwidget("text") ; tie *STDOUT, ref $widget, $widget; print $encrypted_data; } sub OnDecrypt { my $text = $text1->get(0.1, 'end') ; my $decrypted_data = decrypt_string($text) ; my $widget = $text2->Subwidget("text") ; tie *STDOUT, ref $widget, $widget; print $decrypted_data; } sub encrypt_string { my $cipher = new_cipher() ; my $string = shift ; my $encryp_data = $cipher->encrypt($string); $cipher = "" ; return $encryp_data ; } sub decrypt_string { my $cipher = new_cipher() ; my $string = shift ; my $decryp_data = $cipher->decrypt($string); $cipher = "" ; return $decryp_data ; } sub new_cipher { my $key = get_key(); my $cipher = Crypt::CBC->new( {'key' => "$key", 'cipher' => 'DES', 'iv' => '$KJh#(}q', 'regenerate_key' => 0, # default true 'padding' => 'space', 'prepend_iv' => 0 }); return $cipher; } sub get_key { my $getkeybox = $mw->DialogBox(-title=>"Set Encryption Key", -buttons=>["OK"] ); $getkeybox->add('Label', -anchor => 'w', -justify => 'left', -text => qq(Please enter your Encryption Key))->pack(qw/-side top -anchor w/); my $keybox = $getkeybox->add('Scrolled', 'TextUndo', -height => '10' )->pack(qw/-side top -fill both -expand 1 -anchor w/); my $result = $getkeybox->show(); $key = $keybox->get(0.1, 'end') unless(undef $result); }