简介
RSA算法属于非对称加密算法,非对称加密算法需要两个秘钥:公开密钥(publickey)和私有秘钥(privatekey).公开密钥和私有秘钥是一对,如果公开密钥对数据进行加密,只有用对应的私有秘钥才能解密;如果私有秘钥对数据进行加密那么只有用对应的公开密钥才能解密.因为加密解密使用的是两个不同的秘钥,所以这种算法叫做非对称加密算法.简单的说就是公钥加密私钥解密,私钥加密公钥解密
获取公私钥
通过读取文件获取RSA公开密钥(publickey)和私有秘钥(privatekey)。
if (!function_exists('RsaEncrypt')){
function RsaEncrypt($data,$source = 'client')
{
$path = base_path();
$rsa = new \App\Libraries\RSAUtil(file_get_contents($path.'/'.$source.'public.txt'),file_get_contents($path.'/'.$source.'private.txt'));
return $rsa->publicEncrypt($data);
}
}
if (!function_exists('RsaDecrypt')){
function RsaDecrypt($data,$source = 'client')
{
$path = base_path();
$rsa = new \App\Libraries\RSAUtil(file_get_contents($path.'/'.$source.'public.txt'),file_get_contents($path.'/'.$source.'private.txt'));
return $rsa->privDecrypt($data);
}
}
使用公钥加密
/**
* 公钥加密
* @param string $data
* @return null|string
*/
public function publicEncrypt($data = '')
{
if (!is_string($data)) {
return null;
}
$RSA_ENCRYPT_BLOCK_SIZE = 245;
$result = '';
$split = str_split($data, $RSA_ENCRYPT_BLOCK_SIZE);
foreach ($split as $block) {
openssl_public_encrypt($block, $dataEncrypt, $this->getPublicKey(), OPENSSL_PKCS1_PADDING);
$result .= $dataEncrypt;
// echo "||block||".$block;
// echo "||dataEncrypt||".$dataEncrypt;
}
if ($result) {
return $result;
} else {
return false;
}
}
使用私钥加密
**
* 私钥加密
* @param string $data
* @return null|string
*/
public function privEncrypt($data = '')
{
if (!is_string($data)) {
return null;
}
// return openssl_private_encrypt($data,$encrypted,$this->getPrivateKey()) ? base64_encode($encrypted) : null;
$RSA_ENCRYPT_BLOCK_SIZE = 245;
$result = '';
$split = str_split($data, $RSA_ENCRYPT_BLOCK_SIZE);
foreach ($split as $block) {
openssl_private_encrypt($block, $dataEncrypt, $this->getPrivateKey(), OPENSSL_PKCS1_PADDING);
$result .= $dataEncrypt;
}
if ($result) {
return $result;
} else {
return false;
}
}
使用私钥解密
**
* 私钥解密
* @param string $encrypted
* @return null
*/
public function privDecrypt($encrypted = '')
{
if (!is_string($encrypted)) {
return null;
}
// return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, $this->getPrivateKey())) ? $decrypted : null;
$RSA_ENCRYPT_BLOCK_SIZE = 256;
$result = '';
$split = str_split($encrypted, $RSA_ENCRYPT_BLOCK_SIZE);
foreach ($split as $block) {
openssl_private_decrypt($block, $dataEncrypt, $this->getPrivateKey(), OPENSSL_PKCS1_PADDING);
$result .= $dataEncrypt;
}
if ($result) {
return $result;
} else {
return false;
}
}
公钥解密
/**
* 公钥解密
* @param string $encrypted
* @return null
*/
public function publicDecrypt($encrypted = '')
{
if (!is_string($encrypted)) {
return null;
}
// return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, $this->getPublicKey())) ? $decrypted : null;
$RSA_ENCRYPT_BLOCK_SIZE = 256;
$result = '';
$split = str_split($encrypted, $RSA_ENCRYPT_BLOCK_SIZE);
foreach ($split as $block) {
openssl_public_decrypt($block, $dataEncrypt, $this->getPublicKey(), OPENSSL_PKCS1_PADDING);
$result .= $dataEncrypt;
}
if ($result) {
return $result;
} else {
return false;
}
}