中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Js參數RSA加密傳輸之jsencrypt.js的使用

發布時間:2020-09-26 19:02:58 來源:腳本之家 閱讀:283 作者:p312011150 欄目:web開發

注意幾點:

  1、參數傳遞的+號處理,在傳輸時會把+變成空格,不處理后端就報錯了。

1、前端代碼

<!DOCTYPE html>

<html>
<head>
 <meta name="viewport" content="width=device-width" />
 <title>Login</title>
 <script src="~/Scripts/jquery-1.10.2.min.js"></script>
 <script src="http://passport.cnblogs.com/scripts/jsencrypt.min.js"></script>
 <script type="text/javascript">
  $(function () {
   var encrypt = new JSEncrypt();
   encrypt.setPublicKey($("#tra").val());
   var data = encrypt.encrypt("123456789");
   alert(data);
   $("#btn").click(function () {
    $.ajax({
     url: '@Url.Action("Login")',
     data: "pwd=" + encodeURI(data).replace(/\+/g, '%2B'), //+號的處理:因為數據在網絡上傳輸時,非字母數字字符都將被替換成百分號(%)后跟兩位十六進制數,而base64編碼在傳輸到后端的時候,+會變成空格,因此先替換掉。后端再替換回來
     type: 'post',
     success: function (msg) {
      alert(msg);
     }
    });
   });

  });


 </script>
</head>
<body>
 <div>
  <input type="button" id="btn" value="點我" />
  <textarea id="tra" rows="15" cols="65">
   MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCa4KHNwDX44gGmmIAtRu4gjVYt
GWZzcm4t+1wjUD4dn7fMLPvuK7ai4UrfDeEJE1RPwudJw+lJ6crql8wSIg7/DbTl
   G3ihsCT6dT9H5B9OoeR7K9VWUesaW/iyVL6HXiYOANabW14pvJATDmdq91Tfgp6P
   SQyvdfiRdV4r07crpQIDAQAB
  </textarea>
  <hr/>
  注意+好的處理
 </div>
</body>
</html>

2、后端代碼

 

public class IndexController : Controller
 {  public ActionResult Login()
  {
   return View();
  }
  [HttpPost]
  public ActionResult Login(string pwd)
  {
   //密鑰格式要生成pkcs#1格式的 而不是pkcs#8格式的
   string privateKey = @"MIICWwIBAAKBgQCa4KHNwDX44gGmmIAtRu4gjVYtGWZzcm4t+1wjUD4dn7fMLPvuK7ai4UrfDeEJE1RPwudJw+lJ6crql8wSIg7/DbTlG3ihsCT6dT9H5B9OoeR7K9VW
UesaW/iyVL6HXiYOANabW14pvJATDmdq91Tfgp6PSQyvdfiRdV4r07crpQIDAQAB
AoGABb+3gdb+qeG0b1CogVsT/7//UOaTzPk/FGneKQQTf4SsN+H7lVhTYTG9ARFC
JyoWg8IXqmn2ljhywHPTWWD2RCZIn2sYT1sVkGb70EgHGQLBraFHElmw+DsVJ+nD
fBCfMrJ1TYXlwigjRkaueaoGgG8LdR8XD+Xs5LersPLjZgECQQCguSB7C4wF6oSw
EDmwNF8ffT5cQc1U2OIq6NBG8rafrjb7LsjhOd03pmY7i4LbW3Vvq4AhQpJEdF1C
vd+Sk/BBAkEA9rBhqnyumV09zFEomSX3zZu+bdhTzM4bJDfEa95swp1gANCVvF/t
DCnlBf51EhCWdeGSpARPUkQnXrYfFUDiZQJAAZEshuaa6+fYeVr/JP+tucHf3Mhr
dxtSQTbZ6QcuzqnFMXfIT6HfzU4bCxOWKAthPsB+VFSw1mgIDMGLL4OvwQJAJlVy
V9PYLezXVZCnBmVoBINXLCqZmxHMFey0kS6XKAbcjEPdgNBHPcSk2jGYb540Q00y
RFqHGPmORKF4Yw0aIQJAd5JRtD3z2MgP/vPoKHJNHqY8bboVcmwqVAm6xCZoTCZz
jNV1Cnsdf4wBV3LCDzYBy+xR4qYNUy5CFXN+8WzzAA==";
   try
   {
    RSACryptoServiceProvider rsaCryptoServiceProvider = CreateRsaProviderFromPrivateKey(privateKey);
    //把+號,再替換回來
    byte[] res = rsaCryptoServiceProvider.Decrypt(Convert.FromBase64String(pwd.Replace("%2B","+")), false);
    return Content(Encoding.UTF8.GetString(res));
   }
   catch (Exception exception)
   {
   }
   return Content("");
  }
  private RSACryptoServiceProvider CreateRsaProviderFromPrivateKey(string privateKey)
  {
   var privateKeyBits = System.Convert.FromBase64String(privateKey);

   var RSA = new RSACryptoServiceProvider();
   var RSAparams = new RSAParameters();

   using (BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits)))
   {
    byte bt = 0;
    ushort twobytes = 0;
    twobytes = binr.ReadUInt16();
    if (twobytes == 0x8130)
     binr.ReadByte();
    else if (twobytes == 0x8230)
     binr.ReadInt16();
    else
     throw new Exception("Unexpected value read binr.ReadUInt16()");

    twobytes = binr.ReadUInt16();
    if (twobytes != 0x0102)
     throw new Exception("Unexpected version");

    bt = binr.ReadByte();
    if (bt != 0x00)
     throw new Exception("Unexpected value read binr.ReadByte()");

    RSAparams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.D = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.P = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.Q = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.DP = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.DQ = binr.ReadBytes(GetIntegerSize(binr));
    RSAparams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
   }

   RSA.ImportParameters(RSAparams);
   return RSA;
  }
  private int GetIntegerSize(BinaryReader binr)
  {
   byte bt = 0;
   byte lowbyte = 0x00;
   byte highbyte = 0x00;
   int count = 0;
   bt = binr.ReadByte();
   if (bt != 0x02)
    return 0;
   bt = binr.ReadByte();

   if (bt == 0x81)
    count = binr.ReadByte();
   else
    if (bt == 0x82)
    {
     highbyte = binr.ReadByte();
     lowbyte = binr.ReadByte();
     byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
     count = BitConverter.ToInt32(modint, 0);
    }
    else
    {
     count = bt;
    }

   while (binr.ReadByte() == 0x00)
   {
    count -= 1;
   }
   binr.BaseStream.Seek(-1, SeekOrigin.Current);
   return count;
  }
 }

總結

以上所述是小編給大家介紹的Js參數RSA加密傳輸之jsencrypt.js的使用,希望對大家有所幫助!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

雷山县| 濉溪县| 神农架林区| 定日县| 城市| 安福县| 周口市| 永丰县| 普定县| 威海市| 沁水县| 西盟| 邮箱| 濮阳县| 剑川县| 宜阳县| 禄劝| 兴化市| 三穗县| 丹东市| 开平市| 昌平区| 文登市| 中山市| 湾仔区| 安远县| 宝应县| 清镇市| 喀什市| 柯坪县| 轮台县| 陇南市| 永福县| 阿尔山市| 义马市| 南雄市| 扶沟县| 开封县| 平阴县| 东至县| 泽普县|