相思资源网 Design By www.200059.com

本文实例讲述了jQuery+C#实现参数RSA加密传输功能。分享给大家供大家参考,具体如下:

注意:

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

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;
    }
}

附:jsencrypt.min.js点击此本站下载

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

迅雷、快车、旋风URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》、《jQuery切换特效与技巧总结》、《jQuery遍历算法与技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

标签:
jQuery,C#,参数,RSA,加密,传输

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】”

暂无jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。