Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm totally baffled. I don't understand why I see an encryption error when trying to encrypt a single character password from a Tk GUI like so:
use strict; use warnings; use Tk; use Crypt::CBC; my $plaintext; my $mw = MainWindow->new(); my $password_e = $mw->Entry( -textvariable => \$plaintext, -show => '*', -width => '10' )->pack( -side => 'top'); my $password_b = $mw->Button( -text => "Encrypt", -width => '13', -command => \&encrypt )->pack( -side => 'top', -pady => 10 ); sub encrypt { my $key = pack("H16", "0123456789ABCDEF"); print("$plaintext\n"); my $cipher = Crypt::CBC->new( -cipher => 'DES', -key => $key, ); my $ciphertext = $cipher->encrypt($plaintext); print("$ciphertext\n"); my $recovered = $cipher->decrypt($ciphertext); print("$recovered\n"); } MainLoop
This code works fine for a single charcter:
my $plaintext = 'P'; my $key = pack("H16", "0123456789ABCDEF"); print("$plaintext\n"); my $cipher = Crypt::CBC->new( -cipher => 'DES', -key => $key, ); my $ciphertext = $cipher->encrypt($plaintext); print("$ciphertext\n"); my $recovered = $cipher->decrypt($ciphertext); print("$recovered\n");
Any idea why?

2006-10-12 Retitled by g0n, as per Monastery guidelines
Original title: 'Tk Error Mystery'

Replies are listed 'Best First'.
Re: Strange Error from interaction of Tk and Crypt::CBC
by zentara (Cardinal) on Oct 11, 2006 at 17:36 UTC
    There seems to be some weird interaction when using -textvariable. I don't know what is exactly causing it, but here are 2 solutions. The first is just avoid using -textvariable. Uncomment the -textvariable line and see how it breaks the following. The second example uses padding.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Crypt::CBC; my $plaintext; my $mw = MainWindow->new(); my $password_e = $mw->Entry( # -textvariable => \$plaintext, -show => '*', -width => '10' )->pack( -side => 'top'); my $password_b = $mw->Button( -text => "Encrypt", -width => '13', -command => \&encrypt )->pack( -side => 'top', -pady => 10 ); sub encrypt { $plaintext = $password_e->get(); my $key = pack("H16", "0123456789ABCDEF"); print "text->$plaintext\n"; my $cipher = Crypt::CBC->new( -cipher => 'DES', -key => $key, ); my $ciphertext = $cipher->encrypt($plaintext); print("$ciphertext\n"); my $recovered = $cipher->decrypt($ciphertext); print("$recovered\n"); } MainLoop
    if you absolutely need to use -textvariable, you need to pad
    #!/usr/bin/perl use strict; use warnings; use Tk; use Crypt::CBC; my $plaintext; my $mw = MainWindow->new(); my $password_e = $mw->Entry( -textvariable => \$plaintext, -show => '*', -width => '10' )->pack( -side => 'top'); my $password_b = $mw->Button( -text => "Encrypt", -width => '13', -command => \&encrypt )->pack( -side => 'top', -pady => 10 ); sub encrypt { my $key = pack("H16", "0123456789ABCDEF"); print "text->$plaintext\n"; #this will allow you to use -textvariable $plaintext = "\0" x ( 8 - length($plaintext)%8 ) . $plaintext; my $cipher = Crypt::CBC->new( -cipher => 'DES', -key => $key, ); my $ciphertext = $cipher->encrypt($plaintext); print("$ciphertext\n"); my $recovered = $cipher->decrypt($ciphertext); print("$recovered\n"); } MainLoop

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thanks...I had considered using padding so I guess I'll have to go that route.