search
HomeBackend DevelopmentPHP TutorialHow to use 32-digit php to encrypt and decrypt IDs (code attached)

This article introduces to you how to use PHP to encrypt and decrypt IDs using hexadecimal 32 (code attached). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

I recently encountered a problem in the project. The current user shares an invitation code with a friend. After the friend registers as a new user based on the invitation code, he becomes the current user's subordinate. Under certain conditions, the subordinate user's information can be obtained. A range of rebates. What is implemented here is to generate an encrypted string based on the current user's id, which can be reversely decrypted. After constant testing and adjustments, the final result was finally obtained. For example:

id = 12 code = 85U43DM

First implementation

First enter the code, as follows:

/**
 * 加密解密用户邀请码,
 * @param unknown $string
 * @param string $action  encode|decode
 * @return string
 */
function endecodeUserId($string, $action = 'encode') {
    $startLen = 13;
    $endLen = 8;

    $coderes = '';
    #TOD 暂设定uid字符长度最大到9
    if ($action=='encode') {
        $uidlen = strlen($string);
        $salt = 'yourself_code';
        $codestr = $string.$salt;
        $encodestr = hash('md4', $codestr);
        $coderes = $uidlen.substr($encodestr, 5,$startLen-$uidlen).$string.substr($encodestr, -12,$endLen);
        $coderes = strtoupper($coderes);
    }elseif($action=='decode'){
        $strlen = strlen($string);
        $uidlen = $string[0];
        $coderes = substr($string, $startLen-$uidlen+1,$uidlen);
    }
    return  $coderes;
}

Introduction to the idea:

Set a salt value, $salt, and concatenate it with the id to form a new string. The salt value can be used to securely calibrate the invitation code later. test. Perform md4 encryption on the string (considering that md4 is faster and less secure than md5), get $encodestr, split the string into two parts, the first part $startLen , 13 strings; the second part $endLen, 8 strings. Mix $string, here the passed in id, and $uidlen into the previous part of the string. Therefore, it currently only supports a maximum length of id of 9, so the length of $uidlen is 1, so in the end we get a string of length 22.

During the encryption process, we actually mix the value of the id and the length of the id into the encrypted string. During encryption, we find the corresponding position based on the stored information to get the id.

Here, we do not have high requirements for security. In order to make the program run faster, there is no verification during decryption.

Test, encrypt the id:

echo endecodeUserId(12);

Output result:

23471DC2352712F34D6780

Test, decrypt the invitation code

 echo endecodeUserId('23471DC2352712F34D6780','decode');

Output result :

12

The results obtained do not seem to have any problems, but in the actual test, such a problem was found. This may exist for ordinary users. A friend sent a message to his WeChat mobile phone. Invitation code, and then he wants to use a computer to register, but he doesn’t know how to transfer the invitation code from the mobile phone to the computer or finds it troublesome. At this time, he has to enter the invitation code manually on the computer. Oh my God, It's 22 digits, and it's still a mix of capital letters and numbers. He probably has to give up on registration.

Therefore, we have made adjustments and changed to a 7-digit invitation code.

Explore again

Is the method encapsulated before writing the article, or is it better to directly add the code first

<?php class convert
{
    /**
     * 初始数字,自定义
     */
    const INIT_NUM = 123456789;

    /**
     * @var 进制的基本字符串
     */
    private $baseChar;

    /**
     * @var 进制类型
     */
    private $type;

    /**
     * @var array 各进制字符串列表
     */
    private static $convertList = array(
        &#39;32&#39; => '0123456789ABCDEFGHJKMNPQRSTVWXYZ',//不含ILOU
    );

    public function __construct($type='32')
    {
        $this->type = $type;
        $this->baseChar = self::$convertList[$type];
    }

    /**
     * 公用方法,数字进行进制转换
     * @param $num
     * @return string
     */
    private function _idToString($num){
        $str = '';
        while ($num!=0){
            $tmp = $num % $this->type;
            $str .= $this->baseChar[$tmp];
            $num = intval($num/$this->type);
        }

        return $str;
    }

    /**
     * @desc  im:十机制数转换成三十二进制数
     * @param (string)$char 三十二进制数
     * return 返回:十进制数
     */
    public function idToString($id){//10位内id 返回7位字母数字
        //数组 增加备用数值
        $id += self::INIT_NUM;

        //左补0 补齐10位
        $str = str_pad($id,10,'0',STR_PAD_LEFT);

        //按位 拆分 4 6位(32进制 4 6位划分)
        $num1 = intval($str[0].$str[2].$str[6].$str[9]);
        $num2 = intval($str[1].$str[3].$str[4].$str[5].$str[7].$str[8]);
        $str1 = $str2 = '';

        $str1 = $this->_idToString($num1);
        $str1 = strrev($str1);

        $str2 = $this->_idToString($num2);
        $str2 = strrev($str2);

        //4 补足 3 4位 U L
        return str_pad($str1,3,'U',STR_PAD_RIGHT).str_pad($str2,4,'L',STR_PAD_RIGHT);
    }

    /**
     * @desc  im:三十二进制数转换成十机制数
     * @param (string)$char 三十二进制数
     * return 返回:十进制数
     */
    public function stringToId($str){
        //1 清除 3 4 位补足位
        $str1 = trim(substr($str,0,3),'U');
        $str2 = trim(substr($str,3,4),'L');

        $num1 = $this->_stringToId($str1);
        $num2 = $this->_stringToId($str2);
        //补位拼接
        $str1 = str_pad($num1,4,'0',STR_PAD_LEFT);
        $str2 = str_pad($num2,6,'0',STR_PAD_LEFT);
        $id = ltrim($str1[0].$str2[0].$str1[1].$str2[1].$str2[2].$str2[3].$str1[2].$str2[4].$str2[5].$str1[3],'0');
        //减去 备用数值
        $id -= self::INIT_NUM;
        return $id;
    }

    /**
     * 公用方法字符串转数字
     * @param $str
     * @return float|int|string
     */
    private function _stringToId($str){
        //转换为数组
        $charArr = array_flip(str_split($this->baseChar));
        $num = 0;
        for ($i=0;$itype,strlen($str)-$i-1);
        }

        return $num;
    }
}

Introduction to ideas

This method was adopted under the guidance of a master who has worked for many years. Convert the id into a fixed-length 32-digit string and add your own algorithm. Why is base 32 used here instead of other bases? The 32-digit system can contain enough English characters, and the generated encrypted string will look more standardized. On the other hand, some English characters that are not easily recognized (ILOU is excluded here) are excluded, so the 32-digit system is used instead of 36 base.

Encryption process, method idToString(), considering that when the id is relatively small at the beginning, there will be more 0s when converting to 32 hexadecimal, which seems very irregular, so an initial value INIT_NUM is set. , this can be customized. According to the passed id, after adding the initial value, a value with a length of 10 digits is obtained. The interval bit of this value is split into $num1 with a length of 4 digits and $num2 with a length of 6 digits. The two values ​​are converted separately. It is hexadecimal. After conversion, $num1 will get a string with a length of 3. Use U to make up for the shortage. $num2 will get a string with a length of 4. Use L to make up for the shortage.

Decryption is a reverse operation, just reverse the operation.

Test: Generate

$obj = new convert(32);
$res1 = $obj->idToString(12);

Result:

85U43DM

Decryption:

$obj = new convert(32);
$res1 = $obj->stringToId('85U43DM');

Result:

12

Recommended related articles:

Summary of four security filtering functions in php (with code)

php implements extraction of numbers from strings Summary of methods (code)

Simple analysis of PHP system program execution functions (system, passthru, exec) (with code)

The above is the detailed content of How to use 32-digit php to encrypt and decrypt IDs (code attached). For more information, please follow other related articles on the PHP Chinese website!

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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.