dqkxo44488 2013-12-17 02:28
浏览 12
已采纳

PHP加密问题

A quick and dirty experiment.

I put this code into a .php file and loaded it from my web host.

The result was "It works!" but.. why? Should it have failed? I was following Example #1 from here: http://php.net/manual/en/function.crypt.php

<?php
$pass1 = "thetimeshallwhintercows";
$salt = "temperpedic";

$crypt_pass = crypt($pass1, $salt);

if($crypt_pass == crypt("thetimeshallwhintercowz", $crypt_pass))
{
    print("It works!<br/>");
    print( $crypt_pass  );
    print("<br/>");
    print(crypt("thetimeshallwhintercowz", $crypt_pass));
}
else
{
    print("try again....");
}

?>
  • 写回答

3条回答 默认 最新

  • dongqin1861 2013-12-17 02:38
    关注

    You should have a look at this answer to a similar question. the crypt() function requires that you have a correctly formatted salt. While temperpedic is a valid salt (sort of) it's not really a correctly formatted salt.

    If you have a look at the PHP documentation for the crypt() function there are a few examples of using crypt() with different hash types. Have a look at these examples.

    Remember, with crypt for modern web applications, you should be using at least SHA-256.

    <?php
    $pass1 = "thetimeshallwhintercows";
    $salt = "temperpedic";
    
    echo 'SHA-256:      ' . crypt($pass1, '$5$rounds=5000$' . $salt . '$') . "
    ";
    echo 'SHA-256:      ' . crypt($pass1, '$5$rounds=5000$' . $salt . 'extra$') . "
    ";
    echo 'SHA-256:      ' . crypt($pass1, '$5$rounds=5000$' . $salt . 'evenextra$') . "
    ";
    
    ?>
    
    tim@roflcopter /tmp $ php lol.php
    SHA-256:      $5$rounds=5000$temperpedic$4g0qFd4Oqr/O.8aZMPiyrO9x5VUaQt14eXPOMr5asK2
    SHA-256:      $5$rounds=5000$temperpedicextra$3BF4dmqrCBuY2UtQpuhxXm4t4KGp1M9OoJPrskM490/
    SHA-256:      $5$rounds=5000$temperpedicevene$jBsGNFGSAbuL8hdcXsZjHRrH6u4qnXb1bAJ.TOR32A2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了