rsa加密解密java和C#互通

前言

因为第三方项目是java的案例,但是原来的项目使用的是java,故需要将java代码转化为C#代码,其中核心代码就是RSA加密以及加签和验签,其他的都是api接口请求难度不大。

遇到的问题

java和c#密钥格式不一致,java使用的是base64格式的密钥,c#使用的是pem格式

公钥

公钥格式 解释
pem “-----BEGIN RSA PUBLIC KEY-----”、中间的数据、“-----END RSA PUBLIC KEY-----” C#常用
ASN.1 一般用base64编码格式编码 java常用

私钥

公钥格式 解释
PKCS#1 c#常用
PKCS#8 java常用

证书类型

证书类型
X.509证书 X.509只包含公钥,没有私钥,这种证书一般公开发布,可用于放在客服端使用,用于加密、验签
PKCS#7证书 因为X.509证书只包含公钥,但有些时候我们需要把私钥和公钥合并成一个证书,放在服务端使用,用于解密、签名。PKCS#12就定义了这样一种证书,它既包含了公钥有包含了私钥。典型的入pfx、p12证书就是PKCS#12证书。
PKCS#12证书 PKCS#7定义了证书链的类型结构

解决过程

C#和java密钥格式相互转换

这是一个想到的办法,java和c#密钥相互转换,这样密钥就统一了,大家相互加密解密不就可以了
c#代码,添加BouncyCastle.NetCore包
CryptoHelper

using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
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.IO;
using System.Security.Cryptography;
using System.Xml;

namespace RSAStu05
{
    public class CryptoHelper
    {
        /// 
        /// RSA密钥转Pem密钥
        /// 
        /// RSA密钥
        /// 是否是私钥
        /// Pem密钥
        public static string RsaKeyToPem(string RSAKey, bool isPrivateKey)
        {
            string pemKey = string.Empty;
            var rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(RSAKey);
            RSAParameters rsaPara = new RSAParameters();
            RsaKeyParameters key = null;
            //RSA私钥
            if (isPrivateKey)
            {
                rsaPara = rsa.ExportParameters(true);
                key = new RsaPrivateCrtKeyParameters(
                    new BigInteger(1, rsaPara.Modulus), new BigInteger(1, rsaPara.Exponent),
                    new BigInteger(1, rsaPara.D),
                    new BigInteger(1, rsaPara.P), new BigInteger(1, rsaPara.Q), new BigInteger(1, rsaPara.DP),
                    new BigInteger(1, rsaPara.DQ),
                    new BigInteger(1, rsaPara.InverseQ));
            }
            //RSA公钥
            else
            {
                rsaPara = rsa.ExportParameters(false);
                key = new RsaKeyParameters(false,
                    new BigInteger(1, rsaPara.Modulus),
                    new BigInteger(1, rsaPara.Exponent));
            }
            using (TextWriter sw = new StringWriter())
            {
                var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
                pemWriter.WriteObject(key);
                pemWriter.Writer.Flush();
                pemKey = sw.ToString();
            }
            return pemKey;
        }

        /// 
        /// Pem密钥转RSA密钥
        /// 
        /// Pem密钥
        /// 是否是私钥
        /// RSA密钥
        public static string PemToRsaKey(string pemKey, bool isPrivateKey = false)
        {
            string rsaKey = string.Empty;
            object pemObject = null;
            RSAParameters rsaPara = new RSAParameters();
            using (StringReader sReader = new StringReader(pemKey))
            {
                var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sReader);
                pemObject = pemReader.ReadObject();
            }
            //RSA私钥
            if (isPrivateKey)
            {
                RsaPrivateCrtKeyParameters key =
                    (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)pemObject).Private;
                rsaPara = new RSAParameters
                {
                    Modulus = key.Modulus.ToByteArrayUnsigned(),
                    Exponent = key.PublicExponent.ToByteArrayUnsigned(),
                    D = key.Exponent.ToByteArrayUnsigned(),
                    P = key.P.ToByteArrayUnsigned(),
                    Q = key.Q.ToByteArrayUnsigned(),
                    DP = key.DP.ToByteArrayUnsigned(),
                    DQ = key.DQ.ToByteArrayUnsigned(),
                    InverseQ = key.QInv.ToByteArrayUnsigned(),
                };
            }
            //RSA公钥
            else
            {
                RsaKeyParameters key = (RsaKeyParameters)pemObject;
                rsaPara = new RSAParameters
                {
                    Modulus = key.Modulus.ToByteArrayUnsigned(),
                    Exponent = key.Exponent.ToByteArrayUnsigned(),
                };
            }
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaPara);
            using (StringWriter sw = new StringWriter())
            {
                sw.Write(rsa.ToXmlString(isPrivateKey ? true : false));
                rsaKey = sw.ToString();
            }
            return rsaKey;
        }

        ///     
        /// RSA私钥格式转换,java->.net    
        ///     
        /// java生成的RSA私钥    
        ///    
        public static string RsaPrivateKeyJava2DotNet(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私钥格式转换,.net->java /// /// .net生成的私钥 /// public static string RsaPrivateKeyDotNet2Java(string privateKey) { XmlDocument doc = new XmlDocument(); doc.LoadXml(privateKey); if (doc.DocumentElement == null) { return null; } 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公钥格式转换,java->.net /// /// java生成的公钥 /// public static string RsaPublicKeyJava2DotNet(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())); } /// /// RSA公钥格式转换,.net->java /// /// .net生成的公钥 /// public static string RsaPublicKeyDotNet2Java(string publicKey) { XmlDocument doc = new XmlDocument(); doc.LoadXml(publicKey); if (doc.DocumentElement == null) { return null; } 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); } } }

RsaHelper

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace RSAStu05
{
    public class RsaHelper
    {

        /// 
        /// 生成密钥
        /// 私钥
        /// 公钥
        /// 密钥长度:512,1024,2048,4096,8192
        /// 
        public static void Generator(out string privateKey, out string publicKey, int keySize = 1024)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);
            privateKey = rsa.ToXmlString(true); //将RSA算法的私钥导出到字符串PrivateKey中 参数为true表示导出私钥 true 表示同时包含 RSA 公钥和私钥;false 表示仅包含公钥。
            publicKey = rsa.ToXmlString(false); //将RSA算法的公钥导出到字符串PublicKey中 参数为false表示不导出私钥 true 表示同时包含 RSA 公钥和私钥;false 表示仅包含公钥。
        }
        /// 
        /// RSA加密 将公钥导入到RSA对象中,准备加密
        /// 
        /// 公钥
        /// 待加密的字符串
        public static string RsaEncrypt(string publicKey, string encryptstring)
        {
            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var inputBytes = Encoding.UTF8.GetBytes(encryptstring);//有含义的字符串转化为字节流
                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 privateKey, string decryptstring)
        {
            using (var rsaProvider = new RSACryptoServiceProvider())
            {
                rsaProvider.FromXmlString(privateKey); //载入私钥  
                var encryptedBytes = Convert.FromBase64String(decryptstring); //将传入的字符串转化为字节流  
                //var outputStream = new MemoryStream(encryptedBytes);
                var bufferSize = rsaProvider.KeySize / 8;
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(encryptedBytes), 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 decryptedBytes = rsaProvider.Decrypt(temp, false);
                        outputStream.Write(decryptedBytes, 0, decryptedBytes.Length);
                    }
                    return Encoding.UTF8.GetString(outputStream.ToArray()); //转化为字符串  
                }
            }
        }

        /// 
        /// RSA私钥加密
        /// 
        /// 私钥
        /// 待加密的字符串
        public static string RsaPrivateEncrypt(string privateKey, string encryptstring)
        {
            var rsaProvider = new RSACryptoServiceProvider();
            rsaProvider.FromXmlString(privateKey);//载入私钥
            var inputBytes = Convert.FromBase64String(encryptstring);//有含义的字符串转化为字节流
            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 = RsaPrivateEncrypt(privateKey, temp);
                    outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                }
                return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输
            }
        }

        ///   
        /// RSA公钥解密
        ///   
        /// 公钥  
        /// 待解密的字符串  
        public static string RsaPublicDecrypt(string publicKey, string decryptstring)
        {
            var rsaProvider = new RSACryptoServiceProvider();
            rsaProvider.FromXmlString(publicKey); //载入私钥  
            var encryptedBytes = Convert.FromBase64String(decryptstring); //将传入的字符串转化为字节流  
            var bufferSize = rsaProvider.KeySize / 8;
            var buffer = new byte[bufferSize];
            using (MemoryStream inputStream = new MemoryStream(encryptedBytes), 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 decryptedBytes = decryptByPublicKey(publicKey, temp);
                    outputStream.Write(decryptedBytes, 0, decryptedBytes.Length);
                }
                return Convert.ToBase64String(outputStream.ToArray());
            }
        }

        /// 
        /// 私钥加密
        /// 这个方法只能加密 私钥长度/8 -11 个字符,分段加密的代码要自己处理了。
        /// 
        /// 密钥
        /// 要加密的数据
        /// 
        public static byte[] RsaPrivateEncrypt(string privateKey, byte[] data)
        {
            string xmlPrivateKey = privateKey;
            //加载私钥  
            RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider();
            privateRsa.FromXmlString(xmlPrivateKey);
            //转换密钥  
            AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa);
            //IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致       
            IBufferedCipher c = CipherUtilities.GetCipher("RSA");
            c.Init(true, keyPair.Private); //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥 
            byte[] DataToEncrypt = data;
            byte[] outBytes = c.DoFinal(DataToEncrypt);//加密  
            return outBytes;
        }

        /// 
        /// 用公钥解密
        /// 这个方法只能加密 私钥长度/8 -11 个字符,分段加密的代码要自己处理了。
        /// 
        /// 
        /// 
        /// 
        public static byte[] decryptByPublicKey(string publicKey, byte[] data)
        {
            string xmlPublicKey = publicKey;

            RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider();
            publicRsa.FromXmlString(xmlPublicKey);

            AsymmetricKeyParameter keyPair = DotNetUtilities.GetRsaPublicKey(publicRsa);
            //转换密钥  
            // AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(publicRsa);
            //IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致       
            IBufferedCipher c = CipherUtilities.GetCipher("RSA");
            c.Init(false, keyPair); //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥 
            byte[] DataToEncrypt = data;
            byte[] outBytes = c.DoFinal(DataToEncrypt);//解密  
            return outBytes;
        }
    }
}

测试

using System;

namespace RSAStu05
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string publicKey;
            string privateKey;
            RsaHelper.Generator(out privateKey, out publicKey);
            //java的密钥
            string javaPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM/ty6yZzpK//t/fRJhNe8U6POQxb50mPfxnKP44Tdlyn9mcntT67CT0gV+M+2wQwZwPTswSCGG6DIBiaII2+WnXlbhbU0S2CM5az+2iRcNC6hBP6vUOmVAkKWF/ZPOE2Jjuf5Qk1WaTqq1fK7PKPt8w1eSCi0HGFWeNZKjGR9JQIDAQAB";
            //java的私钥
            string javePrivateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIz+3LrJnOkr/+399EmE17xTo85DFvnSY9/Gco/jhN2XKf2Zye1PrsJPSBX4z7bBDBnA9OzBIIYboMgGJogjb5adeVuFtTRLYIzlrP7aJFw0LqEE/q9Q6ZUCQpYX9k84TYmO5/lCTVZpOqrV8rs8o+3zDV5IKLQcYVZ41kqMZH0lAgMBAAECgYAlSFEynRQ+PfG/Szs444UuWcmDRY9CQQVCy1VIwgdElu+2DN/tvfe+jrtHgBLgxtw9xR2eqxTAEXcq3SF8Ny6OtCg3RGoj0lbvJiJ6TwAGRurCw4ltPt2A8OOFhax1lwZstaLVNItMawKw7hfD4Zj0bx/uXsoqZgWj5liDPticgwJBAJUpWENPnTit3YOqUPw1ZcxoxU4aKSiWbDGnKhtGZkS62+iNdb3z9BIk2y83mklpnGqPEq9Blgw38kk1IzH1TxMCQQDx/C/zndFyZdiav/AXpQlu5moHPMr+ODPWNNmtZ0bvL3DLKn+CERaNfZ5MsJEXFUTC3IlZtoF4zVZypW+36rHnAkARjEGj+ZPHfTzYJotMgIOvXowHujApZDjqRn4/ozKY11rTqwC1DiQilk9q6KGwDUqnhpluIMskONi6IBQ55mAdAkEAtTKz7WY1mcXtpiMnc20fXS2oI3dAQZBwMGwuu4vkL+KEQX23MPv+uUBhMufcHT7N2GQvbUAePwjzPLHor/1L7QJAISilTN2cCWYj1TRO2s4+pAzdGXlIg8fQF7xa1J0qIoXM+j2tmhda597O/4b0+zHEM7x9KblrjhsxlLp6efzzqQ==";
            //转化为C#密钥
            publicKey = CryptoHelper.RsaPublicKeyJava2DotNet(javaPublicKey);
            //转化为C#私钥
            privateKey = CryptoHelper.RsaPrivateKeyJava2DotNet(javePrivateKey);
            Console.WriteLine("公钥加密私钥解密");
            var data = "待加密的文字内容";
            var jiami = RsaHelper.RsaEncrypt(publicKey, data);
            Console.WriteLine(jiami);
            var jiemi = RsaHelper.RsaDecrypt(privateKey, jiami);
            Console.WriteLine(jiemi);

            Console.WriteLine("私钥加密公钥解密");
            data = Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes(data));
            Console.WriteLine(data);
            jiami = RsaHelper.RsaPrivateEncrypt(privateKey, data);
            Console.WriteLine(jiami);
            jiemi = RsaHelper.RsaPublicDecrypt(publicKey, jiami);
            Console.WriteLine(jiemi);
        }
    }
}

C#本身自己无论用私钥加密公钥解密还是公钥加密私钥解密都是可以的
接入java代码,创建maven项目,引入一个maven包

<dependency>
	<groupId>commons-codecgroupId>
	<artifactId>commons-codecartifactId>
	<version>1.15version>
dependency>

App.java

package com.wujialiang.test03;

import java.io.ByteArrayOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

/**
 * Hello world!
 *
 */
public class App {

	/**
	 * RSA最大加密明文大小
	 */
	private static final int MAX_ENCRYPT_BLOCK = 117;

	/**
	 * RSA最大解密密文大小
	 */
	private static final int MAX_DECRYPT_BLOCK = 128;

	/**
	 * 获取密钥对
	 *
	 * @return 密钥对
	 */
	public static KeyPair getKeyPair() throws Exception {
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		generator.initialize(1024);
		return generator.generateKeyPair();
	}

	/**
	 * 获取私钥
	 *
	 * @param privateKey 私钥字符串
	 * @return
	 */
	public static PrivateKey getPrivateKey(String privateKey) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
		return keyFactory.generatePrivate(keySpec);
	}

	/**
	 * 获取公钥
	 *
	 * @param publicKey 公钥字符串
	 * @return
	 */
	public static PublicKey getPublicKey(String publicKey) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
		return keyFactory.generatePublic(keySpec);
	}

	/**
	 * RSA加密
	 *
	 * @param data      待加密数据
	 * @param publicKey 公钥
	 * @return
	 */
	public static String encrypt(String data, PublicKey publicKey) throws Exception {
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		int inputLen = data.getBytes().length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段加密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
		// 加密后的字符串
		return new String(Base64.encodeBase64String(encryptedData));
	}
	
	/**
	 * RSA加密
	 *
	 * @param data      待加密数据
	 * @param publicKey 公钥
	 * @return
	 */
	public static String encrypt(String data, PrivateKey publicKey) throws Exception {
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		int inputLen = data.getBytes().length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段加密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
		// 加密后的字符串
		return new String(Base64.encodeBase64String(encryptedData));
	}

	/**
	 * RSA解密
	 *
	 * @param data       待解密数据
	 * @param privateKey 私钥
	 * @return
	 */
	public static String decrypt(String data, PrivateKey privateKey) throws Exception {
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] dataBytes = Base64.decodeBase64(data);
		int inputLen = dataBytes.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_DECRYPT_BLOCK) {
				cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		// 解密后的内容
		return new String(decryptedData, "UTF-8");
	}
	
	/**
	 * RSA解密
	 *
	 * @param data       待解密数据
	 * @param privateKey 私钥
	 * @return
	 */
	public static String decrypt(String data, PublicKey privateKey) throws Exception {
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] dataBytes = Base64.decodeBase64(data);
		int inputLen = dataBytes.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_DECRYPT_BLOCK) {
				cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		// 解密后的内容
		return new String(decryptedData, "UTF-8");
	}

	/**
	 * 签名
	 *
	 * @param data       待签名数据
	 * @param privateKey 私钥
	 * @return 签名
	 */
	public static String sign(String data, PrivateKey privateKey) throws Exception {
		byte[] keyBytes = privateKey.getEncoded();
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey key = keyFactory.generatePrivate(keySpec);
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initSign(key);
		signature.update(data.getBytes());
		return new String(Base64.encodeBase64(signature.sign()));
	}

	/**
	 * 验签
	 *
	 * @param srcData   原始字符串
	 * @param publicKey 公钥
	 * @param sign      签名
	 * @return 是否验签通过
	 */
	public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
		byte[] keyBytes = publicKey.getEncoded();
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PublicKey key = keyFactory.generatePublic(keySpec);
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initVerify(key);
		signature.update(srcData.getBytes());
		return signature.verify(Base64.decodeBase64(sign.getBytes()));
	}

	public static void main(String[] args) throws Exception {
		TestSimple();
	}

	public static void TestSimple() {
		try {
			// 生成密钥对
			KeyPair keyPair = getKeyPair();
			String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
			String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
			//java的密钥
			publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM/ty6yZzpK//t/fRJhNe8U6POQxb50mPfxnKP44Tdlyn9mcntT67CT0gV+M+2wQwZwPTswSCGG6DIBiaII2+WnXlbhbU0S2CM5az+2iRcNC6hBP6vUOmVAkKWF/ZPOE2Jjuf5Qk1WaTqq1fK7PKPt8w1eSCi0HGFWeNZKjGR9JQIDAQAB";
            //java的私钥
			privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIz+3LrJnOkr/+399EmE17xTo85DFvnSY9/Gco/jhN2XKf2Zye1PrsJPSBX4z7bBDBnA9OzBIIYboMgGJogjb5adeVuFtTRLYIzlrP7aJFw0LqEE/q9Q6ZUCQpYX9k84TYmO5/lCTVZpOqrV8rs8o+3zDV5IKLQcYVZ41kqMZH0lAgMBAAECgYAlSFEynRQ+PfG/Szs444UuWcmDRY9CQQVCy1VIwgdElu+2DN/tvfe+jrtHgBLgxtw9xR2eqxTAEXcq3SF8Ny6OtCg3RGoj0lbvJiJ6TwAGRurCw4ltPt2A8OOFhax1lwZstaLVNItMawKw7hfD4Zj0bx/uXsoqZgWj5liDPticgwJBAJUpWENPnTit3YOqUPw1ZcxoxU4aKSiWbDGnKhtGZkS62+iNdb3z9BIk2y83mklpnGqPEq9Blgw38kk1IzH1TxMCQQDx/C/zndFyZdiav/AXpQlu5moHPMr+ODPWNNmtZ0bvL3DLKn+CERaNfZ5MsJEXFUTC3IlZtoF4zVZypW+36rHnAkARjEGj+ZPHfTzYJotMgIOvXowHujApZDjqRn4/ozKY11rTqwC1DiQilk9q6KGwDUqnhpluIMskONi6IBQ55mAdAkEAtTKz7WY1mcXtpiMnc20fXS2oI3dAQZBwMGwuu4vkL+KEQX23MPv+uUBhMufcHT7N2GQvbUAePwjzPLHor/1L7QJAISilTN2cCWYj1TRO2s4+pAzdGXlIg8fQF7xa1J0qIoXM+j2tmhda597O/4b0+zHEM7x9KblrjhsxlLp6efzzqQ==";
           
			System.out.println("私钥:" + privateKey);
			System.out.println("公钥:" + publicKey);
			// RSA加密
			String data = "待加密的文字内容";
			data =  new String(Base64.encodeBase64(data.getBytes()), "utf-8");
			System.out.println(data);
			String encryptData = encrypt(data, getPublicKey(publicKey));
			System.out.println("加密后内容:" + encryptData);
			// RSA解密
			String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
			System.out.println("解密后内容:" + decryptData);

			// RSA签名
			String sign = sign(data, getPrivateKey(privateKey));
			// RSA验签
			boolean result = verify(data, getPublicKey(publicKey), sign);
			System.out.print("验签结果:" + result);
			System.out.println("私钥解密,公钥解密");
			encryptData = encrypt(data,getPrivateKey(privateKey));
			System.out.println("加密后内容:" + encryptData);
			// RSA解密
			//encryptData ="o7NYi5WkOB2mqEDW5SOPOIyLj03MngafRxYBFbDQNtNhQd+i8DVvFFFJ9yExVN7ccUtcLkdr9XQRDUfeuVjXVlpGDV7OM5ifs6emlFn/7eFDJh1b7t+P2aLvlRdyLfY1xis6yiEFWMFrQxSwBfnt/GYmK8dZf7u2NVjuzIlqZAs=";
			decryptData = decrypt(encryptData, getPublicKey(publicKey));
			System.out.println("解密后内容:" + decryptData);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.print("加解密异常");
		}
	}
}

java自己也一样无论用私钥加密公钥解密还是公钥加密私钥解密都是可以的,java和C#可以互通的是公钥加密私钥解密,但是私钥加密公钥解密不行,因为这个问题没法彻底解决还是没法使用,又继续百度查找,最终找到了下面的解决方案

使用BouncyCastle

C#需要安装一下BouncyCastle.Cryptography
rsa加密解密java和C#互通_第1张图片
RSAHelper

using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.Text;

namespace RSAStu06
{
    public class RSAHelper
    {
        private static Encoding Encoding_UTF8 = Encoding.UTF8;

        /// 
        /// KEY 结构体
        /// 
        public struct RSAKEY
        {
            /// 
            /// 公钥
            /// 
            public string PublicKey { get; set; }
            /// 
            /// 私钥
            /// 
            public string PrivateKey { get; set; }
        }
        public RSAKEY GetKey()
        {
            //RSA密钥对的构造器
            RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();

            //RSA密钥构造器的参数
            RsaKeyGenerationParameters param = new RsaKeyGenerationParameters(
                Org.BouncyCastle.Math.BigInteger.ValueOf(3),
                new Org.BouncyCastle.Security.SecureRandom(),
                1024,   //密钥长度
                25);
            //用参数初始化密钥构造器
            keyGenerator.Init(param);
            //产生密钥对
            AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair();
            //获取公钥和密钥
            AsymmetricKeyParameter publicKey = keyPair.Public;
            AsymmetricKeyParameter privateKey = keyPair.Private;

            SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
            PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);

            Asn1Object asn1ObjectPublic = subjectPublicKeyInfo.ToAsn1Object();

            byte[] publicInfoByte = asn1ObjectPublic.GetEncoded("UTF-8");
            Asn1Object asn1ObjectPrivate = privateKeyInfo.ToAsn1Object();
            byte[] privateInfoByte = asn1ObjectPrivate.GetEncoded("UTF-8");

            RSAKEY item = new RSAKEY()
            {
                PublicKey = Convert.ToBase64String(publicInfoByte),
                PrivateKey = Convert.ToBase64String(privateInfoByte)
            };
            return item;
        }
        private AsymmetricKeyParameter GetPublicKeyParameter(string keyBase64)
        {
            keyBase64 = keyBase64.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            byte[] publicInfoByte = Convert.FromBase64String(keyBase64);
            Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入
            AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
            return pubKey;
        }

        private AsymmetricKeyParameter GetPrivateKeyParameter(string keyBase64)
        {
            keyBase64 = keyBase64.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            byte[] privateInfoByte = Convert.FromBase64String(keyBase64);
            // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
            // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
            AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
            return priKey;
        }

        /// 
        /// 私钥加密
        /// 
        /// 加密内容
        /// 私钥(Base64后的)
        /// 返回Base64内容
        public string EncryptByPrivateKey(string data, string privateKey)
        {
            //非对称加密算法,加解密用
            IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());

            //加密
            try
            {
                engine.Init(true, GetPrivateKeyParameter(privateKey));
                byte[] byteData = Encoding_UTF8.GetBytes(data);
                var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
                return Convert.ToBase64String(ResultData);
                //Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// 私钥解密
        /// 
        /// 待解密的内容
        /// 私钥(Base64编码后的)
        /// 返回明文
        public string DecryptByPrivateKey(string data, string privateKey)
        {
            data = data.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            //非对称加密算法,加解密用
            IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());

            //解密
            try
            {
                engine.Init(false, GetPrivateKeyParameter(privateKey));
                byte[] byteData = Convert.FromBase64String(data);
                var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
                return Encoding_UTF8.GetString(ResultData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// 公钥加密
        /// 
        /// 加密内容
        /// 公钥(Base64编码后的)
        /// 返回Base64内容
        public string EncryptByPublicKey(string data, string publicKey)
        {
            //非对称加密算法,加解密用
            IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());

            //加密
            try
            {
                engine.Init(true, GetPublicKeyParameter(publicKey));
                byte[] byteData = Encoding_UTF8.GetBytes(data);
                var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
                return Convert.ToBase64String(ResultData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// 公钥解密
        /// 
        /// 待解密的内容
        /// 公钥(Base64编码后的)
        /// 返回明文
        public string DecryptByPublicKey(string data, string publicKey)
        {
            data = data.Replace("\r", "").Replace("\n", "").Replace(" ", "");
            //非对称加密算法,加解密用
            IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());

            //解密
            try
            {
                engine.Init(false, GetPublicKeyParameter(publicKey));
                byte[] byteData = Convert.FromBase64String(data);
                var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
                return Encoding_UTF8.GetString(ResultData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

测试

using System;

namespace RSAStu06
{
    internal class Program
    {
        static void Main(string[] args)
        {
            RSAHelper rSAHelper = new RSAHelper();
            var rsaKey = rSAHelper.GetKey();
            var privateKey = rsaKey.PrivateKey;
            var publicKey = rsaKey.PublicKey;
            Console.WriteLine($"公钥:{privateKey}");
            Console.WriteLine($"私钥:{publicKey}");
            privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIMc+TaeRUn7A9Uwg1J4R5jBZmvMF1ndhmoRoPZF4obHkKjbZNCjPSnc6QlpT3O5PJ07Y7zyzDJAjr6Xnn8EMCyeER4SgBRUkJwW1KnPI9sojAVWYNtolr3ITf5z+3xgMptnSRBQENaSLWszP/HvelWTsjrcNQ+gJY20bLyb5P3dAgMBAAECgYAY8YYn8exUqsCL6nLRWbilQwXtNCKtIgvUWg45TApQgd7vgO2pE6UrNa/P7o0DAxaZAxdydu6KEOYXNFke6PkQ/js5q7LhnQkUddCQQzjo4ghB9XoUMX3TnocyKcQ8VkC6IlwHgspBeilsgF83UhfOal67Po9diYeEVhB7FVJRsQJBALiw2p/VyDE8+qDJ1rwoYqqC/rAVhEehZyoomm+8lv3y50y619z/LRnsQr594bACHvNYmFc+s+zRN5XJeYQOwJcCQQC1vGpmVZgcNUs/PYnRgBvfAfaENZ+omJcqM9TYAqa/2wD/FGIIGWByhykThYovOciw/8BOX8+7cE300REz+o+rAkBlOVDpj1LkYaZ/n4AYqg3BpIAQZAqW88hGG/Dg0rzyvEG3FSSgVB8U+R9vpjCetdrexqzgDFayscxERSNblHZLAkB9e3ov7JvZpkathM0bNYyI/675/Jif7bQ6dI1bFQGT6SCX/7fshbEdgwuuqf8OuqRC6mQa+XbSoimBh7WMIU5/AkAZVQGb9T2tWn4BQQzxFXAuXTJ4NPLQgP6JfSPGBAMyzbQB9BHOoYsbvXf4Qm7cDqzYoEsvbjCndAmo1DyraC5a";
            publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDHPk2nkVJ+wPVMINSeEeYwWZrzBdZ3YZqEaD2ReKGx5Co22TQoz0p3OkJaU9zuTydO2O88swyQI6+l55/BDAsnhEeEoAUVJCcFtSpzyPbKIwFVmDbaJa9yE3+c/t8YDKbZ0kQUBDWki1rMz/x73pVk7I63DUPoCWNtGy8m+T93QIDAQAB";
            var data = "待加密的文字内容";
            var jiami = rSAHelper.EncryptByPrivateKey(data, privateKey);
            Console.WriteLine(jiami);
            var jiemi = rSAHelper.DecryptByPublicKey(jiami, publicKey);
            Console.WriteLine(jiemi);
        }
    }
}

私钥加密公钥解密还是公钥加密私钥解密都是可以的
java代码,创建maven项目,引入maven依赖

<dependency>
	<groupId>commons-codecgroupId>
	<artifactId>commons-codecartifactId>
	<version>1.15version>
dependency>

KeyStore

package com.wujialiang.test04;

public class KeyStore {
    private String publicKey;
    private String privateKey;

    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }
}


RSAUtil

package com.wujialiang.test04;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * RSA加解密工具
*/
public class RSAUtil { public static String RSA_ALGORITHM = "RSA"; public static String UTF8 = "UTF-8"; /** * 创建公钥私钥 */ public static KeyStore createKeys() throws Exception { KeyPairGenerator keyPairGeno = KeyPairGenerator.getInstance(RSA_ALGORITHM); keyPairGeno.initialize(1024); KeyPair keyPair = keyPairGeno.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); KeyStore keyStore = new KeyStore(); keyStore.setPublicKey(Base64.encodeBase64String(publicKey.getEncoded())); keyStore.setPrivateKey(Base64.encodeBase64String(privateKey.getEncoded())); return keyStore; } /** * 获取公钥对象 */ public static RSAPublicKey getPublicKey(byte[] pubKeyData) throws Exception { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubKeyData); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } /** * 获取公钥对象 */ public static RSAPublicKey getPublicKey(String pubKey) throws Exception { return getPublicKey(Base64.decodeBase64(pubKey)); } /** * 获取私钥对象 */ public static RSAPrivateKey getPrivateKey(String priKey) throws Exception { return getPrivateKey(Base64.decodeBase64(priKey)); } /** * 通过私钥byte[]将公钥还原,适用于RSA算法 */ public static RSAPrivateKey getPrivateKey(byte[] keyBytes) throws Exception { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } public static String encryptByPublicKey(String data, String publicKey) throws Exception { return encryptByPublicKey(data, getPublicKey(publicKey)); } /** * 公钥加密 */ public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] bytes = cipher.doFinal(data.getBytes(UTF8)); return Base64.encodeBase64String(bytes); } public static String decryptByPublicKey(String data, String rsaPublicKey) throws Exception { return decryptByPublicKey(data, getPublicKey(rsaPublicKey)); } /** * 公钥解密 */ public static String decryptByPublicKey(String data, RSAPublicKey rsaPublicKey) throws Exception { Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, rsaPublicKey); byte[] inputData = Base64.decodeBase64(data); byte[] bytes = cipher.doFinal(inputData); return new String(bytes, UTF8); } public static String encryptByPrivateKey(String data, String privateKey) throws Exception { return encryptByPrivateKey(data, getPrivateKey(privateKey)); } /** * 私钥加密 */ public static String encryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] bytes = cipher.doFinal(data.getBytes(UTF8)); return Base64.encodeBase64String(bytes); } public static String decryptByPrivateKey(String data, String privateKey) throws Exception { return decryptByPrivateKey(data, getPrivateKey(privateKey)); } /** * 私钥解密 */ public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] inputData = Base64.decodeBase64(data); byte[] bytes = cipher.doFinal(inputData); return new String(bytes, UTF8); } }

测试

package com.wujialiang.test04;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws Exception {
		KeyStore keyStore = RSAUtil.createKeys();
		String privateKey = keyStore.getPrivateKey();
		String publicKey = keyStore.getPublicKey();
		privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIMc+TaeRUn7A9Uwg1J4R5jBZmvMF1ndhmoRoPZF4obHkKjbZNCjPSnc6QlpT3O5PJ07Y7zyzDJAjr6Xnn8EMCyeER4SgBRUkJwW1KnPI9sojAVWYNtolr3ITf5z+3xgMptnSRBQENaSLWszP/HvelWTsjrcNQ+gJY20bLyb5P3dAgMBAAECgYAY8YYn8exUqsCL6nLRWbilQwXtNCKtIgvUWg45TApQgd7vgO2pE6UrNa/P7o0DAxaZAxdydu6KEOYXNFke6PkQ/js5q7LhnQkUddCQQzjo4ghB9XoUMX3TnocyKcQ8VkC6IlwHgspBeilsgF83UhfOal67Po9diYeEVhB7FVJRsQJBALiw2p/VyDE8+qDJ1rwoYqqC/rAVhEehZyoomm+8lv3y50y619z/LRnsQr594bACHvNYmFc+s+zRN5XJeYQOwJcCQQC1vGpmVZgcNUs/PYnRgBvfAfaENZ+omJcqM9TYAqa/2wD/FGIIGWByhykThYovOciw/8BOX8+7cE300REz+o+rAkBlOVDpj1LkYaZ/n4AYqg3BpIAQZAqW88hGG/Dg0rzyvEG3FSSgVB8U+R9vpjCetdrexqzgDFayscxERSNblHZLAkB9e3ov7JvZpkathM0bNYyI/675/Jif7bQ6dI1bFQGT6SCX/7fshbEdgwuuqf8OuqRC6mQa+XbSoimBh7WMIU5/AkAZVQGb9T2tWn4BQQzxFXAuXTJ4NPLQgP6JfSPGBAMyzbQB9BHOoYsbvXf4Qm7cDqzYoEsvbjCndAmo1DyraC5a";
		publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDHPk2nkVJ+wPVMINSeEeYwWZrzBdZ3YZqEaD2ReKGx5Co22TQoz0p3OkJaU9zuTydO2O88swyQI6+l55/BDAsnhEeEoAUVJCcFtSpzyPbKIwFVmDbaJa9yE3+c/t8YDKbZ0kQUBDWki1rMz/x73pVk7I63DUPoCWNtGy8m+T93QIDAQAB";
		System.out.println("私钥:" + privateKey);
		System.out.println("公钥:" + publicKey);
		String data = "待加密的文字内容";
		String jiami = RSAUtil.encryptByPrivateKey(data, privateKey);
		System.out.println("加密:" + jiami);
		String jiemi = RSAUtil.decryptByPublicKey(jiami, publicKey);
		System.out.println("解密:" + jiemi);
	}
}

如何java和C#的密钥是一样的,他们是可以相互调用的,RSA C#和java语言的之间的加密解决也就解决了

参考

https://www.cnblogs.com/zhaoshujie/p/14666795.html
https://www.jianshu.com/p/c93a993f8997

你可能感兴趣的:(java,dotnet,java,c#,python)