Home > Backend Development > PHP Tutorial > Function to generate random string in php_PHP tutorial

Function to generate random string in php_PHP tutorial

WBOY
Release: 2016-07-13 17:10:17
Original
895 people have browsed it

Introducing an article about the function that generates random encryption in PHP. If you need it, you can refer to it. It is a self-defined function and does not come with the system.

The code is as follows
 代码如下 复制代码

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;
}

Copy code
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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629683.htmlTechArticleIntroduces an article about the function that generates random encryption in php. If you need it, friends can refer to it. It is a customized one. The function does not come with the system. The code is as follows Copy code function encr...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template