C# javascript 采用 RSA 加密解密
C# javascript 采用 RSA 加密解密
1.C#提供公钥
2.javascript用公钥加密
3.C#用私钥解密
4.javascript 类库 https://www.pidder.de/pidcrypt/
<script src="pidcrypt.js"></script> <script src="pidcrypt_util.js"></script> <script src="asn1.js"></script> <script src="jsbn.js"></script> <script src="rng.js"></script> <script src="prng4.js"></script> <script src="rsa.js"></script> <script type="text/javascript"> function submit1() { var input = document.getElementById("txtPwd").value; //var n_str = "kaYjgFd9yA68TbFH/E57B3QeO4Ch0+7jFil4HfGqVfACKbUIybBcHuifVKUSYxrdfKQ+cIHPmMq0JpXnOX2Y5AZs7mVUWWlFFu35Rjc4oiKrd0z95opjPp8s6WYIgtr3BDs3ja0W7cCl5xIrDD/5SF4IM9ui9uqy9iHGSG57XRs="; //var e_str = "AQAB"; var n_str = document.getElementById("txtN").value; var e_str = document.getElementById("txtE").value; var n = pidCryptUtil.convertToHex(pidCryptUtil.decodeBase64(n_str)); var e = pidCryptUtil.convertToHex(pidCryptUtil.decodeBase64(e_str)); var rsa = new pidCrypt.RSA(); rsa.setPublic(n, e, 16); crypted = rsa.encrypt(input); var result = pidCryptUtil.encodeBase64(pidCryptUtil.convertFromHex(crypted)); document.getElementById("txtPwd").value = result; } </script>
protected void Page_Load(object sender, EventArgs e) { InitRSA(); } private void InitRSA() { if (Session["RSA"] == null) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); Session["RSA"] = rsa.ToXmlString(true); txtE.Text = Convert.ToBase64String(rsa.ExportParameters(false).Exponent); txtN.Text = Convert.ToBase64String(rsa.ExportParameters(false).Modulus); } } protected void Button1_Click(object sender, EventArgs e) { var pwd = txtPwd.Text; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //rsa.FromXmlString("<RSAKeyValue><Modulus>kaYjgFd9yA68TbFH/E57B3QeO4Ch0+7jFil4HfGqVfACKbUIybBcHuifVKUSYxrdfKQ+cIHPmMq0JpXnOX2Y5AZs7mVUWWlFFu35Rjc4oiKrd0z95opjPp8s6WYIgtr3BDs3ja0W7cCl5xIrDD/5SF4IM9ui9uqy9iHGSG57XRs=</Modulus><Exponent>AQAB</Exponent><P>ywkLeqqcXpAgx1aaadnFDDDTgjNEnqv0PYlNNMgJ30IHGb+YtOediosMKrM3YZbSRoYrVf9W0UPP0+k5c4VqZQ==</P><Q>t6TCzhVl1hCgwfYGgxOHxGQCsgLwgRLCm5f/RXRfBEvag2RmuiKm9yY6cOHEcrJD9fnFm3t0SJPruaFQhjFxfw==</Q><DP>KQIxrFwLa1onFLwcQib3EshF4DVktbu7gDZcPr3sqIhYkM3PFTL28gW1tmYuWGqzHAV7eUoyid/teTrRHNEktQ==</DP><DQ>PJlX1x1kf2D5S0feGj7FnF7MLOWfa1g/c4ySpd6ixA4ryxPEekOZCRFKU5mMc7SzjGWheY8hbhmxvY2nvsBuDQ==</DQ><InverseQ>dc/pyOJ7UxdUO4kj/bHv7n6PLiS7x6JhkmuXvlxvQb1td4jSVl2EaGROzc9W2SAXWFTa3N4G5KzUYF4CKwSp8A==</InverseQ><D>BQJfyjQ4IT9QfqOQRD82wATgXU+JQU7ABK0ccnszTo1YpbKSQM2N/YomoLSksz/jSjwj4Z61Ux31oDYbo8CMF45MboBNIcqvtDgoL+8kNCUEnjnJ9HIkgn7vO1BozqPRu90P8x5zNQbzRumNM7dgcfjxRMvk4cznHVanz18YuxE=</D></RSAKeyValue>"); rsa.FromXmlString((string)Session["RSA"]); var bytes1 = Convert.FromBase64String(pwd); byte[] bytes2 = rsa.Decrypt(bytes1, false); var p = Encoding.Default.GetString(bytes2); bytes1 = Convert.FromBase64String(p); p = Encoding.Default.GetString(bytes1); TextBox1.Text = p; Session["RSA"] = null; InitRSA(); }
-----------------------------------
http://www.cnblogs.com/rock_chen/