use utf8;
use strict;
use MIME::Base64;
# 文字列の encode/decode
my $encoded = encode_base64("Hello, world");
my $decoded = decode_base64($encoded);
print "Encoded: $encoded\n";
print "Decoded: $decoded\n";
# file からの encode/decode
open(INFILE, "test.txt") or die "$!";
open(ENCFILE, "+> test.enc") or die "$!"; # Read/Write で開く
open(DECFILE, "> test.dec") or die "$!";
binmode INFILE;
binmode ENCFILE;
binmode DECFILE;
my ($buf, $encbuf, $decbuf);
while (read(INFILE, $buf, 60*57)) {
$encbuf = encode_base64($buf);
print "$buf -> $encbuf\n";
print(ENCFILE $encbuf);
}
seek(ENCFILE, 0, 0); # ファイルの先頭に戻る
while (read(ENCFILE, $buf, 60*57)) {
$decbuf = decode_base64($buf);
print "$buf -> $decbuf\n";
print(DECFILE $decbuf);
}
close(INFILE);
close(ENCFILE);
close(DECFILE);
実行結果
Encoded: SGVsbG8sIHdvcmxk
Decoded: Hello, world
aaaaabbbbbcccccdddddeeeee
-> YWFhYWFiYmJiYmNjY2NjZGRkZGRlZWVlZQ0K
YWFhYWFiYmJiYmNjY2NjZGRkZGRlZWVlZQ0K
-> aaaaabbbbbcccccdddddeeeee
0 件のコメント:
コメントを投稿