Js參數(shù)RSA加密傳輸之jsencrypt.js的使用
注意幾點(diǎn):
1、參數(shù)傳遞的+號(hào)處理,在傳輸時(shí)會(huì)把+變成空格,不處理后端就報(bào)錯(cuò)了。
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'), //+號(hào)的處理:因?yàn)閿?shù)據(jù)在網(wǎng)絡(luò)上傳輸時(shí),非字母數(shù)字字符都將被替換成百分號(hào)(%)后跟兩位十六進(jìn)制數(shù),而base64編碼在傳輸?shù)胶蠖说臅r(shí)候,+會(huì)變成空格,因此先替換掉。后端再替換回來(lái)
type: 'post',
success: function (msg) {
alert(msg);
}
});
});
});
</script>
</head>
<body>
<div>
<input type="button" id="btn" value="點(diǎn)我" />
<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);
//把+號(hào),再替換回來(lái)
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;
}
}
總結(jié)
以上所述是小編給大家介紹的Js參數(shù)RSA加密傳輸之jsencrypt.js的使用,希望對(duì)大家有所幫助!
- jQuery+C#實(shí)現(xiàn)參數(shù)RSA加密傳輸功能【附j(luò)sencrypt.js下載】
- vue使用JSEncrypt對(duì)密碼本地存儲(chǔ)時(shí)加解密的實(shí)現(xiàn)
- Java實(shí)現(xiàn)前端jsencrypt.js加密后端解密的示例代碼
- 前端加密cryptojs與JSEncrypt使實(shí)例詳解
- Vue中使用jsencrypt進(jìn)行RSA非對(duì)稱加密的操作方法
- Vue使用JSEncrypt實(shí)現(xiàn)rsa加密及掛載方法
- 在Vue項(xiàng)目中使用jsencrypt.js對(duì)數(shù)據(jù)進(jìn)行加密傳輸?shù)姆椒?/a>
- 前端利用jsencrypt.js進(jìn)行RSA加密示例詳解
相關(guān)文章
JavaScript中將一個(gè)值轉(zhuǎn)換為字符串的方法分析[譯]
在JavaScript中,主要有三種方法能讓任意值轉(zhuǎn)換為字符串.本文講解了每種方法以及各自的優(yōu)缺點(diǎn)2012-09-09
基于javascript實(shí)現(xiàn)最簡(jiǎn)單選項(xiàng)卡切換
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)最簡(jiǎn)單選項(xiàng)卡切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
你所不了解的javascript操作DOM的細(xì)節(jié)知識(shí)點(diǎn)(一)
這篇文章主要介紹了你所不了解的javascript操作DOM的細(xì)節(jié)知識(shí)點(diǎn)的相關(guān)資料,需要的朋友可以參考下2015-06-06
通過(guò)javascript實(shí)現(xiàn)段落的收縮與展開(kāi)
這篇文章主要介紹了通過(guò)javascript實(shí)現(xiàn)段落的收縮與展開(kāi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06
JavaScript數(shù)組方法-系統(tǒng)性總結(jié)詳解
本文是小編給大家特意整理的關(guān)于js數(shù)組方法的知識(shí),非常實(shí)用,在面試筆試題中經(jīng)常用得到,有需要的朋友可以參考下2021-09-09
微信小程序?qū)崿F(xiàn)橫向滾動(dòng)導(dǎo)航欄效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)橫向滾動(dòng)導(dǎo)航欄效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
javascript 使td內(nèi)容不換行不撐開(kāi)
javascript 使td內(nèi)容不換行不撐開(kāi)如何實(shí)現(xiàn),本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-11-11

