お手軽暗号化

                             <p>ソースコード</p>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl

#BEGIN{
#   print "Content-type: text/plainnn";
#   open(STDERR, ">&amp;STDOUT");
#   $|=1;
#}

$usr_title = 'テスト30';

require "tsenv.pl";

{
    printHeader($usr_title);
    printBodyHeader('<font face=times>Crypt Test</font>');

# 乱数初期化
    srand;
    crypt_test('12345678', '12345678');
    crypt_test('987654321', '987654320');
    crypt_test('abcdefgh', 'abcdefgi');

    new_crypt_test('12345678', '12345678');
    new_crypt_test('987654321', '987654320');
    new_crypt_test('abcdefgh', 'abcdefgi');

    print "<pre>";

    print "Normal localtime : ";
    print localtime();
    print "n";

    print "Scalar localtime : ";
    print scalar(localtime());
    print "n";

    print "Expand localtime : ";
    print join(", ", localtime());
    print "n";

    print "n";

    print "int(1.5) = ";
    print int(1.5);
    print "n";

    print "int(2.9) = ";
    print int(2.9);
    print "n";

    print "int(-2.5) = ";
    print int(-2.5);
    print "n";

    print "int(-2.9) = ";
    print int(-2.9);
    print "n";

    print "int(-1.1) = ";
    print int(-1.1);
    print "n";

    print "</pre>";

    printFooter();

    exit(0);
}

sub crypt_test{
    my($setpass, $authpass) = @_;
# 設定パスワード確認
    print qq(<table border=1 width="80%">n<tr>n<td width="40%">n);
    Jprint("設定パスワード:$setpass<br>n");
# salt値作成
    my @char = (".", "/", "0".."9", "a".."z", "A".."Z");
    my $salt = '';
    for(0,1){
        $salt .= $char[int(rand(scalar(@char)))];
    }
# salt値の確認
    Jprint("Salt値:$salt<br>n");
# cryptする
    $sp = crypt($setpass, $salt);
    Jprint("crypt結果:$sp<br>n");

# 認証パスワード確認
    print qq(<td width="40%">n);
    Jprint("認証パスワード:$authpass<br>n");
# 作成したパスワードから「salt値」を取得
    if($sp =~ /^$1$/){
        $offset = 3;
    }else{
        $offset = 0;
    }
    $salt = substr($sp, $offset, 2);
# salt値の確認
    Jprint("Salt値:$salt<br>n");
# cryptする
    $ap = crypt($authpass, $salt);
    Jprint("crypt結果:$ap<br>n");

# 結果は同じ?
    if($sp eq $ap){
        $result = 'OK';
    }else{
        $result = 'NG';
    }
    print "<tr>n<td colspan=2 align=center>n";
    Jprint("認証結果:$result<br>n");
    print "</table><br>nn";
}

# 新しい(というか、正しい)cryptの使い方
# 中身は手抜き・・・(^^;;;
sub new_crypt_test{
    my($PW, $enterPW) = @_;
    my $salt = './'; # salt値作成、手抜き
    my $cryptedPW = crypt($PW, $salt);
    my $result = '';
    if( crypt($enterPW, $cryptedPW) eq $cryptedPW ){
        $result = 'OK';
    }else{
        $result = 'NG';
    }
    print <<EOM;

<pre>
設定パスワード : $PW
認証パスワード : $enterPW
照合結果    : $result
</pre>
EOM
}
                          </div>

Comments

comments powered by Disqus