php5數(shù)字型字符串加解密代碼
更新時間:2008年04月24日 19:41:53 作者:
對應(yīng)awk版加解密程序的PHP實現(xiàn)代碼
<?php
/* ----------------------------------------------------------------------------
* Script Name: encrypt.php
* Creation Date: 2008-4-7 10:36
* Last Modified: 2008-4-12 16:00
* Author: meyu
* Copyright (c) 2007
* Purpose: 數(shù)字字符串簡易加解密
* ----------------------------------------------------------------------------*/
class Encryption {
/**
* 最終的密文代碼,可設(shè)為任意不重復(fù)的10位英文字符a-zA-Z
*/
private $replacement = 'urskydMeIV';
/**
* 增加的密文第一位,可設(shè)為1位除0以外的整數(shù),即 1-9
*/
private $prefix = "8";
/**
* 公鑰,長度小于8位的正整數(shù)
*/
private $match = "111111";
/**
* 轉(zhuǎn)換后對照數(shù)組
*/
private $replaceenc;
private $replacedec;
function __construct() {
for($i =0; $i < 10; $i++) {
$this->replaceenc['/'.$i.'/'] = $this->replacement{$i};
$this->replacedec['/'.$this->replacement{$i}.'/'] = $i;
}
}
public function encrypt($str) {
return preg_replace(
array_keys($this->replaceenc),
$this->replaceenc,
$this->mynotin(preg_replace("/(.)(.)/", "${2}${1}", $str))
);
}
public function decrypt($str) {
return preg_replace("/(.)(.)/", "${2}${1}",
$this->mynotout(preg_replace(array_keys($this->replacedec),$this->replacedec,$str))
);
}
private function mynotin($str) {
$str_out = "";
$i = 0;
while(isset($str{7*$i})) {
$str_out .= (($this->prefix.substr($str, $i*7, 7))+0)^$this->match;
$i++;
}
return $str_out;
}
private function mynotout($str) {
$str_out = "";
$i = 0;
while(isset($str{8*$i})) {
$str_out .= substr((substr($str, $i*8, 8)+0)^$this->match, 1);
$i++;
}
return $str_out;
}
}
?>
相關(guān)文章
php面向?qū)ο蟪绦蛟O(shè)計中self與static的區(qū)別分析
這篇文章主要介紹了php面向?qū)ο蟪绦蛟O(shè)計中self與static的區(qū)別,結(jié)合實例形式分析了php面向?qū)ο蟪绦蛟O(shè)計中self與static的功能、以及在繼承過程中實現(xiàn)多態(tài)的區(qū)別,并總結(jié)了static靜態(tài)延遲綁定的原理,需要的朋友可以參考下2019-05-05