C# 与JAVA 的RSA 加密解密交互,互通。C# 与JAVA 的密钥格式的转换

创建帮助类RSAUtil
---------------------------------------------------加密(社保解密)-----------------------------------------------------------
(BouncyCastle.Crypto.dll)使用到的DLL下载:

下载链接 https://downloads.bouncycastle.org/csharp/bccrypto-csharp-1.8.1-bin.zip 文件
链接: https://pan.baidu.com/s/1eyDePQf5K017R0ft2eUCKg 提取码: nkhn

源码:

using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WinTest
{
public static class RSAUtil
{
public static string PublicKey = “”;
public static string PrivateKey = “”;
public static void deom()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string PrivateKey = RSAUtil.PrivateKey = rsa.ToXmlString(true);
string PublicKey = RSAUtil.PublicKey = rsa.ToXmlString(false);
}
///
/// RSA 加密(分段)
///
///
///
///
public static string RsaEncrypt(string rawInput, string publicKey)
{
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());//转化为字节流方便传输
            }
        }
    }

    /// 
    ///  RSA 解密(分段)
    /// 
    /// 
    /// 
    /// 
    public static string RsaDecrypt(string encryptedInput, string privateKey)
    {
        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());
            }
        }
    }
    ///     
    /// RSA私钥格式转换,.net->java   (提供转义后的秘钥社保解密) 
    ///     
    /// .net生成的私钥    
    ///    
    public static string RSAPrivateKeyDotNet2Java(this string privateKey)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(privateKey);
        BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
        BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
        BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
        BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
        BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
        BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
        BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
        BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
        RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
        PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
        byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
        return Convert.ToBase64String(serializedPrivateBytes);
    }
    ///     
    /// RSA公钥格式转换,.net->java     (提供转义后的秘钥社保加密) 
    ///     
    /// .net生成的公钥    
    ///    
    public static string RSAPublicKeyDotNet2Java(this string publicKey)
    {
        XmlDocument doc = new XmlDocument(); doc.LoadXml(publicKey);
        BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
        BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
        RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
        SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
        byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
        return Convert.ToBase64String(serializedPublicBytes);
    }
    ///     
    /// RSA私钥格式转换,  //java->.net  (社保提供秘钥,.NET先转换再使用解密)  
    ///     
    /// java生成的RSA私钥    
    ///    
    public static string RSAPrivateKeyJava2DotNet(this string privateKey)
    {
        RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
        return string.Format("{0}{1}

{2}

{3}{4}{5}{6}{7}
", 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())); } /// /// RSA公钥格式转换,java->.net (社保提供秘钥,.NET先转换再使用加密) /// /// java生成的公钥 /// public static string RSAPublicKeyJava2DotNet(this string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); return string.Format("{0}{1}", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } /// /// 社保返回的加密数据先处理特殊字符,再解密 /// /// /// /// public static string ReplaceSpecialCharacter(string content, bool bReset) { if (bReset) { content = content.Replace("_", "/").Replace("-", "+"); switch (content.Length % 4) { case 2: content += "=="; break; case 3: content += "="; break; default: break; } string dummyData = content.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } return dummyData; } else { return content.Replace("/", "_").Replace("+", "-"); } } }

}

测试源码:

 //生成.NET 公钥和私钥
        RSAUtil.deom();

        //.NET 公钥
        string PublicKey = @"nwbjN1znmyL2KyOIrRy/PbWZpTi+oekJIoGNc6jHCl0JNZLFHNs70fyf7y44BH8L8MBkSm5sSwCZfLm5nAsDNOmuEV5Uab5DuWYSE4R2Z3NkKexJJ4bnmXEZYXPMzTbXIpyvU2y9YVrz1BjjRPeHsb6daVdrBgjs4+2b/ok9myM=AQAB";
        //.NET 私钥
        string PrivateKey = @"nwbjN1znmyL2KyOIrRy/PbWZpTi+oekJIoGNc6jHCl0JNZLFHNs70fyf7y44BH8L8MBkSm5sSwCZfLm5nAsDNOmuEV5Uab5DuWYSE4R2Z3NkKexJJ4bnmXEZYXPMzTbXIpyvU2y9YVrz1BjjRPeHsb6daVdrBgjs4+2b/ok9myM=AQAB

2PfagXD2zKzUGLkAXpC+04u0xvESpO1PbTUOGA2m8auviEMNz8VempJ/reOlJjEO2q2nrUsbtqKd0m96Cxz0Fw==

u6Kiit1XhIgRD9jQnQh36y28LOmku2Gqn9KownMSVGhWzkkHQPw77A2h1OirQiKe6aOIO/yxdwTI/9Ds4Kwc1Q==GfwtPj1yQXcde8yEX88EG7/qqbzrl7cYQSMOihDwgpcmUbJ+L/kaaHbNNd1CxT0w4z3TDC0np4r4TeCuBDC2hw==hl8I0jOC2klrFpMpilunLUeaa/uCWiKuQzhkXKR1qvbxu1b3F+XKr9hvXX6mLn2GmkDfbj4fhOFrZC/lg1weZQ==P1y+6el2+1LsdwL14hYCILvsTKGokGSKD35N7HakLmHNjXiU05hN1cnXMsGIZGg+pNHmz/yuPmgNLJoNZCQiCg==D27DriO99jg2W4lfQi2AAaUV/Aq9tUjAMjEQYSEH7+GHe0N7DYnZDE/P1Y5OsWEC76I8GV0N9Vlhi9EaSiJndRvUgphTL2YuAjrVr59Il+lwh/LUBN46AX3cmQm3cFf1F1FXKj4S+QCx/qrCH4mmKpynuQsPL/1XiQSWpugI30E=
"; //JAVA 社保使用公钥 string javaPublicKey = RSAUtil.RSAPublicKeyDotNet2Java(RSAUtil.PublicKey/*PublicKey*/); //JAVA 社保使用私钥 string javaPrivateKey = RSAUtil.RSAPrivateKeyDotNet2Java(RSAUtil.PrivateKey/*PrivateKey*/); //加密字符串参数 string str = "1.0"; //(数据参数)分段加密 string strMessPublic = RSAUtil.RsaEncrypt(str, RSAUtil.PublicKey); //C# 使用公钥(社保提供公钥先转换再使用) string WinFormPublicKey = RSAUtil.RSAPublicKeyJava2DotNet(javaPublicKey); //(数据参数)分段解密 string strMessPrivate = RSAUtil.RsaDecrypt(strMessPublic, RSAUtil.PrivateKey);

效果:
输入字符串,加密
C# 与JAVA 的RSA 加密解密交互,互通。C# 与JAVA 的密钥格式的转换_第1张图片
解密后:
C# 与JAVA 的RSA 加密解密交互,互通。C# 与JAVA 的密钥格式的转换_第2张图片

调试结果:
字符串参数:
string str = “1.0”;
C#
PublicKey:
nzGdMOWU7tSc9WdR7JzKdMjKVqyNbNSg6tHld3c6u33p08g7wVYv388l+WHTOQ64bdT4//nJRZbSRj00JMb8Ej3OHqPMjIbt7vlJoaXnvvTrwMvZBl8DnNI12Vaq/mGKuyxh9p6emAoDjPpZcWJ70YodVNOzYzB9/bkIc81NAs0=AQAB
PrivateKey:
nzGdMOWU7tSc9WdR7JzKdMjKVqyNbNSg6tHld3c6u33p08g7wVYv388l+WHTOQ64bdT4//nJRZbSRj00JMb8Ej3OHqPMjIbt7vlJoaXnvvTrwMvZBl8DnNI12Vaq/mGKuyxh9p6emAoDjPpZcWJ70YodVNOzYzB9/bkIc81NAs0=AQAB

1NSHFWrlP49np9g/74gUjTItV4b3w9RvX27lyBrmy1P+lrLVa44RRa+rBr/36pRpfDkYgyKlwmkoAZrfBYi89w==

v3vzU1Swa4zWy9BC9EEf62L12f/3NB2+DtSvYPFdCE55k9pyKuLzA0PSPRfxbYPw6UaDWUDob19WxtyJTdohWw==yATUz3Qe75U+vaemrt8dMXWu10upHwpPgUkf+ugNb5oI8mgv210xLmTiPv27aWMBoBWoy9WMIyHgz2FVqEXSEQ==nPygFzedk+9Zwqsx2VLzOmZdraa/IwknYr1YM6BuZ3r/DgrQAv0hwxF9cf0JQIIlpvragPhIF9cQK+Vu5FbrdQ==R7gICwFORMdmzXAb90n3LCulT5Bqo1UnZ7temRx+vRwvi3PPQcliPEUfSsBnB+D8IrSCXRfK3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WMIyHgz2FVqEXSEQJBAJz8oBc3nZPvWcKrMdlS8zpmXa2mvyMJJ2K9WDOgbmd6/w4K0AL9IcMRfXH9CUCCJab62oD4SBfXECvlbuRW63UCQEe4CAsBTkTHZs1wG/dJ9ywrpU+QaqNVJ2e7Xpkcfr0cL4tzz0HJYjxFH0rAZwfg/CK0gl0Xyt4r/pRAyqpjpys=

加密的数据串:
RDNK8EBpoe58GD5Wfqev0iZu6MBENQQEOaE4vF7kbvyae8+Qh96NrVMcepe8uY1lvFUgc4X0jTSYuQlFPLuFKaBpqoeLRXILgs0uxnWZHmQoR/f+ibROPZnjU/pAiagxax+lO22S0zn8QqsdLy2n/ge5u23LWx8KEMnReNOTll8DneYGrkehGXeCRxXkeLiuG/CmwIX6o5HWUYR9vAZixhiCTQpTs+FZDb+SRfNEDbyhn7hOitQwLTTURfpFUr39W8Y1QwPlgYlcwVOtcifUANyooq7k08zDFLNQQ6zxEM0a2wpNJQpWt6GzX01Cll5+PpxDcz28K0xKbMPwYT2NIJK6Xy2JLikbgODSiURaVxct8vafySL4r46VpDRoEOfW0jNxOvhsdH5hd5FJrWWkcBAZYhnAp46SBTGVqcxvxFAuZAy66qApJ130NU79wwMaa5iqNfR19Lw7KJfEJCevogzE+vGvtb57xEB3ep811wT1a/+YnQ2D6m0vBxi06CAVCUQ08XHY/5aCrUC1hBRTZakqCCuthtDtqj4sylowxz69G5wobYziit+1cp/TLWH+1gWWsjFykYVus5jSbusP5I/LV3vplxqXuxPXc0oX5kqXJSfIUcmvhxT1Y3aqtibE+XC1N63uHzCpZp2/ZJgeNvCUuvFtAEjOjX650J90D/c=

社保解密成功!

-------------------------------------------社保解密成功返回加密数据【医院解密】----------------------------------------

公钥加密(社保返回数据):
Wliq5c9jbYnjQStbXqVstgdOfV1UIJhol89CmHs710AiCxNnolEARu_z7VxLGy1oVMEmfPh2TIUh69N2EthePNlpSeihjfCzHNUqYQigIedj8HKJ7AxCMKEWgSi41_yirFV7UnDSiBTbSmcxfkIczCipgXK5JxJmyYB-ZkD6pi9ROzkEEYPgBvERyoBZYwBaXKdaGrWIZF6NjgLli0V5rpNBN0ZxhygsCfKM4N3lIL9klokFcb8ImJyJ3XlWUjrXSMP5h1EDuQQgFVNJaweeQYPnXP7x2kEZ0DZ3u7C82oRl046PzACL-VkIsxaepmsLxpDEoVv3jFCrGe4ZbpJmv1fsNupELa0A7f5LjP4olwwqXVxxUsbPVECufcrPVWazjGhVpoDOVCT5eeS80YDQwQkpQ0fZeWhSB-MlIDHVxX0aRkjtRaKUtzusbwz_XmDHjmfcphCV6M7i2y65E_zpbixJ9kvjTiRrCKH6lmUAaCzI4GOCJL5yXS07Glu0bRWkhxQOdTtX_erSFYLNmqMrwpm_H-XSU6VOwexCVJoztIDHovZhFM6vmEGNB4sMNSFq_WiRVa0fXiqWYRDD6PijX_eXH-c5G7uIhWN1npXhqc6ad2OEdX134procjBJyLUv5VT5uDNN3EeX-AEvjSjiTwrgo9ZyslrRidTfLF-XNL8

社保返回的加密数据先处理特殊字符(再解密):
C# 与JAVA 的RSA 加密解密交互,互通。C# 与JAVA 的密钥格式的转换_第3张图片
content = content.Replace("_", “/”).Replace("-", “+”);
switch (content.Length % 4)
{
case 2:
content += “==”;
break;
case 3:
content += “=”;
break;
default:
break;
}
string dummyData = content.Trim().Replace("%", “”).Replace(",", “”).Replace(" ", “+”);
if (dummyData.Length % 4 > 0)
{
dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, ‘=’);
}
return dummyData;

注意:不处理特殊字符直接解密报错如下
1、
C# 与JAVA 的RSA 加密解密交互,互通。C# 与JAVA 的密钥格式的转换_第4张图片
2、Base-64 字符数组或字符串的长度无效。

测试例子://测试回传解密

        string PublicKeycs = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfMZ0w5ZTu1Jz1Z1HsnMp0yMpWrI1s1KDq0eV3dzq7fenTyDvBVi/fzyX5YdM5Drht1Pj/+clFltJGPTQkxvwSPc4eo8yMhu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nDSiBTbSmcxfkIczCipgXK5JxJmyYB-ZkD6pi9ROzkEEYPgBvERyoBZYwBaXKdaGrWIZF6NjgLli0V5rpNBN0ZxhygsCfKM4N3lIL9klokFcb8ImJyJ3XlWUjrXSMP5h1EDuQQgFVNJaweeQYPnXP7x2kEZ0DZ3u7C82oRl046PzACL-VkIsxaepmsLxpDEoVv3jFCrGe4ZbpJmv1fsNupELa0A7f5LjP4olwwqXVxxUsbPVECufcrPVWazjGhVpoDOVCT5eeS80YDQwQkpQ0fZeWhSB-MlIDHVxX0aRkjtRaKUtzusbwz_XmDHjmfcphCV6M7i2y65E_zpbixJ9kvjTiRrCKH6lmUAaCzI4GOCJL5yXS07Glu0bRWkhxQOdTtX_erSFYLNmqMrwpm_H-XSU6VOwexCVJoztIDHovZhFM6vmEGNB4sMNSFq_WiRVa0fXiqWYRDD6PijX_eXH-c5G7uIhWN1npXhqc6ad2OEdX134procjBJyLUv5VT5uDNN3EeX-AEvjSjiTwrgo9ZyslrRidTfLF-XNL8”;

         //C# 使用公钥(社保提供公钥先转换再使用)
        string WinFormPublicKeycs = RSAUtil.RSAPublicKeyJava2DotNet(PublicKeycs);
        //C# 使用私钥(社保提供私钥先转换再使用)
        string WinFormPrivateKeycs = RSAUtil.RSAPrivateKeyJava2DotNet(PrivateKeycs);
        //特殊字符处理
         string strContent = RSAUtil.ReplaceSpecialCharacter(strcs, true);
        //特殊字符处理后解密(的到结果值)
        string strMessPrivatecs = RSAUtil.RsaDecrypt(strContent, WinFormPrivateKeycs);

调试结果:
C# 与JAVA 的RSA 加密解密交互,互通。C# 与JAVA 的密钥格式的转换_第5张图片

java 公钥
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfMZ0w5ZTu1Jz1Z1HsnMp0yMpWrI1s1KDq0eV3dzq7fenTyDvBVi/fzyX5YdM5Drht1Pj/+clFltJGPTQkxvwSPc4eo8yMhu3u+Umhpee+9OvAy9kGXwOc0jXZVqr+YYq7LGH2np6YCgOM+ll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

解密后

1.0

参考链接

你可能感兴趣的:(RSA非对称加密和解密)