码迷,mamicode.com
首页 > 其他好文 > 详细

openssl Rsa 分段加密解密

时间:2016-09-21 14:22:40      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

 密钥长度 1024

openssl genrsa -out rsa_private_key.pem 1024
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
function readPublicKey($keyPath)
{
    $key = file_get_contents($keyPath);
    $this->rsaPublicKey =  openssl_pkey_get_public($key)
}
function readPrivateKey($keyPath)
{
    $key = file_get_contents($keyPath);
    $this->rsaPrivateKey =  openssl_pkey_get_private($key)
}
// 加密后转为base64编码
function encrypt($originalData)
{
    $crypto = ‘‘;
    foreach (str_split($originalData, 117) as $chunk)
    {
        openssl_public_encrypt($chunk, $encryptData, $this->rsaPublicKey);
        $crypto .= $encryptData;
    }
    return base64_encode($crypto);
}
// base64 post 过来后 ‘+‘ 号变成 空格
function decrypt($encryptData)
{
    $crypto = ‘‘;
    foreach (str_split(str_replace(‘ ‘, ‘+‘, base64_decode($encryptData)), 128) as $chunk)
    {
        openssl_private_decrypt($chunk, $decryptData, $this->rsaPrivateKey);
        $crypto .= $decryptData;
    }
    return $crypto;
}

 

openssl Rsa 分段加密解密

标签:

原文地址:http://www.cnblogs.com/john-h/p/5892326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!