前端页面代码
传递到php后台的是公钥加密的数据
Document
后台私钥php解密
public function rsa(){ if($this->request->isPost()){ $encrypted=$this->request->post('encrypted'); //私钥 PKCS8(JAVA适用) $pri ='MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJ6uw4HtwMmUHHN68S93Yh8WvAPrZ/FtphGCxo9ULRBt8Jp54SrQwS/xgJANjUlQrynUPAWKKhkb3bBHe0rFYdpXbTk1RhYYv+l0HRILQNJZbbBYLnM/ce0BJq9cPER81CzNes74U+OrVD4Ria6O+q5HVPkvdYvJa5yIi4EcfCsJAgMBAAECgYBtYD7obu3ExC98NdDLC/n5Pd1owjlKQ28FgOPGZZoOSglfIQVgUrXmiws3v++UCmZCacTZeKud1QRQfmTwW+8eyMs69IzVisLCc57ZR4m/0r16Llj7Z6PX8UgyvsvfpQNT5GSx4bY2eBZOESB7m2KxWzEpcN7cZMuqXzH1QbFcJQJBAPyrQh2gf3EudrrhtLf0J7I+ODGU/FzDPjyvPfxY/I7Zb9sytt8sPjMPUuPf09mmb+0zQOph4RyOLfGxNURMDZ8CQQCgxk7AmfEr++RhfIf31/e0tJcLC4tof4vM+8IaOt6EOFtk/eSpTYBnvscJbMgPAl9V09PbauvsK/0IXOdFvDZXAkEAkKFPZTob+SOJnwvXsHy3xFXNQMRxeH/iPkZpius9Fl/soWJ0P2dJ61xYLExRLgMRiouOGxwQCZJxRioTvZZzLQJAIxNgtxBe13O7HbGnkGDjv97uXRCFt6SsAWqjrElPppUNC3mA25b2/qtWjS3Yb73awsTWIbawBfSK3bo3ErW/PQJBAJ8lIMpBrZdirZ+syB/U2H/1Ay7kXhOFRSRy+XH5hsiD8wTm8IFiwwwPSH6WzrKugmfmLSTG7Z+3JxPg7tTk7Ho='; //PKCS1(非JAVA适用) 解密不了 //$pri ='MIICWwIBAAKBgQCXn41kvOtvur5bwUMWMKv/VMEVldF9GUm0AD8FH9gZETDdbRvluSVocd8hSZu97NJwnsml0GgIR1ps4TtRhvkbPHgYOakfRFwl+icYbA0lbjg+udPWxICWfBkFi7KyTuV6Ts95BO09+ZLtbAKwTMEryD5pIctPvw5kmDmn7iuZYwIDAQABAoGAWYXcLxmYjSyejoBqguST+TrqkjsWKJ3QtZXbn8PwjMRorKRqb1XE74kkbHwQl7AHhHVMrDmD4zd1/Ylyw61E0NZY9qzLsxN3V+ZXzx9i8TdYVr9Dvmz9oAIeItzCjMR4uDHGF1Yb72j9bTgZ0Nky0TrabPEf9rGurw+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解密类,如果系统没有请使用解密方式一 } return $this->view->fetch(); }
问题处理:
我这边测试的是PKS1的密钥前端js可以加密解密,但是php后台解密不了,提示 注意:我这边测试的是PKS1的密钥前端js可以加密解密,但是php后台解密不了,
报错:openssl_private_decrypt(): key parameter is not a valid private key
我用的是支付宝的开发助手生成的,所以选择的时候选择PKCS8
来源地址:https://blog.csdn.net/oraclechaozi/article/details/127023333