アットウィキロゴ
プレーンテキスト備忘録
掲示板 掲示板 ページ検索 ページ検索 メニュー メニュー

プレーンテキスト備忘録

可逆暗号Crypt-RC4

最終更新:

Bot(ページ名リンク)

- view
管理者のみ編集可

可逆暗号Crypt::RC4

環境:Windows XP 32bit, Active Perl 5.12.4

事前準備:Active Perlをインストール後、コマンドプロンプトを開き
     「cpan」と入力後「install Crypt::RC4」(だったかな?)を入力すると自動でインストールが始まる。
     全て終わったら「q」でcpanを抜ける。

配布時は配下に「Crypt」フォルダを作成し、その中に「RC4.pm」を入れておけば動作する。
Crypt::RC4はディレクトリ構成らしい。


use Crypt::RC4;
my $passphrase = 'phrasetest'; # 暗号化キー(自由な英数字)
my $plaintext = 'mypassword'; # 暗号化したい文字列

print "暗号化前 = $plaintext\n";

# 暗号化キーを用いて暗号化する関数へ
my $encrypted = encrypt($passphrase, $plaintext);

print "暗号化後 = $encrypted\n";

# 暗号化キーを用いて復号化する関数へ
my $decrypted = decrypt($passphrase, $encrypted);

print "復号化  = $decrypted\n";

sub encrypt{
    my ($passphrase, $plaintext) = @_;
    my $encrypted = RC4($passphrase, $plaintext);
    $encrypted =~ s/(.)/unpack('H2', $1)/eg;
    return $encrypted;
}

sub decrypt{
    my ($passphrase, $encrypted) = @_;
    $encrypted =~ s/([0-9A-Fa-f]{2})/pack('H2', $1)/eg;
    my $decrypted = RC4($passphrase, $encrypted);
    return $decrypted;
}

実行結果

   暗号化前 = mypassword
   暗号化後 = 9f81c462a3ec0ed72f97
   復号化  = mypassword

名前:
コメント:

すべてのコメントを見る
最近更新されたスレッド
ウィキ募集バナー