Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Password Encryption and Decryption

by zentara (Archbishop)
on Mar 23, 2012 at 23:13 UTC ( #961333=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Password Encryption and Decryption
Select or Download Code
Re^2: Password Encryption and Decryption
by slayedbylucifer (Scribe) on Mar 24, 2012 at 11:25 UTC
    Thanks Zentara. I did come across the CryptCBC and MIMEBase64 Modules and knew that this would somehow work, but I could not get what i wanted. your examples will definitely help me. this is what i wanted. thank you very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-09-19 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (134 votes), past polls