-2

さて、これは「私のコードをデバッグしてください」という質問ではありません。私は費やしました...私はこの問題を修正しようとしてどれくらいの時間を考えたくありません。

私の問題は、このphpページを実行していることです。このページはアカウントのパスワードを保存するためのものであるため、理想的には文字列を調べてより安全な形式に変換します。

以下のこのコードで私が目指しているのは、各文字で文字列を繰り返しスライスし、それを自分のキーで評価することです。これは、文字列全体の長さに対して行われます。私に返されているのは「0」です。システムがこの値をどのように取得しているのかわかりません。

たぶん私はsubstr()関数を間違って使用していますか?また、RegExpを使用するなど、文字列を解析するまったく異なる方法を利用できます。皆さん、助けてくれてありがとう!

コード:

<?php
error_reporting(0);
#region Initial
$accstr = "apple";
$accn = "scourge";
//Key
$a[0] = "#"; //These variables are for conversion; indexes of this array correspond to those
$a[1] = "!"; //of the other array ($ind)
$a[2] = "@";
$a[3] = "%$";
$a[4] = "%";
$a[5] = "!@";
$a[6] = "&*";
$a[7] = "^";
$a[8] = "##";
$a[9] = "&^";
$a[10] = "&";
$a[11] = "%~";
$a[12] = "!%";
$a[13] = "!$";
$a[14] = "*#";
$a[15] = "#*";
$a[16] = "~";
$a[17] = "~&";
$a[18] = "``";
$a[19] = "/^";
$a[20] = "%`";
$a[21] = "~~";
$a[22] = "`~";
$a[23] = "%%";
$a[24] = "~!";
$a[25] = "~#";
$a[26] = "``#";
$a[27] = "``!";
$a[28] = "``@";
$a[29] = "``%$";
$a[30] = "``%";
$a[31] = "``!@";
$a[32] = "``&*";
$a[33] = "``^";
$a[34] = "``##";
$a[35] = "``&^";
$a[36] = "&&^#";
$a[37] = "~@!";
$a[38] = "!@&@";
$a[39] = "%~~$";
$a[40] = "%`%";
$a[41] = "!^~@";
$a[42] = "&#$*";
$a[43] = "^**&";
$a[44] = "#%#`";
$a[45] = "&``!@^";
$a[46] = "&**~&";
$a[47] = "%|~";
$a[48] = "!-|~%";
$a[49] = "!$~";
$a[50] = "*/#";
$a[51] = "#%*";
$a[52] = "|~";

$ind[0] = "a";//These are used to tell what's being looked at in the string
$ind[1] = "b";
$ind[2] = "c";
$ind[3] = "d";
$ind[4] = "e";
$ind[5] = "f";
$ind[6] = "g";
$ind[7] = "h";
$ind[8] = "i";
$ind[9] = "j";
$ind[10] = "k";
$ind[11] = "l";
$ind[12] = "m";
$ind[13] = "n";
$ind[14] = "o";
$ind[15] = "p";
$ind[16] = "q";
$ind[17] = "r";
$ind[18] = "s";
$ind[19] = "t";
$ind[20] = "u";
$ind[21] = "v";
$ind[22] = "w";
$ind[23] = "x";
$ind[24] = "y";
$ind[25] = "z";
$ind[26] = "0";
$ind[27] = "1";
$ind[28] = "2";
$ind[29] = "3";
$ind[30] = "4";
$ind[31] = "5";
$ind[32] = "6";
$ind[33] = "7";
$ind[34] = "8";
$ind[35] = "9";
$ind[36] = "~";
$ind[37] = "!";
$ind[38] = "@";
$ind[39] = "#";
$ind[40] = "$";
$ind[41] = "%";
$ind[42] = "^";
$ind[43] = "&";
$ind[44] = "*";
$ind[45] = "(";
$ind[46] = ")";
$ind[47] = "_";
$ind[48] = "+";
$ind[49] = "`";
$ind[50] = "-";
$ind[51] = "=";
$ind[52] = "?";

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$finln = "";
#endregion

#region Create coded password
$pwlen = strlen($accstr);
for($cnter=1;$cnter<=$pwlen;$cnter++)
    {
        $a1 = substr($accstr,$cnter,1);
        for($cnter2=1;$cnter2<=52;$cnter2++)
            {
                if($a1==$ind[$cnter2])
                    {
                        $finln += $a[$cnter2];
                    }
            }
    }
#endregion

#region Send finln
$newpw = $xml->createElement($accn);
$newpw->appendChild($xml->createElement('password', $finln));
$xml->getElementsByTagName('cache')->item(0)->appendChild($newpw);
file_put_contents("pwDB.xml",$xml->saveXML());
print $finln;
#endregion
?>
4

1 に答える 1

1

したがって、典型的なパスワードハッシュは一方向です。双方向が必要な場合は、異なる暗号化について話していることになります。

通常、ハッシュ化するには次のようなことを行いますが、これをそのまま受け入れるのではなく、自分でいくつかを調べて、自分が何をしているのかと関連する概念を理解することをお勧めします。

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');

$account = 'someuser';
$password = 'passw0rd';

// your salt can be a constant that you never change, or can be user specific
// if you make it user specific then you need to store it as well as the password
$salt = "1j0i90@$t%";

$hash = hash('sha256', $password . $salt);

$acct = $xml->createElement($account);
$pw = $xml->createElement('password', $salt);
$acct->appendChild($pw);

$xml->appendChild($acct);

file_put_contents("pwDB.xml",$xml->saveXML());

そして、ログインのように資格情報を比較するには、次のようにします。

    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->load('pwDB.xml');

    $account = 'someuser';
    $password = 'passw0rd';
    $salt = "1j0i90@$t%";

    $hash = hash('sha256', $password . $salt);
    $xpath = new DOMXPath($xml);

   // look up by account name - assuming these are unique
   $accountNodes = $xpath->query('//'.$account);
   if($accountNodes->length) {
      $accountNode = $accountNodes->item(0);
      $pwNodes = $xpath->query('//password', $accountNode);
      if($pwNodes->length) {
         $pwNode = $pwNodes->item(0);
         if($hash === (string) $pwNode) {
             // authentication OK!
         } 
      }
   }
于 2012-06-26T19:48:42.810 に答える