c# RSA 加解密
public static string publicKey = "<RSAKeyValue><Modulus>zUQAZhzjzJcNmEAG1lN+lzScePUh6t5iwHejCIpemILi6yBGyb5S2ZB7EX9v+T6FAx/A/LRNNJbgr8duv8mHttrWD5XSmncv83nXEgj6qbNkqgVtBHC0q10tqizpmQ5m9MsL7XXT+Y080wJ/r6d/SUFPo4Dpe8Mq/pRiDhyZz9M=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; public static string privateKey = "<RSAKeyValue><Modulus>zUQAZhzjzJcNmEAG1lN+lzScePUh6t5iwHejCIpemILi6yBGyb5S2ZB7EX9v+T6FAx/A/LRNNJbgr8duv8mHttrWD5XSmncv83nXEgj6qbNkqgVtBHC0q10tqizpmQ5m9MsL7XXT+Y080wJ/r6d/SUFPo4Dpe8Mq/pRiDhyZz9M=</Modulus><Exponent>AQAB</Exponent><P>9Rl038ZjMl0YJznlvZLP8KR97/CXsvWA4nOJ/b8F1jH9nqsJEJ+bHW94zfXNQjExmilf/C06HybH2wrcBdjVcQ==</P><Q>1mUF6FYTmhd5GmYbKET1l5zxb6yYyWDhlPZO8J9X+Ia1aqXVKHHodWEAEBPWYpyslXwooTN24XTxepdjpHaHgw==</Q><DP>weO1dSB3cGv8o2tFtsXhATje92SYXNWhc4J68uSeNWeufkpHDYN1Lb/1DNkZ2vcXiiCY8eDFTcdb6oeYrndtwQ==</DP><DQ>hV6o8ySA4y3Q/1QfdsiXQu613vOuxC+JhUJITzXA4vDh8GsjFBrwMfzariW5CvVOA1wOZmF+hbha9BkzOj8mAw==</DQ><InverseQ>SNlR4TGrdZivLsOmmcOdebza77swbe5iU4T95y9P3yy1GjCj5ZqynMxyKah0AOVY5WoBhQ5ngrHhMhJ1AFuFLA==</InverseQ><D>EgMwocVRPCfZOo4Ly40CI9SrEnY4ZrrjIY6eEObLjEiYDxgC/jskXBEwq4VqVw3kcDS+I67Sw5Wl2+b9UfkmXn4dKmKu5Axcay8RrgBOVIVwjvnz9d6Q04bOGzOI1kctTHnqp1imm+HA1lwyu7UUypd/POLQozJVjjcGb+BZpiE=</D></RSAKeyValue>"; #region 加解密 /// <summary> /// RSA 加密 /// </summary> /// <param name="rawInput">待加密的内容</param> /// <param name="publicKey">公钥</param> /// <returns></returns> public string RsaEncrypt(string rawInput) { if (string.IsNullOrEmpty(rawInput)) { return string.Empty; } if (string.IsNullOrWhiteSpace(publicKey)) { throw new ArgumentException("Invalid Public Key"); } using (var rsaProvider = new RSACryptoServiceProvider()) { var inputBytes = Encoding.UTF8.GetBytes(rawInput);//有含义的字符串转化为字节流 rsaProvider.FromXmlString(publicKey);//载入公钥 int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度 var buffer = new byte[bufferSize]; using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream()) { while (true) { //分段加密 int readSize = inputStream.Read(buffer, 0, bufferSize); if (readSize <= 0) { break; } var temp = new byte[readSize]; Array.Copy(buffer, 0, temp, 0, readSize); var encryptedBytes = rsaProvider.Encrypt(temp, false); outputStream.Write(encryptedBytes, 0, encryptedBytes.Length); } return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输 } } } /// <summary> /// RSA 解密 /// </summary> /// <param name="encryptedInput">待解密的内容</param> /// <param name="privateKey">私钥</param> /// <returns></returns> public string RsaDecrypt(string encryptedInput) { if (string.IsNullOrEmpty(encryptedInput)) { return string.Empty; } if (string.IsNullOrWhiteSpace(privateKey)) { throw new ArgumentException("Invalid Private Key"); } using (var rsaProvider = new RSACryptoServiceProvider()) { var inputBytes = Convert.FromBase64String(encryptedInput); rsaProvider.FromXmlString(privateKey); int bufferSize = rsaProvider.KeySize / 8; var buffer = new byte[bufferSize]; using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream()) { while (true) { int readSize = inputStream.Read(buffer, 0, bufferSize); if (readSize <= 0) { break; } var temp = new byte[readSize]; Array.Copy(buffer, 0, temp, 0, readSize); var rawBytes = rsaProvider.Decrypt(temp, false); outputStream.Write(rawBytes, 0, rawBytes.Length); } return Encoding.UTF8.GetString(outputStream.ToArray()); } } } #endregion