Home >php教程 >php手册 >php中生成随机字符串的函数

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

WBOY
WBOYOriginal
2016-06-13 10:12:031116browse

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

 代码如下 复制代码

function encrypt_str( $TXT, $KEY )
{
        srand( ( double )microtime( ) * 1000000 );
        $ENCRYPT_KEY = md5( rand( 0, 32000 ) );
        $CTR = 0;
        $TMP = "";
        $I = 0;
        for ( ;    $I         {
                if ( $CTR == strlen( $ENCRYPT_KEY ) )
                {
                        $CTR = 0;
                }
                $TMP .= substr( $ENCRYPT_KEY, $CTR, 1 ).( substr( $TXT, $I, 1 ) ^ substr( $ENCRYPT_KEY, $CTR, 1 ) );
                ++$CTR;
        }
        return keyed_str( $TMP, $KEY );
}

function decrypt_str( $TXT, $KEY )
{
        $TXT = keyed_str( $TXT, $KEY );
        $TMP = "";
        $I = 0;
        for ( ;    $I         {
                $MD5 = substr( $TXT, $I, 1 );
                ++$I;
                $TMP .= substr( $TXT, $I, 1 ) ^ $MD5;
        }
        return $TMP;
}

function keyed_str( $TXT, $ENCRYPT_KEY )
{
        $ENCRYPT_KEY = md5( $ENCRYPT_KEY );
        $CTR = 0;
        $TMP = "";
        $I = 0;
        for ( ;    $I         {
                if ( $CTR == strlen( $ENCRYPT_KEY ) )
                {
                        $CTR = 0;
                }
                $TMP .= substr( $TXT, $I, 1 ) ^ substr( $ENCRYPT_KEY, $CTR, 1 );
                ++$CTR;
        }
        return $TMP;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn