PHP中加密解密字串函數原始碼

來源:互聯網
上載者:User
PHP中加密解密字串函數原始碼:
  1. /**
  2. *功能:對字串進行加密處理
  3. *參數一:需要加密的內容
  4. *參數二:密鑰
  5. */
  6. function passport_encrypt($str,$key){ //加密函數
  7. srand((double)microtime() * 1000000);
  8. $encrypt_key=md5(rand(0, 32000));
  9. $ctr=0;
  10. $tmp='';
  11. for($i=0;$i $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
  12. $tmp.=$encrypt_key[$ctr].($str[$i] ^ $encrypt_key[$ctr++]);
  13. }
  14. return base64_encode(passport_key($tmp,$key));
  15. }
  16. /**
  17. *功能:對字串進行解密處理
  18. *參數一:需要解密的密文
  19. *參數二:密鑰
  20. */
  21. function passport_decrypt($str,$key){ //解密函數
  22. $str=passport_key(base64_decode($str),$key);
  23. $tmp='';
  24. for($i=0;$i $md5=$str[$i];
  25. $tmp.=$str[++$i] ^ $md5;
  26. }
  27. return $tmp;
  28. }
  29. /**
  30. *輔助函數
  31. */
  32. function passport_key($str,$encrypt_key){
  33. $encrypt_key=md5($encrypt_key);
  34. $ctr=0;
  35. $tmp='';
  36. for($i=0;$i $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
  37. $tmp.=$str[$i] ^ $encrypt_key[$ctr++];
  38. }
  39. return $tmp;
  40. }
  41. $str='作者:uphtm.com;
  42. $key='uphtm.com';
  43. $encrypt=passport_encrypt($str,$key);
  44. $decrypt=passport_decrypt($encrypt,$key);
  45. echo '原文:',$str."
    ";
  46. echo '密文:',$encrypt."
    ";
  47. echo '譯文:',$decrypt."
    ";
  48. ?>
複製代碼
加密解密, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.