前端rsa加密和PHP解密

  • 前端代码
<html>
<head>
  <title>JSEncrypttitle>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="https://cdn.bootcss.com/jsencrypt/3.0.0-beta.1/jsencrypt.js">script>
head>
<body>
<form action="index.php" method="get">
    <input type="text" value="" name="encode" id="input"/><br/>
    <input type="submit" value="提交"><br/>
form>
<br/>
<script type="text/javascript">
  var pubkey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5aqX/9A7NTOb7WNkI3mf6qXHA
f1otJrR7glTlMDZQ/hkxL7oyf7soj77jYh3d5e/+/xeyJaOtOxuntzmzOv27JkMh
aHetqd557qaXnf0KRL3Ht2MAMCHmH6+D5WXHUD6FXvSPkm3EU5AyKhwtCaaFtFYi
VNlPmj2t2ZCfXyCczQIDAQAB
-----END PUBLIC KEY-----`;
  var encrypt = new JSEncrypt();
  encrypt.setPublicKey(pubkey);
  var inp = document.querySelector('#input')
  inp.onblur = function (){
    var encrypted = encrypt.encrypt(inp.value); //每次生成的密文值不一样
    inp.value = encrypted
  }
script>
body>
html>
  • php代码 index.php

 header('Content-Type:text/html;Charset=utf-8;');

$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC5aqX/9A7NTOb7WNkI3mf6qXHAf1otJrR7glTlMDZQ/hkxL7oy
f7soj77jYh3d5e/+/xeyJaOtOxuntzmzOv27JkMhaHetqd557qaXnf0KRL3Ht2MA
MCHmH6+D5WXHUD6FXvSPkm3EU5AyKhwtCaaFtFYiVNlPmj2t2ZCfXyCczQIDAQAB
AoGABJvXBnati4LW1vaNZoAIu51Mm4lO14J3x/4kWBILt5WpUKSsDydHoql4CDTf
Iy3c+kyi7SWsXNj0RgXy5DaaKerjK896gLMio7ypSpESgCS1OkYMHbPvvemV+S/Q
SlnDwZFabmOUClg9byXnSbTSWaYRw4ccACQxKp7TC7i82g0CQQDetKx9I3BeYNaE
fJ2Kp+LjB0GjLWdLLqnIuROc0ff5Zk/o9QLsEA6+o89lykT12EhCqO64NzDqnLLV
tnOnUV6bAkEA1SLa9zkubB/+/J/2PyrYna/E5XlebPk7MCoFI7qZvetUPtaSxvxx
zilMtmwOF0ELPyqDx3PgvPA25Cj5nuO0twJAdaldLGxJ1QMysyKnlXcnzyypSA4X
8cfAmjT2ILdqXRDqDTlNhuzg7WXckHnyPzU60EUygo9eP8X0UicoKXE7GwJAFzAV
lnP4IvdQLHfSuIiWzIEE8NpFdaGf2NuZd4ujkk/5+GQrz85DK2S+3dHfrpP6/0JG
1/o5ZxTTQ/tZPPoU/QJBAJNivTcWQK6tSR5DV/MosvObgwoG9Jc9N8RzvtyLZqW1
HrIDAgsI8iV2Z06zXh+d8VzdG52qSlbGGWWq/SkKZ9A=
-----END RSA PRIVATE KEY-----';


$pi_key =  openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可
openssl_private_decrypt(base64_decode($_GET["encode"]),$str1,$pi_key);
echo '
'
; echo '解密' . $str1; ?>

你可能感兴趣的:(php,html,JavaScript,jq,css)