Home Backend Development PHP Tutorial 深入研究 UCenter API 之 PHP加密与解密

深入研究 UCenter API 之 PHP加密与解密

Jun 20, 2016 pm 01:02 PM
api interface

深入研究 UCenter API 之 PHP加密与解密

UCenter API 中的加密解密函数,被称为 php 领域的经典之作,也是康盛公司为 php 做的一大贡献

这个函数,可以通过一个 KEY ,生成动态的密文,并可以再通过这个 KEY 来解

 

  
// $string: 明文 或 密文   
// $operation:DECODE表示解密,其它表示加密   
// $key: 密匙   
// $expiry:密文有效期   
//字符串解密加密   
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {   
     // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙   
    $ckey_length = 4;   // 随机密钥长度 取值 0-32;   
                // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。   
                // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方   
                // 当此值为 0 时,则不产生随机密钥   
    // 密匙   
    $key = md5($key ? $key : UC_KEY);   
    // 密匙a会参与加解密   
    $keya = md5(substr($key, 0, 16));   
    // 密匙b会用来做数据完整性验证   
    $keyb = md5(substr($key, 16, 16));   
    // 密匙c用于变化生成的密文   
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5('1303808563'), -$ckey_length)) : '';   
    // 参与运算的密匙   
    $cryptkey = $keya.md5($keya.$keyc);   
 
    $key_length = strlen($cryptkey);   
 
    // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),解密时会通过这个密匙验证数据完整性   
    // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确   
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;   
    $string_length = strlen($string);   
    $result = '';   
    $box = range(0, 255);   
    $rndkey = array();   
    // 产生密匙簿   
    for($i = 0; $i <= 255; $i++) {   
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);   
     }   
     // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度   
    for($j = $i = 0; $i < 256; $i++) {   
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;   
        $tmp = $box[$i];   
        $box[$i] = $box[$j];   
        $box[$j] = $tmp;   
     }   
    // 核心加解密部分   
    for($a = $j = $i = 0; $i < $string_length; $i++) {   
        $a = ($a + 1) % 256;   
        $j = ($j + $box[$a]) % 256;   
        $tmp = $box[$a];   
        $box[$a] = $box[$j];   
        $box[$j] = $tmp;   
        // 从密匙簿得出密匙进行异或,再转成字符   
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));   
     }   
    if($operation == &#39;DECODE&#39;) {   
        // 验证数据有效性,请看未加密明文的格式   
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {   
            return substr($result, 26);   
         } else {   
            return &#39;&#39;;   
         }   
     } else {   
         // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因   
         // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码   
        return $keyc.str_replace(&#39;=&#39;, &#39;&#39;, base64_encode($result));   
     }   
}  
echo authcode(&#39;123456&#39;, &#39;INCODE&#39;, &#39;scutephp&#39;, 0);  
echo "";  
echo authcode(&#39;3575ijqlNCR+R3s7Aakwy1HlvDzHw5g4oKp82qhE7q5FS88&#39;, &#39;DECODE&#39;, &#39;scutephp&#39;, 0);  

Copy after login

 


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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Email Sending API Interface Guide in PHP Email Sending API Interface Guide in PHP May 21, 2023 pm 12:12 PM

With the popularity of email in our daily lives, email sending has become an essential feature in many applications. As a popular web development language, PHP also provides a corresponding email sending API interface. This article will introduce the email sending API interface in PHP to beginners and developers, including how to configure the mail server, how to use PHP's built-in email functions, and how to use a third-party email sending library. 1. Configure the mail server. Before using PHP to send mail, you need to first configure an SMTP server.

What are the free API interface websites? What are the free API interface websites? Jan 05, 2024 am 11:33 AM

Free api interface website: 1. UomgAPI: a platform that provides stable and fast free API services, with over 100 API interfaces; 2. free-api: provides multiple free API interfaces; 3. JSON API: provides free data API interface; 4. AutoNavi Open Platform: Provides map-related API interfaces; 5. Face recognition Face++: Provides face recognition-related API interfaces; 6. Speed ​​data: Provides over a hundred free API interfaces, suitable for various needs In the case of data sources; 7. Aggregate data, etc.

What is the API interface for? What is the API interface for? Apr 23, 2024 pm 01:51 PM

An API interface is a specification for interaction between software components and is used to implement communication and data exchange between different applications or systems. The API interface acts as a "translator", converting the developer's instructions into computer language so that the applications can work together. Its advantages include convenient data sharing, simplified development, improved performance, enhanced security, improved productivity and interoperability.

What are the main types of api interfaces? What are the main types of api interfaces? Apr 23, 2024 pm 01:57 PM

API interface types are rich and diverse, including RESTful API, SOAP API, GraphQL API, etc. RESTful API communicates through the HTTP protocol, with a simple and efficient design, which is the current mainstream Web API design style. SOAP API is based on XML, focuses on cross-language and platform interoperability, and is mostly used in large enterprises and government agencies. GraphQL API is a new query language and runtime environment that supports flexible data query and response.

Developing API documentation: A step-by-step guide to PHP API interfaces Developing API documentation: A step-by-step guide to PHP API interfaces Jan 22, 2024 am 11:20 AM

With the increasing popularity of web applications, APIs (Application Programming Interfaces) are becoming more and more important and playing an increasingly important role in web development. WebAPI is a technology that allows users to access applications through the Internet. It is a basic tool for combining different applications. PHP is a widely used programming language, especially in the field of web development. Developers can allow other applications to use their application functionality by developing PHP API interfaces. In order to achieve this

How to use PHP to call API interface and realize data interaction? How to use PHP to call API interface and realize data interaction? Sep 05, 2023 am 09:30 AM

How to use PHP to call API interface and realize data interaction? With the development of web applications, many developers need to use API (Application Programming Interface) interfaces to implement data interaction with third-party services. As a commonly used back-end development language, PHP provides powerful functions to call API interfaces for data transmission and processing. This article will introduce how to use PHP to call the API interface, and provide some code examples to help readers better understand

How to build an API interface using Go language and Redis How to build an API interface using Go language and Redis Oct 27, 2023 pm 01:23 PM

Overview of how to use Go language and Redis to build API interfaces: Go language (Golang) is a concise, efficient, and powerful programming language, while Redis is an open source in-memory database that provides rich data structures and powerful query functions. This article will introduce how to use Go language and Redis to build API interfaces, and provide specific code examples. Step 1: Install and configure the Go language environment. First, you need to install the Go language on your computer and set the relevant environment variables. After completing this step

Use PHP to build a WeChat public account API interface Use PHP to build a WeChat public account API interface May 13, 2023 pm 12:01 PM

In today's Internet era, WeChat official accounts have become an important marketing channel for more and more companies. If you want your WeChat official account to implement more functions, you often need to write corresponding interfaces. This article will use PHP language as an example to introduce how to build a WeChat public account API interface. 1. Preparation Before writing the WeChat public account API interface, the developer needs to have a WeChat public account and apply for developer interface permissions in the WeChat public platform. After the application is successful, you can obtain the relevant developer AppID and AppSe

See all articles