search
HomeBackend DevelopmentPHP Tutorial 求php高手,看上这一段加密算法的原理

求php高手,看下这一段加密算法的原理

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

function sys_auth($str, $action = 'encode', $key = ''){
    $key = md5($key);
    $str = $action == 'encode' ? $str : base64_decode($str);
    
    
    $strlen = strlen($str);
    
    $keylen = strlen($key);
    
    $code = '';
    for($i = 0; $i 


我将每句都echo 下的代码:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

function sys_auth($str, $action = 'encode', $key = ''){
    $key = md5($key);
    echo '<br>$key:'.$key;
    $str = $action == 'encode' ? $str : base64_decode($str);
    
    echo '<br>加密方式的$str:'.$str;
    
    $strlen = strlen($str);
    echo '<br>$str长度'.$strlen;
    
    $keylen = strlen($key);
    echo '<br>$keylen长度'.$keylen;
    
    $code = '';
    for($i = 0; $i 第".$i."次循环的\$k的值是".$k;
        $code .= $str[$i] ^ $key[$k];
        echo "<br>第".$i.'次循环$k值是'.$k;
        echo "<br>第".$i.'次循环$str[$i]值是'.$str[$i];
        echo "<br>第".$i.'次循环$key[$k]值是'.$key[$k];
        echo "<br>第".$i."次循环的\$code的值是".$code."<br><br><br><br><br>";
    }
    $code = $action == "decode" ? $code : base64_encode($code);
    echo "<br>".$code;
    return $code;
}


得到的结果是:
[code=PHP]
$key:c81e728d9d4c2f636f067f89cc14862c
加密方式的$str:123456
$str长度6
$keylen长度32
第0次循环的$k的值是0
第0次循环$k值是0
第0次循环$str[$i]值是1
第0次循环$key[$k]值是c
第0次循环的$code的值是R





第1次循环的$k的值是1
第1次循环$k值是1
第1次循环$str[$i]值是2
第1次循环$key[$k]值是8
第1次循环的$code的值是R 





第2次循环的$k的值是2
第2次循环$k值是2
第2次循环$str[$i]值是3
第2次循环$key[$k]值是1
第2次循环的$code的值是R 





第3次循环的$k的值是3
第3次循环$k值是3
第3次循环$str[$i]值是4
第3次循环$key[$k]值是e
第3次循环的$code的值是R Q





第4次循环的$k的值是4
第4次循环$k值是4
第4次循环$str[$i]值是5
第4次循环$key[$k]值是7
第4次循环的$code的值是R Q





第5次循环的$k的值是5
第5次循环$k值是5
第5次循环$str[$i]值是6
第5次循环$key[$k]值是2
第5次循环的$code的值是R Q





UgoCUQIE
加密后:UgoCUQIE




[/code]
 
现在的问题是:第0次循环$str[0]值是1,$key[0]值是c
我直接按照上面的 $str[0]^$key[0],(即1^0)却报错,怎么回事,另外 $str[0]^$key[0]不懂

------解决方案--------------------
手册,运算符
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 Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

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

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools