zoukankan      html  css  js  c++  java
  • 【PHP】使用openssl进行Rsa长数据加密(117)解密(128)

    PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文;解密的时候需要128个字符解密一下,然后拼接成数据。

    加密:

     /**
         * 加密
         * @param $originalData
         * @return string|void
         */
        /*function encrypt($originalData){
    
            // if (openssl_private_encrypt($originalData, $encryptData, $this->rsaPrivateKey)) {
            if (openssl_public_encrypt($originalData, $encryptData, $this->rsaPublicKey)) {
                return base64_encode($encryptData);
            } else {
                return false;
            }
        }*/
        function encrypt($originalData){
    
            $crypto = '';
    
            foreach (str_split($originalData, 117) as $chunk) {
    
                openssl_public_encrypt($chunk, $encryptData, $this->rsaPublicKey);
    
                $crypto .= $encryptData;
            }
    
            return base64_encode($crypto);
        }

    解密:

     /**
         * 私钥解密
         * @param $encryptData
         */
    
        /*function decrypt($encryptData){
    
            // if (openssl_public_decrypt(base64_decode($encryptData), $decryptData, $this->rsaPublicKey)) {
            if (openssl_private_decrypt(base64_decode($encryptData), $decryptData, $this->rsaPrivateKey)) {
    
                return $decryptData;
    
            } else {
    
                return false;
            }
        }*/
        function decrypt($encryptData){
    
            $crypto = '';
    
            foreach (str_split(base64_decode($encryptData), 128) as $chunk) {
    
                openssl_private_decrypt($chunk, $decryptData, $this->rsaPrivateKey);
    
                $crypto .= $decryptData;
            }
    
            return $crypto;
        }

    学习留存!

    原文地址:http://blog.csdn.net/leedaning/article/details/51780511

  • 相关阅读:
    frida枚举当前加载的模块以及模块中方法
    python request请求时候json严格校验怎么去除空格
    firda-so静态注册
    LeetCode 724. 寻找数组的中心索引
    LeetCode 679. 24点游戏
    LeetCode 845. 数组中的最长山脉
    并查集各种情况下的时间复杂度
    LeetCode 547. 省份数量
    LeetCode 5. 最长回文子串
    LeetCode 103. 二叉树的锯齿形层序遍历
  • 原文地址:https://www.cnblogs.com/richerdyoung/p/7866502.html
Copyright © 2011-2022 走看看