【签名加解密】c# rsa 解密超过128报错解决
分段解密
public string DecryptJava(string privateKeyJava, string data, string encoding = "UTF-8") { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromPrivateKeyJavaString(privateKeyJava); byte[] encryptData = Convert.FromBase64String(data); byte[] dencryContent = null; #region 分段解密 if (encryptData == null || encryptData.Length <= 0) { throw new NotSupportedException(); } int keySize = rsa.KeySize / 8; byte[] buffer = new byte[keySize]; using (MemoryStream input = new MemoryStream(encryptData)) using (MemoryStream output = new MemoryStream()) { while (true) { int readLine = input.Read(buffer, 0, keySize); if (readLine <= 0) { break; } byte[] temp = new byte[readLine]; Array.Copy(buffer, 0, temp, 0, readLine); byte[] decrypt = rsa.Decrypt(temp, false); output.Write(decrypt, 0, decrypt.Length); } dencryContent = output.ToArray(); } #endregion return Encoding.UTF8.GetString(dencryContent); } 补充: /// <summary> /// RSA加载JAVA PrivateKey /// </summary> /// <param name="privateJavaKey">java提供的第三方私钥</param> /// <returns></returns> public static void FromPrivateKeyJavaString(this RSA rsa, string privateJavaKey) { string xmlPrivateKey = rsa.ConvertToXmlPrivateKey(privateJavaKey); rsa.FromXmlString(xmlPrivateKey); } /// <summary> /// 把java的私钥转换成.net的xml格式 /// </summary> /// <param name="rsa"></param> /// <param name="privateJavaKey"></param> /// <returns></returns> public static string ConvertToXmlPrivateKey(this RSA rsa, string privateJavaKey) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateJavaKey)); string xmlPrivateKey = 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())); return xmlPrivateKey; }