加密解密


/**
* api 加密接口,轻量级加密算法
*/
function erp_api_encrypt($string) {

$encryptKey = md5('test');
$keyLen = strlen($encryptKey);

$data = substr(md5($string.$encryptKey), 0, 8).$string;
$dataLen = strlen($data);

$rndkey = array();
$box = array();
$cipherText = "";

for ($i = 0; $i < 256; $i++) {
$rndkey[$i] = ord($encryptKey[$i % $keyLen]);
$box[$i] = $i;
}

for ($i = 0, $j = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($i = 0, $j = 0, $k = 0; $i < $dataLen; $i++) {
$k = ($k + 1) % 256;
$j = ($j + $box[$k]) % 256;
$tmp = $box[$k];
$box[$k] = $box[$j];
$box[$j] = $tmp;
$cipherText .= chr(ord($data[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
}

return str_replace('=', '', base64_encode($cipherText));
}

/**
* api 解密接口,轻量级解密算法
*/
function erp_api_decrypt($cipherText) {

$encryptKey = md5('test');
$keyLen = strlen($encryptKey);

$cipherText = base64_decode($cipherText);
$textLen = strlen($cipherText);

$rndkey = array();
$box = array();
$decryptText = "";

for ($i = 0; $i < 256; $i++) {
$rndkey[$i] = ord($encryptKey[$i % $keyLen]);
$box[$i] = $i;
}

for ($i = 0, $j = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($i = 0, $j = 0, $k = 0; $i < $textLen; $i++) {
$k = ($k + 1) % 256;
$j = ($j + $box[$k]) % 256;
$tmp = $box[$k];
$box[$k] = $box[$j];
$box[$j] = $tmp;
$decryptText .= chr(ord($cipherText[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
}

if (substr($decryptText, 0, 8) == substr(md5(substr($decryptText, 8).$encryptKey), 0, 8)) {
return substr($decryptText, 8);
} else {
return false;
}
}


/**
* api 加密接口,轻量级加密算法 key值不固定,copy erp_api_encrypt
*/
function api_encrypt($string,$key='') {

//$encryptKey = md5('test');
$encryptKey = md5($key);
$keyLen = strlen($encryptKey);

$data = substr(md5($string.$encryptKey), 0, 8).$string;
$dataLen = strlen($data);

$rndkey = array();
$box = array();
$cipherText = "";

for ($i = 0; $i < 256; $i++) {
$rndkey[$i] = ord($encryptKey[$i % $keyLen]);
$box[$i] = $i;
}

for ($i = 0, $j = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($i = 0, $j = 0, $k = 0; $i < $dataLen; $i++) {
$k = ($k + 1) % 256;
$j = ($j + $box[$k]) % 256;
$tmp = $box[$k];
$box[$k] = $box[$j];
$box[$j] = $tmp;
$cipherText .= chr(ord($data[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
}

return str_replace('=', '', base64_encode($cipherText));
}

/**
* api 解密接口,轻量级解密算法 key值不固定,copy erp_api_decrypt
*/
function api_decrypt($cipherText,$key='') {

//$encryptKey = md5('test');
$encryptKey = md5($key);
$keyLen = strlen($encryptKey);

$cipherText = base64_decode($cipherText);
$textLen = strlen($cipherText);

$rndkey = array();
$box = array();
$decryptText = "";

for ($i = 0; $i < 256; $i++) {
$rndkey[$i] = ord($encryptKey[$i % $keyLen]);
$box[$i] = $i;
}

for ($i = 0, $j = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($i = 0, $j = 0, $k = 0; $i < $textLen; $i++) {
$k = ($k + 1) % 256;
$j = ($j + $box[$k]) % 256;
$tmp = $box[$k];
$box[$k] = $box[$j];
$box[$j] = $tmp;
$decryptText .= chr(ord($cipherText[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
}

if (substr($decryptText, 0, 8) == substr(md5(substr($decryptText, 8).$encryptKey), 0, 8)) {
return substr($decryptText, 8);
} else {
return false;
}
}
posted @ 2018-11-05 12:31  brady-wang  阅读(335)  评论(0编辑  收藏  举报