来源:www.cncfan.com | 2006-1-11 | (有2138人读过)
---摘自《http://www.ctohome.com》 (文/hansanderson)
<?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>"; /* PHP3 Cryption is a very hackable, insecure encryption function that shouldn't be used for anything of great importance. Though the encryption is good, it's not going to hold up to the rigors of sophisticated cracking procedures.
That said, try it out... it's a great way to encrypt and decrypt strings. Unlike many crypt functions, this goes both ways. Based on a password, you can encrypt or decrypt. You can also decrypt or encrypt numerous time, through looping or other methods. The alphabet of characters is also changeable. All these things allow you to modify and solidify the encryption.
The best part about this? You can decrypt or encrypt with a piece of paper and a pencil. It takes quite a bit longer, but you don't need to be near a computer to use it, and if you ever lose the code, if you remember the technique you can decrypt it.
I wrote these functions in about an hour, after several unsuccessful and frustrating attempts that took much longer and got me nowhere. The successful day came after a sudden realization on the best way to do it.
Note that this won't encrypt/decrypt invisible characters (whitespace), like newline (n) or tab (t)! Sorry, but I tried. If you find a way, please let me know! */ highlight_file("crypto.php"); ?>
|