最近在做一个新需求,对网络传输的数据安全性要求很高。

如何保障网络请求数据传输的安全性、一致性和防篡改呢?
我们使用了对称加密与非对称加密的结合的策略。
首先说明一下对称加密和非对称加密的概念。
对称加密:采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。
非对称加密:非对称加密算法需要两个密钥:公开密钥(publickey:简称公钥)和私有密钥(privatekey:简称私钥)。公钥与私钥是一对,如果用公钥对数据进行加密,只有用对应的私钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。
将对称加密和非对称加密的优点加以整合,参考了https加解密的实现思路,我们自己封装实现SSL(Secure Scoket Layer 安全套接层)。
APP端发起请求和服务端返回数据加密:
参数明文
{
 key : miwenKey,
 data : miwenData
}
实际请求
{
 data : “上述json进行base64编码后的字符串”
}
我的示例代码是PHP,其他语言可以参考我的实现思路:
(别问我为啥没用Go实现,甲方要求使然,哈哈哈。)
public function myMessage($data, $status = "success")
{
    $aes = new AesSecurity(); //对称加密
    $rsa = new RsaService(); //非对称加密
    //1,随机生成一个多位由数字字母组成的字符串作为本次请求的AES128密钥 16位
    $aes_key = randomkeys(16);
    //2. 使用上述密钥对本次请求的参数进行AES128加密,得到请求参数密文,得到密文miwenData
    $miwenData = $aes::encrypt(json_encode($data),$aes_key);
    //3. 使用前后端约定的RSA公钥对1中的密钥加密,得到miwenKey
    $miwenKey = $rsa->publicEncrypt($aes_key);
    //4. base64转码
    $data = base64_encode(json_encode([
        'key'=>$miwenKey,
        'data'=>$miwenData,
    ]));
    return Response::json($data,$this->getStatusCode(),$header=[]);
}
public function aesData(BaseFormRequest $request)
{
    //解密数据
    $data = $request->post('data','');
    $data = json_decode(base64_decode($data),true);
    $key = $data['key'];
    $data = $data['data'];
    $aes = new AesSecurity(); //对称加密
    $rsa = new RsaService(); //非对称加密
    //1.使用前后端约定的RSA私钥key解密,得到miwenKey(因为客户端使用公钥加密,所以服务端使用公钥解密)
    $miwenKey = $rsa->privateDecrypt($key);
    //2.使用上述miwenKey对本次请求的data参数进行AES128解密,得到请求参数密文miwenData
    $miwenData = $aes::decrypt($data,$miwenKey);
    //3.将json字符串转成数组
    $data = json_decode($miwenData,true);
    //todo 打开时间戳校验
    $time = $data['time'];
    //超过30秒校验失败不允许继续操作
    if ($time
业务层controller中获得解析后的参数
public function create(LoginRequest $request)
{
    //解密数据
    $data = $request->aesData($request);
    $name = $data['name'];
    $password = $data['password'];
     .
    .
    .
}
生成RSA秘钥参考链接[1]
private_key = $this->getPrivateKey();
//        } else {
            $this->public_key = $this->getPublicKey();
//        }
    }
    /**
     * 配置私钥
     * openssl_pkey_get_private这个函数可用来判断私钥是否是可用的,可用,返回资源
     * @return bool|resource
     */
    private function getPrivateKey()
    {
        $original_private_key = file_get_contents(__DIR__ . '/../' . $this->private_key_path);
        return openssl_pkey_get_private($original_private_key);
    }
    /**
     * 配置公钥
     * openssl_pkey_get_public这个函数可用来判断私钥是否是可用的,可用,返回资源
     * @return resource
     */
    public function getPublicKey()
    {
        $original_public_key = file_get_contents(__DIR__ . '/../' . $this->public_key_path);
        return openssl_pkey_get_public($original_public_key);
    }
    /**
     * 私钥加密
     * @param $data
     * @param bool $serialize 是为了不管你传的是字符串还是数组,都能转成字符串
     * @return string
     * @throws \Exception
     */
    public function privateEncrypt($data, $serialize = true)
    {
        $data = substr($data,0,30);
        openssl_private_encrypt(
            $serialize ? serialize($data) : $data,
            $encrypted, $this->private_key
        );
        if ($encrypted === false) {
            throw new \Exception('Could not encrypt the data.');
        }
        return base64_encode($encrypted);
    }
    /**
     * 私钥解密
     * @param $data
     * @param bool $unserialize
     * @return mixed
     * @throws \Exception
     */
    public function privateDecrypt($data, $unserialize = true)
    {
        openssl_private_decrypt(base64_decode($data),$decrypted, $this->private_key);
        if ($decrypted === false) {
            throw new \Exception('Could not decrypt the data.');
        }
        return $unserialize ? unserialize($decrypted) : $decrypted;
    }
    /**
     * 公钥加密
     * @param $data
     * @param bool $serialize 是为了不管你传的是字符串还是数组,都能转成字符串
     * @return string
     * @throws \Exception
     */
    public function publicEncrypt($data, $serialize = true)
    {
        openssl_public_encrypt(
            $serialize ? serialize($data) : $data,
            $encrypted, $this->public_key
        );
        if ($encrypted === false) {
            throw new \Exception('Could not encrypt the data.');
        }
        return base64_encode($encrypted);
    }
    /**
     * 公钥解密
     * @param $data
     * @param bool $unserialize
     * @return mixed
     * @throws \Exception
     */
    public function publicDecrypt($data, $unserialize = true)
    {
        openssl_public_decrypt(base64_decode($data),$decrypted, $this->public_key);
        if ($decrypted === false) {
            throw new \Exception('Could not decrypt the data.');
        }
        return $unserialize ? unserialize($decrypted) : $decrypted;
    }
}
RSA非对称加密的算法示例[2]
// 第一步:生成私钥,这里我们指定私钥的长度为1024, 长度越长,加解密消耗的时间越长
openssl genrsa -out rsa_private_key.pem 1024
// 第二步:根据私钥生成对应的公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pub
// 第三步:私钥转化成pkcs8格式,【这一步非必须,只是程序解析起来方便】
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_private_key_pkcs8.pem
[1]生成RSA秘钥参考链接: https://www.cnblogs.com/chenhaoyu/p/10695245.html
[2]RSA非对称加密的算法示例: https://github.com/chenyRain/Common-Code/tree/master/RSA加密解密
本文转载自微信公众号「 程序员升级打怪之旅」,作者「王中阳Go」,可以通过以下二维码关注。
转载本文请联系「 程序员升级打怪之旅」公众号。
                文章题目:如何保证网络传输的数据安全性?
                
                网站网址:http://www.csdahua.cn/qtweb/news36/426886.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网