网络编程 
首页 > 网络编程 > 浏览文章

Js参数RSA加密传输之jsencrypt.js的使用

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

注意几点:

  1、参数传递的+号处理,在传输时会把+变成空格,不处理后端就报错了。

1、前端代码

<!DOCTYPE html>

<html>
<head>
 <meta name="viewport" content="width=device-width" />
 <title>Login</title>
 <script src="/UploadFiles/2021-04-02/jquery-1.10.2.min.js">

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的使用,希望对大家有所帮助!

上一篇:详解为element-ui的Select和Cascader添加弹层底部操作按钮
下一篇:Vue使用JSEncrypt实现rsa加密及挂载方法
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网