$data = 'this is a test data for 0xff & $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。 ';
$key = 'thisissecuritykey';
$code = CCEncode::getInstance()->encrypt($data, $key);
echo $code;
echo "<br>";
echo CCEncode::getInstance()->decrypt($code, $key);
1 . [PHP]代码
232@208@210@230@137@220@230@133@196@149@230@206@231@237@139@201@218@232@201@137@217@216@229@147@149@219@219@216@137@154@153@143@198@153@154@136@141@213@137@147@147@166@209@217@353@293@252@351@247@238@349@305@245@333@299@247@354@303@238@131@346@290@239@346@259@245@133@157@213@136@137@344@251@255@147@137@197@149@146@333@300@294@340@232@310@344@288@291@147@154@147@346@255@231@345@303@246@348@295@297@329@305@302@136@154@342@233@245@147
this is a test data for 0xff & $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。
2 . [PHP]代码
class CCEncode{
public static $instance = NULL;
public static function getInstance(){
if(is_null(self::$instance)){
self::$instance = new self;
}
return self::$instance;
}
protected function getByte($data){
$length = strlen($data);
for($i = 0; $i < $length; $i ++){
$tmpList[] = ord($data{$i});
}
return $tmpList;
}
protected function getChar($data, $string = ''){
$length = count($data);
foreach($data as $value){
$string .= chr($value);
}
return $string;
}
public function encrypt($data, $key){
$dataArr = $this->getByte($data);
$keyArr = $this->getByte($key);
$lengthA = count($dataArr);
$lengthB = count($keyArr);
for($i = 0; $i < $lengthA; $i ++){
$tmpList[] = (0xFF & $dataArr[$i]) + (0xFF & $keyArr[$i % $lengthB]);
}
return implode('@', $tmpList);
}
public function decrypt($data, $key){
$dataArr = explode('@', $data);
$keyArr = $this->getByte($key);
$lengthA = count($dataArr);
$lengthB = count($keyArr);
for($i = 0; $i < $lengthA; $i ++){
$tmpList[] = $dataArr[$i] - (0xFF & $keyArr[$i % $lengthB]);
}
return $this->getChar($tmpList);
}
}
3 . javascript版本
<script type="text/javascript">
var ccode = ccode || {};
ccode.getByte = function (data){
var length = data.length;
var result = new Array();
for(var i = 0; i < length; i++){
result[i] = data.charCodeAt(i);
}
return result;
}
ccode.getChar = function (array){
var string = new String();
for (var i in array){
string += String.fromCharCode(array[i]);
}
return string;
}
ccode.encrypt = function (data, key){
dataArr = ccode.getByte(data);
keyArr = ccode.getByte(key);
array = new Array();
lengthData = dataArr.length;
lengthKey = keyArr.length;
for(var i = 0; i < lengthData; i ++){
array[i] = (0xff & dataArr[i]) + (0xff & keyArr[i % lengthKey])
}
return array.join('@');
}
ccode.decrypt = function (encode, key){
codeArr = encode.split('@');
keyArr = ccode.getByte(key);
array = new Array();
lengthCode = codeArr.length;
lengthKey = keyArr.length;
for(var i = 0; i < lengthCode; i ++){
array[i] = codeArr[i] - (0xff & keyArr[i % lengthKey]);
}
return ccode.getChar(array);
}
t = 'this is a test data for 0xff & $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。';
key = 'thisissecuritykey';
f = ccode.encrypt(t, key);
d = ccode.decrypt(f, key);
console.log(f);
console.log(d);
</script>