一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2022-06-24 15:59:22 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

function encrypt_str( $TXT, $KEY )
{
        srand( ( double )microtime( ) * 1000000 );
        $ENCRYPT_KEY = md5( rand( 0, 32000 ) );
        $CTR = 0;
        $TMP = "";
        $I = 0;
        for ( ;    $I < strlen( $TXT );    ++$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 < strlen( $TXT );    ++$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 < strlen( $TXT );    ++$I    )
        {
                if ( $CTR == strlen( $ENCRYPT_KEY ) )
                {
                        $CTR = 0;
                }
                $TMP .= substr( $TXT, $I, 1 ) ^ substr( $ENCRYPT_KEY, $CTR, 1 );
                ++$CTR;
        }
        return $TMP;
}

热门栏目