##################UPDATE################
The problems above have been fixed here
use strict;
use warnings;
use Tk 800.000;
use Tk::Frame;
use Tk::TextUndo;
use Tk::Menu;
use Tk::Menubutton;
use Tk::DialogBox;
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 => '50');
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', '<Control-Key-o>',
sub { OnFileLoad(); }
);
$mw->bind('Tk::TextUndo', '<Control-Key-s>',
sub { OnFileSave(); }
);
$mw->bind('<Control-Key-q>',
sub { OnExit(); }
);
$mw->bind('Tk::TextUndo', '<Control-Key-e>',
sub { OnEncrypt(); }
);
$mw->bind('Tk::TextUndo', '<Control-Key-d>',
sub { OnDecrypt(); }
);
$mw->bind('<Control-Key-a>',
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
+ /);
$decrypt_button->pack(qw/-side right
+ /);
$get_key_button->pack(qw/
+ /);
$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 $usedkey;
if(defined $key)
{
$usedkey = $key;
}
elsif(undef $key)
{
$key = get_key();
$usedkey = $key;
}
else
{
return $key
}
my $cipher = Crypt::CBC->new( {'key' => "$usedkey",
'cipher' => 'DES',
'iv' => '$KJh#(}q',
'regenerate_key' => 0, # default tr
+ue
'padding' => 'space',
'prepend_iv' => 0
});
return $cipher;
}
sub get_key
{
my $getkeybox = $mw->DialogBox(-title=>"Set Encryption Key",
-buttons=>["Ok", "Cancel"]
);
$getkeybox->add('Label',
-anchor => 'w',
-justify => 'left',
-text => qq(Please enter your Encryption Key))->pa
+ck(qw/-side top -anchor w/);
my $keybox = $getkeybox->add('Entry',
-takefocus => '1',
-textvariable => "$key",
)->pack(qw/-side top -fill both -expand 1
+ -anchor w/);
my $button = $getkeybox->Show();
$key = pack("H16", $keybox->get) unless $button eq "Cancel";
print $key;
if($button eq "Cancel"){
if(defined $key)
{
return $key
}
elsif(undef $key)
{
return $key
}
}
return $key;
}
Thanks for the help
pg.
Eoin.