php 簡單的對稱加密
<?php
header("content-type:text/html;charset=utf-8");
/**
*@param $string目標字串
*@param $key 加密key
*@return string
*/
function encryption($string="",$key="cxphp"){
//str_split把字串分割到陣列中
$strArr = str_split(base64_encode($string));
$strcount = count($strArr);
foreach(str_split($key) as $key=>$value){
$key < $strcount && $strArr[$key].= $value;
return str_replace(array('=','+','1'), array('O0O0O', 'o000o', 'oo00o'), join($strArr));
}
}
/**
*@param $string 解析字串
*@param $key 揭祕到key
*@return string
*/
function deciphering($string='',$skey='cxphp'){
$strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
$strCount = count($strArr);
foreach (str_split($skey) as $key => $value)
$key <= $strCount && @$strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
return base64_decode(join($strArr));
}
$a = encryption('這是我的一條測試資料');
var_dump($a);
$a = '6cLo000oZ5piv5oiR55qE5LiA5p2h5rWL6Ko000oV5pWw5o2u';
$b = deciphering($a);
var_dump($b);