首页 前端知识 js插件jsencrypt实现rsa前端加密php后台解密

js插件jsencrypt实现rsa前端加密php后台解密

2024-06-07 23:06:52 前端知识 前端哥 971 809 我要收藏

前端页面代码

传递到php后台的是公钥加密的数据

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <!-- 引入jsencrypt.js文件 -->
    <script src="https://cdn.bootcss.com/jsencrypt/3.0.0-beta.1/jsencrypt.js"></script>
</head>
<body>
   <script>
    //公钥 PKCS8(JAVA适用)
    var PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCersOB7cDJlBxzevEvd2IfFrwD62fxbaYRgsaPVC0QbfCaeeEq0MEv8YCQDY1JUK8p1DwFiioZG92wR3tKxWHaV205NUYWGL/pdB0SC0DSWW2wWC5zP3HtASavXDxEfNQszXrO+FPjq1Q+EYmujvquR1T5L3WLyWuciIuBHHwrCQIDAQAB';
	//私钥
    var PRIVATE_KEY ='MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJ6uw4HtwMmUHHN68S93Yh8WvAPrZ/FtphGCxo9ULRBt8Jp54SrQwS/xgJANjUlQrynUPAWKKhkb3bBHe0rFYdpXbTk1RhYYv+l0HRILQNJZbbBYLnM/ce0BJq9cPER81CzNes74U+OrVD4Ria6O+q5HVPkvdYvJa5yIi4EcfCsJAgMBAAECgYBtYD7obu3ExC98NdDLC/n5Pd1owjlKQ28FgOPGZZoOSglfIQVgUrXmiws3v++UCmZCacTZeKud1QRQfmTwW+8eyMs69IzVisLCc57ZR4m/0r16Llj7Z6PX8UgyvsvfpQNT5GSx4bY2eBZOESB7m2KxWzEpcN7cZMuqXzH1QbFcJQJBAPyrQh2gf3EudrrhtLf0J7I+ODGU/FzDPjyvPfxY/I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ttWKwZh3IKKR4sbOLsVcUAvBxpMi6FO96NC/q4LbYCYALPrAkEAxIJ7x5wAO7fwNTUC6vdqiLTFw0aBfrMfjnDVLINnP+5xPH1MoRcSKeShucCpg9uYNCnd+EDXzQYHeQUIkgfqaQJAAyJuucbdsKVwkaQIkrIe5yvJ6WYp/slf9dG+ip8EvAlSwJrPN4KLIpNz5JM3gYeLU0uj86zaWmXQPT4vnDnmoQJAGWWbxcJ9rToqi1XcmK19UUm7vW8hUVSpIWmnw2cAMx0H+X9gJfmrWUq5NB6YSfhZhXRbwbfvrAm5cYSbBPSM8QJAIcXXb3zX0CDdCQq2ZsIAbuTbWM5urp5y9suMP2UnE2UEhJGZrDDR2U4gr6JlbIt4PjJB7SFOObeNRqs3d+/t/Q==';
	//使用公钥加密
    var encrypt = new JSEncrypt();
    encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
    var encrypted = encrypt.encrypt("张三666");
    console.log('加密后数据:%o', encrypted);

    //使用私钥解密 不需要可以删除,方便测试查看使用
    var decrypt = new JSEncrypt();
    decrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
    var uncrypted = decrypt.decrypt(encrypted);
    console.log('解密后数据:%o', uncrypted); // 张三
    //ajax 请求后台
    $.ajax
        ({
        	url: "/index/Test/rsa",
    		dataType: "json",
    		type: "POST",
    		data: {
    				encrypted: encrypted,
    			  },
    	    success:function(res){
    		       console.log(res);  //解密后的数据
    		},
    	    error:function(){
    	    	alert('failed!');
    	    },
        });
    </script>
</body>
</html>

后台私钥php解密

public function rsa(){
        if($this->request->isPost()){
            $encrypted=$this->request->post('encrypted');
            //私钥 PKCS8(JAVA适用)
            $pri ='MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJ6uw4HtwMmUHHN68S93Yh8WvAPrZ/FtphGCxo9ULRBt8Jp54SrQwS/xgJANjUlQr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oAIeItzCjMR4uDHGF1Yb72j9bTgZ0Nky0TrabPEf9rGurw+nZtP5r3ECQQDFhl2XCbcEtXflkEx8uYBRZqqhft3FwK0YDOOJPttWKwZh3IKKR4sbOLsVcUAvBxpMi6FO96NC/q4LbYCYALPrAkEAxIJ7x5wAO7fwNTUC6vdqiLTFw0aBfrMfjnDVLINnP+5xPH1MoRcSKeShucCpg9uYNCnd+EDXzQYHeQUIkgfqaQJAAyJuucbdsKVwkaQIkrIe5yvJ6WYp/slf9dG+ip8EvAlSwJrPN4KLIpNz5JM3gYeLU0uj86zaWmXQPT4vnDnmoQJAGWWbxcJ9rToqi1XcmK19UUm7vW8hUVSpIWmnw2cAMx0H+X9gJfmrWUq5NB6YSfhZhXRbwbfvrAm5cYSbBPSM8QJAIcXXb3zX0CDdCQq2ZsIAbuTbWM5urp5y9suMP2UnE2UEhJGZrDDR2U4gr6JlbIt4PjJB7SFOObeNRqs3d+/t/Q==';
            
            //解密方式一
            $pem = chunk_split($pri, 64, "\n");
            $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
            $prikeyid = openssl_pkey_get_private($pem);
            $crypttext = base64_decode($encrypted);
            if (openssl_private_decrypt($crypttext, $sourcestr, $prikeyid, OPENSSL_PKCS1_PADDING))
            {
                $this->success('成功','',['后台解密结果'=> $sourcestr]);
            }
            //解密方式二,我用的fastadmin系统,自带了rsa解密类,如果系统没有请使用解密方式一
           /* $rsa=new \fast\Rsa('',$pri);
            $this->success('成功','',['后台解密结果'=> $rsa->privDecrypt($encrypted)]);*/
        }
        return $this->view->fetch();
    }

问题处理:

我这边测试的是PKS1的密钥前端js可以加密解密,但是php后台解密不了,提示 注意:我这边测试的是PKS1的密钥前端js可以加密解密,但是php后台解密不了,

报错:openssl_private_decrypt(): key parameter is not a valid private key

我用的是支付宝的开发助手生成的,所以选择的时候选择PKCS8

 

转载请注明出处或者链接地址:https://www.qianduange.cn//article/11328.html
标签
评论
发布的文章

echarts 鼠标划过显示信息

2024-06-14 23:06:40

echarts@5 动画失效

2024-06-14 23:06:39

vue3 聊天机器人 聊天界面

2024-06-14 23:06:06

Vue - Vue3 集成编辑器功能

2024-06-14 23:06:54

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!