温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在php项目中自定义一个加密与解密程序

发布时间:2021-03-05 16:56:59 来源:亿速云 阅读:137 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关怎么在php项目中自定义一个加密与解密程序,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

php有什么用

php是一个嵌套的缩写名称,是英文超级文本预处理语言,它的语法混合了C、Java、Perl以及php自创新的语法,主要用来做网站开发,许多小型网站都用php开发,因为php是开源的,从而使得php经久不衰。

代码如下:

$ralphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !,.:;?~@#$%^&*()_+-=][}{/><"'"; 
$alphabet = $ralphabet . $ralphabet;
 
class Crypto {
 
function encrypt ($password,$strtoencrypt) {
 
global $ralphabet; 
global $alphabet;
 
for( $i=0; $i<strlen($password); $i++ ) 

$cur_pswd_ltr = substr($password,$i,1); 
$pos_alpha_ary[] = substr(strstr($alphabet,$cur_pswd_ltr),0,strlen($ralphabet)); 
}
 
$i=0; 
$n = 0; 
$nn = strlen($password); 
$c = strlen($strtoencrypt);
 
while($i<$c) 

$encrypted_string .= substr($pos_alpha_ary[$n],strpos($ralphabet,substr($strtoencrypt,$i,1)),1);
 
$n++; 
if($n==$nn) $n = 0; 
$i++; 
}
 
return $encrypted_string;
 
}
 
function decrypt ($password,$strtodecrypt) {
 
global $ralphabet; 
global $alphabet;
 
for( $i=0; $i<strlen($password); $i++ ) 

$cur_pswd_ltr = substr($password,$i,1); 
$pos_alpha_ary[] = substr(strstr($alphabet,$cur_pswd_ltr),0,strlen($ralphabet)); 
}
 
$i=0; 
$n = 0; 
$nn = strlen($password); 
$c = strlen($strtodecrypt);
 
while($i<$c) 

$decrypted_string .= substr($ralphabet,strpos($pos_alpha_ary[$n],substr($strtodecrypt,$i,1)),1);
 
$n++; 
if($n==$nn) $n = 0; 
$i++; 
}
 
return $decrypted_string;
 
}
 
function cryption_table ($password) {
 
global $ralphabet; 
global $alphabet;
 
for( $i=0; $i<strlen($password); $i++ ) 

$cur_pswd_ltr = substr($password,$i,1); 
$pos_alpha_ary[] = substr(strstr($alphabet,$cur_pswd_ltr),0,strlen($ralphabet)); 
}
 
print "<table border=1 cellpadding="0" cellspacing="0">n";
 
print "<tr><td></td>"; 
for( $j=0; $j<strlen($ralphabet); $j++ ) 

print "<td align="center"><font size="2" face="arial">" . substr($ralphabet,$j,1) . "</td>n"; 

print "</tr>";
 
 
for( $i=0; $i<count($pos_alpha_ary); $i++ ) 

print "<tr><td align="right"><font size="2"><b>" . ($i+1) . "|</b></font></td>"; 
for( $k=0; $k<strlen($pos_alpha_ary[$i]); $k++ ) 

print "<td align="center"><font size="2" face="arial">" . substr($pos_alpha_ary[$i],$k,1) . "</td>n"; 

print "</tr>"; 
}
 
print "</table>n";
 
}
 
} // end class Crypto
 
// Example written by Macro Zeng 
$ct = new Crypto; 
//$ct->cryption_table($password); 
echo "<form action=$PHP_SELF method=post>"; 
if ($mod == 2) { 
$strtodecrypt = $ct->encrypt ($password,$strtoencrypt); 
echo 'Encrypted String(加密后的字段): '; 
echo "<input type=text name=strtodecrypt size=45 value=$strtodecrypt>"; 
echo "密码锁: <input type=text name=password size=6 value=$password>"; 
echo "<input type=submit value="Decrypt(解密)">"; 

else { 
$strtoencrypt = $ct->decrypt ($password,$strtodecrypt); 
echo 'String to Encrypt(需要加密的字段): '; 
echo "<input type=text name=strtoencrypt size=45 value=$strtoencrypt>"; 
echo "密码锁: <input type=text name=password size=6 value=$password>"; 
echo "<input type=submit value="Encrypt(加密)">"; 
echo "<input type=hidden name=mod value=2>"; 

echo "</form>";

复制代码 代码如下:


highlight_file("crypto.php");

上述就是小编为大家分享的怎么在php项目中自定义一个加密与解密程序了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI