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step = 117;
$encryptedData = '';
$len = strlen($data);
for ($i = 0; $i < $len; $i += $step) {
$tmpData = substr($data, $i, $step);
$encrypted = '';
openssl_public_encrypt($tmpData, $encrypted, $key,OPENSSL_PKCS1_PADDING);
$encryptedList[] = ($encrypted);
}
$encryptedData = base64_encode(join('', $encryptedList));
return $encryptedData;
}
function decrypt($encryptedData,$private_key)
{
if (empty($encryptedData)) {
return '';
}
$encryptedData = base64_decode($encryptedData);
$decryptedList = array();
$step = 12800;
$len = strlen($encryptedData);
for ($i = 0; $i < $len; $i += $step) {
$data = substr($encryptedData, $i, $step);
$decrypted = '';
@openssl_private_decrypt($data, $decrypted, $private_key, OPENSSL_PKCS1_PADDING);
$decryptedList[] = $decrypted;
}
return join('', $decryptedList);
}
$a = encrypt('你好',$public_key);
//dump($a);
$b = decrypt($a,$private_key);
dump($b);
详情