Skip to content

Instantly share code, notes, and snippets.

@Allan-Gong
Last active December 1, 2015 00:18
Show Gist options
  • Save Allan-Gong/9c16eddf9b36cf9979a7 to your computer and use it in GitHub Desktop.
Save Allan-Gong/9c16eddf9b36cf9979a7 to your computer and use it in GitHub Desktop.
Encrypts the plaintext string using the passphrase. Whitespace characters are appended to the string if its length is not a multiple of eight. User applications can correct for this by storing plaintext size with the cyphertext. The passphrase is an ASCII character string of upto 48 characters.
###################################
#########Encrypt example###########
###################################
use Data::Random qw( rand_chars );
use Crypt::TripleDES;
my $password = q{password};
my $password_len = length $password;
my $password_len_chars = sprintf('%02d',$password_len);
my $password_padding_len = 8 - ($password_len % 8);
my $rand_arr = join q{}, rand_chars(
'set' => 'alphanumeric',
'size' => $password_padding_len + 6,
);
my $completed_password = join q{}, $password, $rand_arr, $password_len_chars;
my $des= Crypt::TripleDES->new();
my $secret_text = q{secret text};
my $encrypted_password = $des->encrypt3($completed_password, $secret_text);
###################################
#########Decrypt example###########
###################################
# To decrypt the encrypted password from the above example
my $des = Crypt::TripleDES->new();
my $secret_text = q{secret text};
my $decrypted_string = $des->decrypt3($encrypted_password, $secret_text);
my $decrypted_password_length = substr($decrypted_string, -2);
my $decrypted_password = substr($decrypted_string, 0, $decrypted_password_length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment