jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
程序员文章站
2023-02-19 17:50:19
本文实例讲述了jquery+c#实现参数rsa加密传输功能。分享给大家供大家参考,具体如下:
注意:
参数传递的+号处理,在传输时会把+变成空格,不处理后端就报错了。...
本文实例讲述了jquery+c#实现参数rsa加密传输功能。分享给大家供大家参考,具体如下:
注意:
参数传递的+号处理,在传输时会把+变成空格,不处理后端就报错了。
1、前端代码
<!doctype html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>login</title> <script src="jquery-1.10.2.min.js"></script> <script src="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; } }
附:jsencrypt.min.js点击此处。
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
迅雷、快车、旋风url加密/解密工具:
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具:
更多关于jquery相关内容感兴趣的读者可查看本站专题:《jquery常用插件及用法总结》、《jquery扩展技巧总结》、《jquery切换特效与技巧总结》、《jquery遍历算法与技巧总结》、《jquery常见经典特效汇总》、《jquery动画与特效用法总结》及《jquery选择器用法总结》
希望本文所述对大家jquery程序设计有所帮助。
上一篇: 一个简单的合并txt的批处理
下一篇: 文本左右对齐排版的批处理