C#版本的sha256withRSA 私钥签名,公钥验签 方法,java<->.net通用
using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using System; using System.Security.Cryptography; using System.Text; namespace ConsoleAppRSA { class Program { static void Main(string[] args) { //原始数据: string data = "app_id=202406181252594586098860032&billIds=[\"SDGDFONC2406200054b\"]&biz_content={\"billIds\":[\"SDGDFONC2406200054b\"],\"bizCode\":\"orderProductionScheduled\"}&bizCode=orderProductionScheduled&charset=UTF-8&format=json&method=ihomeoms.customorder.syncStatus&sign_type=RSA2×tamp=2024-06-20 17:18:12&version=1.0"; //私钥: string privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDWe05OGA+CmD162P5jNWqIivkmDHa3ooV8mcpAAL+uT5RZ+8BHkosvUpNouRqr5RgLZK/02NCJJ9Jc6DBKu9XYWIV09pOCeM7MQ2cj0RoTRD3z/YOP40z4S+GgR1OD8kOujbsRlGE8gzpUhZkpEZ7aa4R0wkUDoDvnP8P8AgB/UCLb2/OfpnrIldl+J0ehUMFuQutLHsiJxQSKnkeQOxORKtEBC7KHxtgk7p/VN6G0746+VBLSWzBOhsDWtAHGrETkhf4b/avMXrXx/IgeqknrNrcgFTz6ZLnhwuuLeFRTddPqv2horEaZllcj8CXytbHPlx8JwwZqtwgK5Zi56sMjAgMBAAECggEBAI3eD8R+C7xRINFiOJm8mlPowLYXJmt+15a8TtgY5rDQuIrwh5WP6/qIPzLUxTyQE2Bt4CkvRHiEW5PSjSbRqbgpjVT5SEbTy822g8FgLUQzLlt2A5CoQ6A06O/QXgB3vab23pbZM5wIHQwNNmrA2fcViQ6F9YpZcWYbenH1jQin7ZSRXNZtSNIiKqSZOOEpmH9Ky8BdjjWnn2Kv45nma9RuJQyM2VSd0SChXRDeniEBHNhTmtfp2E8eZL10hNl7kPGPl9R1HkBV2YG0mIqQa7VBfMZ3eA5BqAXHU0JNffW9KmzYiwu93visQkR5xXd6i0jvbr2TS7PYG6vOVxoY2nECgYEA9nkDAL2sT6+li4YPzlTJ5tTlNJcdCZUFMPtF7i80IMTZnt8Lce3PA5mxRoHAjyrwZUF4kgjYE0KJXAnX+i5fP5HgDGiPhaA8vf9QtD4evLh77mBsdcMeLlPZ60OOvlPP4bNTkGDGiLzTdgT+zMfqctISRwEvpuuzfP1cojT5ODsCgYEA3sW5e0dZ7nBgXqdYhtCB3ByP7CdFCU5HT7v4xx4IzGguxP53xQxsPfqlJBF++t8u1qjhdfWOwgZSfmDKy6LDV46WIfX37vJkudR7PDo1nL5V+eOa35h+mt1oobwxwBMJEelbG7BrPQwQOxBnLqbN/lhiLJzjO3QmE2mmWv/x2jkCgYEAlCwl0QE2IOXAiQN8Zxd0FbBxZs9d5o/PAiZdYRbBqN2O0sLWV5SWulxgRbnlcGxo/waAHJLyi4mZmFpWnBHasxXYcr09YyD0w2hFNUaPJfdmkeUn7xfxAfro7FW978/lDpPHLenwEO4r1iyknaml2YdO857T7e6PBhou4VklZlcCgYAV3fC8Ah/4wL0k7WZKLXNhyMfz1VL4e9+SGxBrcy5H6j4VBil5hSOm3dCR/smpFHlDhuZCruzpbLdyQkwFux9Tm+JuyPciVFbAyvOh0NDsZt6HnejY494hk3Qu1AtKIFQ2luJjkGimYGXDdnO6Z5M20TQpzuibMGaAZ1F2A0OZOQKBgD9bPSEVsM4/dSpQ+fw2mSwpEjo/OYaLAylB726q97SlU3bAKANhjey4gLw1D+PtqSlEcHv4pK4SAicSvh/x0DSLBkxxxFGy2/ekLsAqt7FEzDxs+arUoOVWm9aE6uSnUE2AVuZMyGbdchCtR3UavhMsMufszXHFxk3obgToo0cw"; Console.WriteLine("签名结果:"); string signData = Sign(data, privateKey); //Console.WriteLine("加密结果:"); Console.WriteLine(signData); //公钥: string publceKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1ntOThgPgpg9etj+YzVqiIr5Jgx2t6KFfJnKQAC/rk+UWfvAR5KLL1KTaLkaq+UYC2Sv9NjQiSfSXOgwSrvV2FiFdPaTgnjOzENnI9EaE0Q98/2Dj+NM+EvhoEdTg/JDro27EZRhPIM6VIWZKRGe2muEdMJFA6A75z/D/AIAf1Ai29vzn6Z6yJXZfidHoVDBbkLrSx7IicUEip5HkDsTkSrRAQuyh8bYJO6f1TehtO+OvlQS0lswTobA1rQBxqxE5IX+G/2rzF618fyIHqpJ6za3IBU8+mS54cLri3hUU3XT6r9oaKxGmZZXI/Al8rWxz5cfCcMGarcICuWYuerDIwIDAQAB"; //验签: var verifyResult = VerifyData(data,signData, publceKey); Console.WriteLine("验签结果:"); Console.WriteLine(verifyResult); Console.ReadLine(); } public static string Sign(string contentForSign, string privateKey) { //转换成适用于.Net的秘钥 var netKey = RSAPrivateKeyJava2DotNet(privateKey); var rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(netKey); //签名返回 using (var sha256 = new SHA256CryptoServiceProvider()) { var signData = rsa.SignData(Encoding.UTF8.GetBytes(contentForSign), sha256); return Convert.ToBase64String(signData); } } public static string RSAPrivateKeyJava2DotNet(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> /// 验证签名 /// </summary> /// <param name="data">原始数据</param> /// <param name="signData">用私钥签名原始数据得到的签名结果</param> /// <param name="publicKey">公钥</param> /// <returns></returns> public static bool VerifyData(string data,string signData, string publicKey) { //转换成适用于.Net的秘钥 var netKey = RSAPublicKeyJava2DotNet(publicKey); var rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(netKey); //签名返回 using (var sha256 = new SHA256CryptoServiceProvider()) { return rsa.VerifyData(Encoding.UTF8.GetBytes(data), sha256, Convert.FromBase64String(signData)); } } private static string RSAPublicKeyJava2DotNet(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())); } } }