一个php的加密程序

<?php
$ralphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$alphabet = $ralphabet . $ralphabet;

class Crypto {

 /* 函数作用:解密 */

 public function encrypt ($strtoencrypt) {

  $password = "XXXXXX";
  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);
  $encrypted_string = '';

  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;

 }

 /* 函数作用:加密 */

 public function decrypt ($strtodecrypt) {

  $password = "XXXXXX";
  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);
  $decrypted_string = '';

  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;

 }
}
$us_pw = new Crypto;
?>

posted on 2011-09-26 15:54  belie8  阅读(203)  评论(0编辑  收藏  举报

导航