可以用到 前端加密,后端解密,或者后端加解密
首先要知道这这个rsa是需要一个公钥一个私钥进行加解密的,公钥加密,私钥解密。可以去百度在线生成。
脚本或者页面中先引用jsencrypt.js
document.write("<script src= '../jsencrypt.js'></script>");
var publicKey="公钥";
//这个sign就是加密后的字符串
var sign = encryptRequest("123456", publicKey);
用jsencrypt类库加密js方法
function encryptRequest(str, publicKey) { var encrypt = new JSEncrypt(); encrypt.setPublicKey(publicKey); // ajax请求发送的数据对象 // 将data数组赋给ajax对象 return encrypt.encrypt(str); }
下面是c# 后端的加解密
public class RSAHelper { /// <summary> /// RSA加密 /// </summary> /// <param name="publickey">公钥</param> /// <param name="content"></param> /// <returns></returns> public static string RSAEncrypt(string publickey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(publickey); cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } /// <summary> /// RSA解密 /// </summary> /// <param name="privatekey">私钥</param> /// <param name="content"></param> /// <returns></returns> public static string RSADecrypt(string privatekey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(privatekey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); } /// <summary> /// PEM格式私钥转换成XML格式私钥 /// </summary> /// <param name="privateKey">PEM格式私钥,(纯文字字符,注意不带-----BEGIN PRIVATE KEY-----和-----END PRIVATE KEY-----)</param> /// <returns></returns> public static string RSAPrivateKeyToXml(string privateKey) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>", Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned())); } /// <summary> /// PEM格式公钥转换成XML格式公钥 /// </summary> /// <param name="publicKey">PEM格式公钥,(纯文字字符,注意不带-----BEGIN PUBLIC KEY-----和-----END PUBLIC KEY-----)</param> /// <returns>转换后的XML格式公钥</returns> public static string RSAPublicKeyXML(string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } }
使用实例:
var privateKey ="私钥" var RSAKey = RSAHelper.RSAPrivateKeyToXml(privateKey);//把私钥转换成xml格式 var RSADecrypt = RSAHelper.RSADecrypt(RSAKey, Sign);//解密
标签:Convert,string,c#,RSA,JS,ToBase64String,-----,privateKeyParam,ToByteArrayUnsigne From: https://www.cnblogs.com/wangxlei/p/16910411.html