php自定义加密与解密程序
php3 cryption用于phhp代码加密时,容易被破解,很不安全。
基于一个密码,可以加密或解密,也可以解密或加密过无数次,通过循环或其他方法。
注意,这不会加密/解密无形字符(空格),如换行符(n)或标签(吨)!
例子:
//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");