PHP正反加密解密类
时间:2007-02-17 来源: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
// 网络技术主管座右铭:三人行其必有我师焉。http://www.ctohome.com
$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爱好者站 http://www.phpfans.net c/vc/c++/java.
$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
// 网络技术主管座右铭:三人行其必有我师焉。http://www.ctohome.com
$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爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +