|
本帖最后由 花开ヽ若相依 于 2023-3-12 07:19 编辑
这两天闲得无聊看到问答有人问。。。顺手写一下
第一个对应的是精易模块的文本_加密 和 文本_解密 第二个对应的是 CryptoJS 里面的 RC4加密和解密
精易模块的解密只是判断了一下密码。。。实际上解密根本用不到密码
另外CryptoJS的RC4和原生的RC4是有区别的 使用了 CryptoJS里面的evpKDF 来生成一个随机的Key和对应的Salt来进行RC4加密 之后把Salt拼接上去
[PHP] 纯文本查看 复制代码 <?php
function bytesToStr($bytes)
{
$str = '';
foreach ($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
function strToBytes($string)
{
$bytes = array();
for ($i = 0; $i < strlen($string); $i++) {
$bytes[] = ord($string[$i]);
}
return $bytes;
}
function estrencode($text,$key){
$temptext = iconv("UTF-8","GB2312",$text);
$BytesArray = strToBytes($temptext);
$TempBytesStr = '';
foreach ($BytesArray as $ch) {
$TempBytesStr.= sprintf("%02X",strToBytes(chr(($ch^3600)))[0]);
}
$TempKey = strtoupper(substr(md5($key),2,9));
return $TempBytesStr.$TempKey;
}
function estrdecode($text,$key){
$UserKey = substr($text,-9);
$TempKey = strtoupper(substr(md5($key),2,9));
if($UserKey!=$TempKey){
exit("密码错误");
}
$Temptext = substr($text,0,strlen($text)-9);
$TempBytes = array();
for ($i=0;$i<strlen($Temptext)/2;$i++)
$TempBytes[]= strToBytes(chr((hexdec($Temptext[$i*2].$Temptext[$i*2+1])^3600)))[0];
return iconv("GB2312","UTF-8",bytesToStr($TempBytes));
}
$text = "精易论坛花开若相依";
$key = "bbs125la";
$EnStr = "AEBBC2C7D2CBDCA3ABB8AFBAD8E4DFF0C2D081BA0240D";
echo estrencode($text,$key);
echo "</br>";
echo estrdecode($EnStr,$key);
?>
[PHP] 纯文本查看 复制代码 <?php
function EVP_BytesToKey($salt, $password) {
$bytes = '';
$last = '';
while(strlen($bytes) < 48) {
$last = hash('md5', $last . $password . $salt, true);
$bytes.= $last;
}
return $bytes;
}
function evpKDF($password, $keySize = 8, $ivSize = 4, $iterations = 1, $hashAlgorithm = "md5") {
$salt=mt_rand(11111111,99999999);
$targetKeySize = $keySize + $ivSize;
$derivedBytes = "";
$numberOfDerivedWords = 0;
$block = NULL;
$hasher = hash_init($hashAlgorithm);
while ($numberOfDerivedWords < $targetKeySize) {
if ($block != NULL) {
hash_update($hasher, $block);
}
hash_update($hasher, $password);
hash_update($hasher, $salt);
$block = hash_final($hasher, TRUE);
$hasher = hash_init($hashAlgorithm);
for ($i = 1; $i < $iterations; $i++) {
hash_update($hasher, $block);
$block = hash_final($hasher, TRUE);
$hasher = hash_init($hashAlgorithm);
}
$derivedBytes .= substr($block, 0, min(strlen($block), ($targetKeySize - $numberOfDerivedWords) * 4));
$numberOfDerivedWords += strlen($block)/4;
}
return array(
"key" => substr($derivedBytes, 0, $keySize * 4),
"salt"=>$salt
);
}
function Js_Rc4_Decode($text, $password) {
$TextBin = base64_decode($text);
$KdfSalt = substr($TextBin, 8, 8);
$TextStr = substr($TextBin, 16);
$KdfIv = EVP_BytesToKey($KdfSalt, $password);
$Rc4Key = substr($KdfIv, 0, 32);
$Rc4DeStr = openssl_decrypt($TextStr, "RC4", $Rc4Key, OPENSSL_RAW_DATA, '');
return $Rc4DeStr;
}
function Js_Rc4_Encode($text, $password) {
$EncodeInfoArray = evpKDF($password);
$EncodeKey = $EncodeInfoArray['key'];
$EncodeSalt = $EncodeInfoArray['salt'];
$Rc4EnStr = base64_encode('Salted__'.$EncodeSalt.openssl_encrypt($text, "RC4", $EncodeKey, OPENSSL_RAW_DATA, ''));
return $Rc4EnStr;
}
$Temp_Str = "精易论坛花开若相依";
$Key = "bbs125la";
$EnStr = "U2FsdGVkX19j7ZGiks/qOK6x0ZK+icVIraTPQG8EPV60uWWIpLVrRfEigw==";
$Str = Js_Rc4_Decode($EnStr,$Key);
echo $Str.'</br>';
$EnStr = Js_Rc4_Encode($Temp_Str,$Key);
echo $EnStr.'</br>';
$Str = Js_Rc4_Decode($EnStr,$Key);
echo $Str.'</br>';
?>
|
|