=readme charset shift-jis LastModified : 2005-08/20 /_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ Arcfour : encrypt / decrypt http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt Written by kerry http://202.248.69.143/~goma/ /_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ Usage # 平文・暗号文の暗号化・復号化 *cipherText = arcfour'crypter( *plainText, *key ); # ファイルの暗号化・復号化 *cipherText = arcfour'fcrypter( *infile, *key ); # ファイルを暗号化・復号化しファイルに書き出す $bool = arcfour'fcrypter( *infile, *key, *outfile ); + 暗号化、復号化ともに同じサブルーチンへ $plainText は $cipherText, $cipherText は $plainText と読み替えることができる + 引数は全て参照渡しで統一 + $key は 0-256 byte + $bool は何かファイルで問題が起こっていれば 偽 =cut package arcfour; sub crypter { (*data, *key, $thrift) = @_; $buf = ""; &keyUpdata; $i = $j = 0; if ($thrift) { $data =~ s/(.)/ pack "C", unpack("C", $1)^&getKeyByte /eg; return *data; } else { $buf .= pack "C", $_ ^ &getKeyByte for unpack "C*", $data; return *buf; } } sub fcrypter { (*infile, *key, *outfile) = @_; local (*buf)= ""; $buffering = @_ < 3; $bufSize = 2**9; if (!-e $infile) { return 0; die "$infile は存在しません" } if (!$buffering and !-e $outfile) { return 0; die "$outfile は存在しません" } if (!open IN, $infile) { return 0; die "$infile を開けません" } if (!$buffering) { if (open OUT, "> $outfile") { binmode OUT; } else { close IN; return 0; die "$outfile を開けません" } } binmode IN; &keyUpdata; $i = $j = 0; while (read IN, $tmp, $bufSize) { $buf .= pack "C", $_ ^ &getKeyByte for unpack "C*", $tmp; if (!$buffering) { print OUT $buf; $buf = ""; } } close IN; if ($buffering) { return *buf; } else { close OUT; } } sub getKeyByte { $i = ($i+1) % 256; $j = ($j+ $s[$i]) % 256; ($s[$i], $s[$j]) = ($s[$j], $s[$i]); return $s[ ($s[$i]+ $s[$j]) % 256 ]; } sub keyUpdata { local ($keyLen) = length $key; $keyLen = 0xff if $keyLen > 0xff; @key = unpack "C$keyLen", $key; $s[$_] = $_ for 0..0xff; $s2[$_] = $key[ $_ % $keyLen ] for 0..0xff; $j = 0; for (0..0xff) { $j = ($j+ $s[$_]+ $s2[$_]) % 256; ($s[$_], $s[$j]) = ($s[$j], $s[$_]); } undef @s2; undef @key; } &arg_crypter if @ARGV; sub arg_crypter { ($txt, $key) = @ARGV; $bufSize = 2**9; $| = 1; binmode STDOUT; &arg_help if @ARGV < 2; if ($key =~ m|^/(.+)|) { if (open IN, $1) { binmode IN; read IN, $key, 0xff; close IN; } else { if (-e $1) { printf "キーファイル\x22%s\x22を開けません\n", $1;} else { printf "キーファイル\x22%s\x22は存在しません\n", $1;} exit; } } &keyUpdata; if ($txt =~ m|^/(.+)|) { if (open IN, $1) { while (read IN, $buf, $bufSize) { print pack "C", $_ ^ &getKeyByte for unpack "C*", $buf; } close IN; } else { if (-e $1) { printf "ファイル\x22%s\x22を開けません\n", $1;} else { printf "ファイル\x22%s\x22は存在しません\n", $1;} exit; } } else { print pack "C", $_ ^ &getKeyByte for unpack "C*", $txt; } } sub arg_help { print <<_HTML_; ========================================= Arcfour.pl Powered by kerry http://202.248.69.143/~goma/ ========================================= Usage; 暗号化、復号化共に同じ書式; はプレイン・テキスト又は暗号化済みデータ。ファイルを指定する場合 は先頭文字を / スラッシュにする。例えばカレント・ディレクトリにあ るmytext.txtというファイルを暗号化、復号化する場合には > Perl /mytext.txt "mykey" となる。一文字目がスラッシュであった場合それ以降をパスとする。 暗号化、復号化時に使用するキーを256byte以内で指定する。ファイルを 指定する場合は先頭文字を / スラッシュにする。 _HTML_ exit; } 1;