php中生成随机字符串的函数

介绍一篇关于php中生成随机加密的函数,有需要朋友可以参考一下,是一个自定了的函数不是系统自带的.

php中生成随机字符串的函数实例代码如下:

  1. function encrypt_str( $TXT, $KEY )
  2. {
  3. srand( ( double )microtime( ) * 1000000 );
  4. $ENCRYPT_KEY = md5( rand( 0, 32000 ) );
  5. $CTR = 0;
  6. $TMP = "";
  7. $I = 0;
  8. for ( ; $I < strlen( $TXT ); ++$I )
  9. {
  10. if ( $CTR == strlen( $ENCRYPT_KEY ) )
  11. {
  12. $CTR = 0;
  13. }
  14. $TMP .= substr( $ENCRYPT_KEY, $CTR, 1 ).( substr( $TXT, $I, 1 ) ^ substr( $ENCRYPT_KEY, $CTR, 1 ) );
  15. ++$CTR;
  16. }
  17. return keyed_str( $TMP, $KEY );
  18. }
  19. function decrypt_str( $TXT, $KEY )
  20. {
  21. $TXT = keyed_str( $TXT, $KEY );
  22. $TMP = "";
  23. $I = 0;
  24. for ( ; $I < strlen( $TXT ); ++$I )
  25. {
  26. $MD5 = substr( $TXT, $I, 1 );
  27. ++$I;
  28. $TMP .= substr( $TXT, $I, 1 ) ^ $MD5;
  29. }
  30. return $TMP;
  31. }
  32. function keyed_str( $TXT, $ENCRYPT_KEY )
  33. {
  34. $ENCRYPT_KEY = md5( $ENCRYPT_KEY );
  35. $CTR = 0;
  36. $TMP = "";
  37. $I = 0;
  38. for ( ; $I < strlen( $TXT ); ++$I )
  39. {
  40. if ( $CTR == strlen( $ENCRYPT_KEY ) )
  41. {
  42. $CTR = 0;
  43. }
  44. $TMP .= substr( $TXT, $I, 1 ) ^ substr( $ENCRYPT_KEY, $CTR, 1 ); //开源代码phpfensi.com
  45. ++$CTR;
  46. }
  47. return $TMP;
  48. }