http://www.perlmonks.org?node_id=961333


in reply to Password Encryption and Decryption

Could you give me guidance on how to proceed.

Your main problem with keeping an encrypted string in your program, is that most encrypted output is binary, and not printable. So.... you will need a technique to base64encode the encrypted output, THEN base64decode the string back to binary, before decryption. Pack and unpack could also be used.

You could also hide your key in plain site, but hidden in your script, like seek to line 42, and grab the 3rd word. :-) Few novices would follow the code to the the key, but its poor security.

Here are the basic techniques. You will find that anyone who knows even a bit of Perl, will be able to hack this. You cannot decrypt a password in a script, without the password being in the script somewhere. You can use RSA keys, to decrypt a key, because the RSA key is in another file, but that depends on home directories, user groups, and permissions setup correctly. Your easiest bet is to use a technique like pack() or MIME_BASE64 to just obscure your key.

#!/usr/bin/perl use warnings; use strict; use Crypt::CBC; use MIME::Base64; my $KEY = 'secret_foo'; my $string = 'yadda yadda yadda yadda'; print "input: $string\n"; my $enc = encryptString( $string ); print "encrypted binary: $enc\n"; my $mime = encode_base64($enc); print "MIME: $mime\n"; my $mime_decode = decode_base64($mime); print "MIME_decode: $mime_decode\n"; my $dec = decryptString( $enc ); print "decrypted: $dec\n"; my $mime_dec = decryptString( decode_base64($mime) ); print "decrypted_mime: $mime_dec\n"; ############################################################ sub encryptString { my $string = shift; my $cipher = Crypt::CBC->new( -key => $KEY, -cipher => 'Blowfish', -padding => 'space', -add_header => 1 ); my $enc = $cipher->encrypt( $string ); return $enc; } ################################################################### sub decryptString { my $string = shift; my $cipher = Crypt::CBC->new( -key => $KEY, -cipher => 'Blowfish', -padding => 'space', -add_header => 1 ); my $dec = $cipher->decrypt( $string ); return $dec; } #############################################################3
And here is a way of obscuring your key with pack(). You could also use encode_base64 to obscure your key.
#!/usr/bin/perl use warnings; use strict; #by fokat of perlmonks my $string = 'justanotherperlhacker'; print "$string\n"; my $obscure = pack("u",$string); print "$obscure\n"; my $unobscure = unpack(chr(ord("a") + 19 + print ""),$obscure); print "$unobscure\n";

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh