Perl で暗号化/複合化 に関するメモ(2002/07/04) 
 

Perlで暗号化、複合化を行う手法について

Crypt::CBC を使用することにより、DES/IDEA/Blowfish/Blowfish_PP の4種類の暗号化方式がつかえます。
この暗号化方法を使用することにより、暗号化/複合化を簡単に行う事ができます。

  参考 : 要素技術と留意点

  参考 : Crypt::CBC 

 

設定方法

1.CPAN からソースをもってきて、インストールします。

1.
% wget http://www.cpan.org/authors/id/LDS/Crypt-CBC-2.05.tar.gz
% gzip -cd Crypt-CBC-2.05.tar.gz | tar xf -
% cd Crypt-CBC-2.05
% perl Makefile.PL
% make
% su
# make install

2.
% wget http://www.cpan.org/authors/id/GAAS/Digest-MD5-2.20.tar.gz
% gzip -cd Digest-MD5-2.20.tar.gz | tar xf -
% cd Digest-MD5-2.20
% perl Makefile.PL
% make
% su
# make install

3.
% wget http://www.cpan.org/authors/id/D/DE/DELTA/Crypt-Rijndael_PP-0.04.tar.gz
% gzip -cd Crypt-Rijndael_PP-0.04.tar.gz | tar xf -
% cd Crypt-Rijndael_PP-0.04
% perl Makefile.PL
% make
% su
# make install

3.
% wget http://www.cpan.org/authors/id/D/DP/DPARIS/Crypt-Blowfish-2.09.tar.gz
% gzip -cd Crypt-Blowfish-2.09.tar.gz | tar xf -
% cd Crypt-Blowfish-2.09
% perl Makefile.PL
% make
% su
# make install

3.
% wget http://www.cpan.org/authors/id/D/DP/DPARIS/Crypt-DES-2.03.tar.gz
% gzip -cd Crypt-DES-2.03.tar.gz | tar xf -
% cd Crypt-DES-2.03
% perl Makefile.PL
% make
% su
# make install

サンプル

1.DES方式のサンプル

use Crypt::CBC;
my $key = 'my secret key';
my $cipher = Crypt::CBC->new($key, 'DES');

my $input = 'The answer is 42.';
my $ciphertext = $cipher->encrypt_hex($input);
my $plaintext = $cipher->decrypt_hex($ciphertext);

print "ciphertext = $ciphertext\n";
print "plaintext = $plaintext\n";

% perl sample.pl
ciphertext = 52616e646f6d49561f63125ef4b3e444a570b6e1ed1fbd546410108a813a8c0943fad0dcf28ab709
plaintext = The answer is 42.

2.Rijndael_PPのサンプル

use Crypt::CBC;

my $key = 'my secret key';
my $input = 'The answer is 42.';
my $cipher = new Crypt::CBC($key,'Rijndael_PP');

my $ciphertext = $cipher->encrypt_hex($input);
my $plaintext = $cipher->decrypt_hex($ciphertext);

print "ciphertext = $ciphertext\n";
print "plaintext = $plaintext\n";

% perl sample.pl
ciphertext = 52616e646f6d49566477e0c3f5d427c45c56ed7105b7ddeb75c06cebf6a41c142d93af10ead700014ed5c48
3ce3f1d5a
plaintext = The answer is 42.

3.Blowfishのサンプル

use Crypt::CBC;
$cipher = Crypt::CBC->new( {'key' => 'my secret key',
'cipher' => 'Blowfish',
'iv' => '12kak234',
'regenerate_key' => 0, # default true
'padding' => 'space',
'prepend_iv' => 0
});

my $input = 'The answer is 42.';

my $ciphertext = $cipher->encrypt_hex($input);
my $plaintext = $cipher->decrypt_hex($ciphertext);

print "ciphertext = $ciphertext\n";
print "plaintext = $plaintext\n";

% perl sample.pl
ciphertext = 57a26235c23b04d0cf5ddd87ea58695be6f5987942966401
plaintext = The answer is 42.