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());//转化为字节流方便传输
            }
        }
    }

    /// <summary>
    ///  RSA 解密(分段)
    /// </summary>
    /// <param name="encryptedInput"></param>
    /// <param name="privateKey"></param>
    /// <returns></returns>
    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());
            }
        }
    }
    /// <summary>    
    /// RSA私钥格式转换,.net->java   (提供转义后的秘钥社保解密) 
    /// </summary>    
    /// <param name="privateKey">.net生成的私钥</param>    
    /// <returns></returns>   
    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.GetE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值