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";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.