jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
本文实例讲述了jQuery+C#实现参数RSA加密传输功能。分享给大家供大家参考,具体如下:
注意:
参数传递的+号处理,在传输时会把+变成空格,不处理后端就报错了。
1、前端代码
Login $(function(){ varencrypt=newJSEncrypt(); encrypt.setPublicKey($("#tra").val()); vardata=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); } }); }); }); MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCa4KHNwDX44gGmmIAtRu4gjVYt GWZzcm4t+1wjUD4dn7fMLPvuK7ai4UrfDeEJE1RPwudJw+lJ6crql8wSIg7/DbTl G3ihsCT6dT9H5B9OoeR7K9VWUesaW/iyVL6HXiYOANabW14pvJATDmdq91Tfgp6P SQyvdfiRdV4r07crpQIDAQAB
注意+号的处理
2、后端代码
publicclassIndexController:Controller { publicActionResultLogin() { returnView(); } [HttpPost] publicActionResultLogin(stringpwd) { //密钥格式要生成pkcs#1格式的而不是pkcs#8格式的 stringprivateKey=@"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 { RSACryptoServiceProviderrsaCryptoServiceProvider=CreateRsaProviderFromPrivateKey(privateKey); //把+号,再替换回来 byte[]res=rsaCryptoServiceProvider.Decrypt(Convert.FromBase64String(pwd.Replace("%2B","+")),false); returnContent(Encoding.UTF8.GetString(res)); } catch(Exceptionexception) { } returnContent(""); } privateRSACryptoServiceProviderCreateRsaProviderFromPrivateKey(stringprivateKey) { varprivateKeyBits=System.Convert.FromBase64String(privateKey); varRSA=newRSACryptoServiceProvider(); varRSAparams=newRSAParameters(); using(BinaryReaderbinr=newBinaryReader(newMemoryStream(privateKeyBits))) { bytebt=0; ushorttwobytes=0; twobytes=binr.ReadUInt16(); if(twobytes==0x8130) binr.ReadByte(); elseif(twobytes==0x8230) binr.ReadInt16(); else thrownewException("Unexpectedvaluereadbinr.ReadUInt16()"); twobytes=binr.ReadUInt16(); if(twobytes!=0x0102) thrownewException("Unexpectedversion"); bt=binr.ReadByte(); if(bt!=0x00) thrownewException("Unexpectedvaluereadbinr.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); returnRSA; } privateintGetIntegerSize(BinaryReaderbinr) { bytebt=0; bytelowbyte=0x00; bytehighbyte=0x00; intcount=0; bt=binr.ReadByte(); if(bt!=0x02) return0; 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); returncount; } }
附: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程序设计有所帮助。