c#rsa加密java解密_C# 通过java生成的RSA公钥加密和解密

本文档详细介绍了如何使用C#进行RSA加密,并使用Java进行解密,反之亦然。提供了一系列静态方法,包括将Java格式的公钥转换为C#可用的格式,以及不同平台之间的加密和解密操作。代码示例涵盖了C#的RSACryptoServiceProvider类的使用,以实现数据的安全传输。
摘要由CSDN通过智能技术生成

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Security.Cryptography;usingSystem.Security.Cryptography.X509Certificates;usingOrg.BouncyCastle;usingOrg.BouncyCastle.Crypto;usingOrg.BouncyCastle.Math;usingOrg.BouncyCastle.Crypto.Parameters;usingOrg.BouncyCastle.Security;usingOrg.BouncyCastle.Crypto.Generators;usingOrg.BouncyCastle.Crypto.Engines;usingOrg.BouncyCastle.Asn1.X509;usingOrg.BouncyCastle.X509;usingOrg.BouncyCastle.Utilities.Collections;usingOrg.BouncyCastle.Asn1.Pkcs;usingOrg.BouncyCastle.Pkcs;usingOrg.BouncyCastle.Asn1;usingOrg.BouncyCastle.Crypto.Encodings;namespaceTAPI.Common

{///

///RSA签名工具类。///

public classRSAUtil

{///

///java公钥转C#所需公钥///

///

///

public static string RSAPublicKeyJava2DotNet(stringpublicKey)

{

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 RSAEncryptMore(string xmlPublicKey, stringm_strEncryptString)

{if (string.IsNullOrEmpty(m_strEncryptString))

{return string.Empty;

}if (string.IsNullOrEmpty(xmlPublicKey))

{throw new ArgumentException("Invalid Public Key");

}using (var rsaProvider = newRSACryptoServiceProvider())

{var inputBytes = Encoding.UTF8.GetBytes(m_strEncryptString);//有含义的字符串转化为字节流

rsaProvider.FromXmlString(xmlPublicKey);//载入公钥

int bufferSize = (rsaProvider.KeySize / 8) - 11;//单块最大长度

var buffer = new byte[bufferSize];using (MemoryStream inputStream = newMemoryStream(inputBytes),

outputStream= newMemoryStream())

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

}

}

}#region 加密

///

///RSA加密///

///

///

///

public static string EncryptJava(string publicKeyJava, string data, string encoding = "UTF-8")

{

RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();//byte[] cipherbytes;

rsa.FromPublicKeyJavaString(publicKeyJava);//☆☆☆☆.NET 4.6以后特有☆☆☆☆//HashAlgorithmName hashName = new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm);//RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA512;//RSAEncryptionPadding.CreateOaep(hashName);//.NET 4.6以后特有//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);//☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆//☆☆☆☆.NET 4.6以前请用此段代码☆☆☆☆//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), false);

using (var rsaProvider = newRSACryptoServiceProvider())

{var inputBytes = Encoding.UTF8.GetBytes(data);//有含义的字符串转化为字节流

int bufferSize = (rsa.KeySize / 8) - 11;//单块最大长度

var buffer = new byte[bufferSize];using (MemoryStream inputStream = newMemoryStream(inputBytes),

outputStream= newMemoryStream())

{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 EncryptCSharp(string publicKeyCSharp, string data, string encoding = "UTF-8")

{

RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();byte[] cipherbytes;

rsa.FromXmlString(publicKeyCSharp);//☆☆☆☆.NET 4.6以后特有☆☆☆☆//HashAlgorithmName hashName = new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm);//RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA512;//RSAEncryptionPadding.CreateOaep(hashName);//.NET 4.6以后特有//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);//☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆//☆☆☆☆.NET 4.6以前请用此段代码☆☆☆☆

cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), false);returnConvert.ToBase64String(cipherbytes);

}///

///RSA加密PEM秘钥///

///

///

///

public static string EncryptPEM(string publicKeyPEM, string data, string encoding = "UTF-8")

{

RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();byte[] cipherbytes;

rsa.LoadPublicKeyPEM(publicKeyPEM);//☆☆☆☆.NET 4.6以后特有☆☆☆☆//HashAlgorithmName hashName = new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm);//RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA512;//RSAEncryptionPadding.CreateOaep(hashName);//.NET 4.6以后特有//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);//☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆//☆☆☆☆.NET 4.6以前请用此段代码☆☆☆☆

cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), false);returnConvert.ToBase64String(cipherbytes);

}#endregion

///

///解密公钥///

///

///

///

///

///解密公钥///

///

///

///

public static string DecryptByPublicKey(string s, stringkey)

{

s= s.Replace("\r", "").Replace("\n", "").Replace(" ", "");//非对称加密算法,加解密用

IAsymmetricBlockCipher engine = new Pkcs1Encoding(newRsaEngine());//解密

try{

engine.Init(false, GetPublicKeyParameter(key));byte[] byteData =Convert.FromBase64String(s);var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);returnSystem.Text.Encoding.UTF8.GetString(ResultData);

}catch(Exception ex)

{returnex.Message;

}

}//#region 私有属性

private RSAParameters RSAKeyInfo;

//private static RSACryptoServiceProvider RSA = null;

私钥

//private const string NET_PRIVATE_KEY = @"ndSLc+4nW6DJbZKjs+UrQynUjxca1IPOIyfcZxPB7lpEQFUJWwpN+hDabWdVeFldNhaNSFg1UlQz4N2wPR030ui62ayyD66yEm0KCvAUOfw0fVhiEf/5CmoLSz+co6fAYvCf5GymwB0fjziiIorNvmZiAJyBNrm4JLbbvsoNDIU=AQAB

zS4nps270U327EPDQjcCQVQXSnOQILtJyiH8V0QoImQpT6a1dhFwLfe/bl/3L7nBr3PLk9nkPMtUdwXnZ6lrcQ==

xOwSJfUODzVETrMc2D2947krqcR+XYubvPIsiDyeYqqMFQMYA+ONZKoExn3o1tb1ORvunTApH2d/f5qq6aJgVQ==vwHio+QOnrDn19bVZUT0coCoFgUy/WWdMfElis/GVQ3Nb3sQntNpDUIAEe6AnQtehclUkVVcpkPbY9o5LEWJ4Q==JB0zOtjVSj63l0NL7/Bqyb+k3U6W6ir3VdCIEDglx+yFIjleByCNRr/Tfl+K+xOTB3Uy7ortj7/YZxuDarOHvQ==Ueugp68z1cKJXLXSFz/LRJNd+uh4vVOBt6ndBtmJ+H4gI0JgBoL8QmR5X1iiD7v9LD+5cJng5k4uriil6cAeFw==InIDqV59inrR2y8YuSc3xOW5NS1mtqC5eWS2rmxac8mRgbTNYOgj0oKhGSVnOufN9wL+/J37rSchV18qmnvo9bABSEMYNlTkViTgmAWdU3sIXa8EmFVS6sf6Ba+SBTYQLv8PyzxWXU3aXFdLGvU/WIY2QRYtIIL/mHsLrw3/p0E=";

公钥参数

//private const string PUB_KEY_MODULES = @"1lpnLvumD8/NedJ7s4WS8UO9OORbXVTgJXmfa72bI4A1L1l6Np91BETQ+yB8Fq6iGWw5OR8OB2UbRBcopb2etepDqWd7kmCtbVT36kTW+E8dWdaVjbI2BCXEGaXuzPPdGOlp52OaawYR5zyG0MiCvJ4jE7RDJax4Cl24ZqPUs4U=";

公钥参数

//private const string PUB_KEY_EXP = @"AQAB";//public RSAUtil()//{//}//#endregion//#region 私有方法

取大头的数据

//private static ushort readBEUInt16(BinaryReader br)//{//byte[] b = br.ReadBytes(2);//byte[] rb = new byte[2];//rb[0] = b[1];//rb[1] = b[0];//return BitConverter.ToUInt16(rb, 0);//}//private static bool equalBytes(byte[] first, byte[] second)//{//try//{//if (first.Length != second.Length)//{//return false;//}//for (int i = 0; i < first.Length; i++)//{//if (first[i] != second[i])//{//return false;//}//}//return true;//}//catch (Exception e)//{//Console.WriteLine(e.Message);//return false;//}//}//private static int getHead(BinaryReader br, byte elementFlag)//{//try//{//int count = 0;//byte bt = 0;//bt = br.ReadByte();//if (elementFlag != 0x00 && bt != elementFlag)//{//throw (new Exception("pem format err,element head : " + bt + " != " + elementFlag));//}//count = getElementLen(br);//return count;//}//catch (Exception e)//{//Console.WriteLine(e.Message);//return -1;//}//}//private static int getElementLen(BinaryReader br)//{//try//{//ushort count = 0;//byte bt = 0;//bt = br.ReadByte();//if (bt == 0x81)//{//count = br.ReadByte();//}//else if (bt == 0x82)//{//count = readBEUInt16(br); ;//}//else//{//count = bt;//}//return (int)count;//}//catch (Exception e)//{//Console.WriteLine(e.Message);//return -1;//}//}//private static byte[] loadBytesFromPemFile(String fileName)//{//StringBuilder sb = new StringBuilder();//using (StreamReader sr = new StreamReader(fileName))//{//String line;//do//{//line = sr.ReadLine();//} while (line != null && (line.Length == 0 || line.Substring(0, 1) != "-"));//do//{//line = sr.ReadLine();//} while (line != null && (line.Length == 0 || line.Substring(0, 1) == "-"));//while (line != null && (line.Length == 0 || line.Substring(0, 1) != "-"))//{//sb.Append(line);//line = sr.ReadLine();//}//}// //Response.Write("base64:" + sb.ToString() + "
\n");//return Convert.FromBase64String(sb.ToString());//}//private static byte[] stripLeftZeros(byte[] a)//{//int lastZero = -1;//for (int i = 0; i < a.Length; i++)//{//if (a[i] == 0)//{//lastZero = i;//}//else//{//break;//}//}//lastZero++;//byte[] result = new byte[a.Length - lastZero];//Array.Copy(a, lastZero, result, 0, result.Length);//return result;//}//private static byte[] getElement(BinaryReader br, byte elementFlag)//{//try//{//int count = 0;//byte bt = 0;//bt = br.ReadByte();//if (elementFlag != 0x00 && bt != elementFlag)//{//throw (new Exception("pem format err,element head : " + bt + " != " + elementFlag));//}//count = getElementLen(br);//byte[] value = stripLeftZeros(br.ReadBytes(count));//return value;//}//catch (Exception e)//{//Console.WriteLine(e.Message);//return null;//}//}//#endregion//#region 公有方法

/

/ 通过私key文件 获取RSAParameters/

/

/

//public static RSAParameters getPrivateKeyFromPem(String fileName)//{//byte[] keyBytes = loadBytesFromPemFile(fileName);//RSAParameters para = new RSAParameters();//BinaryReader br = new BinaryReader(new MemoryStream(keyBytes));//byte bt = 0;//ushort twoBytes = 0;//twoBytes = readBEUInt16(br);//if (twoBytes == 0x3081)//{//br.ReadByte();//}//else if (twoBytes == 0x3082)//{//br.ReadInt16();//}//else//{//throw (new Exception("pem format err,head 1: " + twoBytes + " != 0x3081 or 0x3082," + 0x3082));//}//twoBytes = readBEUInt16(br);//bt = br.ReadByte();//if (twoBytes != 0x0201 || bt != 0x00)//{//throw (new Exception("pem format err,head 2: " + twoBytes + " != 0x0201 or " + bt + " != 0x00"));//}//para.Modulus = getElement(br, 0x02);//para.Exponent = getElement(br, 0x02);//para.D = getElement(br, 0x02);//para.P = getElement(br, 0x02);//para.Q = getElement(br, 0x02);//para.DP = getElement(br, 0x02);//para.DQ = getElement(br, 0x02);//para.InverseQ = getElement(br, 0x02);//if (para.Equals(""))//{//throw (new Exception("pem format err,para=null!"));//}//return para;//}

/

/ 通过公key文件 获取RSAParameters/

/

/

//public static RSAParameters getPublicKeyFromPem(String fileName)//{//byte[] keyBytes = loadBytesFromPemFile(fileName);//RSAParameters para = new RSAParameters();//BinaryReader br = new BinaryReader(new MemoryStream(keyBytes));//byte bt = 0;//ushort twoBytes = 0;// //两个30开头的Sequence//getHead(br, 0x30);//getHead(br, 0x30);// //{ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 }//byte[] correctOid = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 };//byte[] oid = getElement(br, 0x06);//if (!equalBytes(correctOid, oid))//{//throw (new Exception("pem format err,oid err"));//}//bt = br.ReadByte();// //05 00//if (bt == 0x05)//{//br.ReadByte();//}//else//{// //已经获取了一个字节,只能调用两个函数组合,不能用getElement//int len = getElementLen(br);//br.ReadBytes(len);//}// //03开头的BitString,03+len+00//getHead(br, 0x03);//br.ReadByte();// //30开头的Sequence//getHead(br, 0x30);//para.Modulus = getElement(br, 0x02);//para.Exponent = getElement(br, 0x02);//if (para.Equals(""))//{//throw (new Exception("pem format err,para=null!"));//}//return para;//}//public static bool verifySignature(byte[] signature, string signedData, RSAParameters pubPara)//{//try//{//RSA = new RSACryptoServiceProvider();//RSAParameters RSAParams = RSA.ExportParameters(false);//RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();// //RSA2.ImportParameters(priPara)//RSA2.ImportParameters(pubPara);//byte[] hash = Encoding.UTF8.GetBytes(signedData);//if (RSA2.VerifyData(hash, "SHA1", signature))//{//return true;//}//else//{//return false;//}//}//catch (Exception e)//{//Console.WriteLine(e.Message);//return false;//}//}

/

/ 验证签名数据/

/秘钥

/明文

/公钥文件

/

//public static bool verifySignature(string signature, string signedData, string pubFileName)//{//RSAParameters pubPara;//pubPara = getPublicKeyFromPem(pubFileName);//byte[] sign = Convert.FromBase64String(signature);// //Convert.FromBase64String(signature);//return verifySignature(sign, signedData, pubPara);//}

/

/ 数据签名/

/需要加密的字符串

/私钥文件

/

//public static string signData(string dataToBeSigned, string priFileName)//{//RSAParameters priPara;//priPara = getPrivateKeyFromPem(priFileName);//RSA = new RSACryptoServiceProvider();// //RSA.FromXmlString(NET_PRIVATE_KEY);//RSAParameters RSAParams = RSA.ExportParameters(false);//RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();//RSA2.ImportParameters(priPara);//byte[] data = Encoding.UTF8.GetBytes(dataToBeSigned);//byte[] endata = RSA2.SignData(data, "SHA1");//return Convert.ToBase64String(endata);//}

/

/ 数据加密/

/

/

/

//public static string RSAEncrypt(string dataSign, string publicFileName)//{//RSAParameters priPara;//string hyxfmes = "";//priPara = getPublicKeyFromPem(publicFileName);//try//{//RSA = new RSACryptoServiceProvider();//RSAParameters RSAParams = RSA.ExportParameters(false);//RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();//RSA2.ImportParameters(priPara);//byte[] hash = Encoding.UTF8.GetBytes(dataSign);//byte[] de = RSA2.Encrypt(hash, false);//hyxfmes = Convert.ToBase64String(de, Base64FormattingOptions.None);//return hyxfmes;//}//catch (Exception e)//{//return "数据加密失败!";//}//}

/

/ 数据解密/

/

/

/

//public static string RSADecrypt(string dataSigned, string privateFileName)//{//RSAParameters pubPara;//pubPara = getPrivateKeyFromPem(privateFileName);//try//{//RSA = new RSACryptoServiceProvider();//RSAParameters RSAParams = RSA.ExportParameters(false);//RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();//RSA2.ImportParameters(pubPara);//byte[] hash = Convert.FromBase64String(dataSigned);//byte[] de = RSA2.Decrypt(hash, false);//return Encoding.UTF8.GetString(de);//}//catch (Exception e)//{//return e.ToString();//}//}//#endregion

///

///获取公钥///

///

///

private static AsymmetricKeyParameter GetPublicKeyParameter(strings)

{

s= s.Replace("\r", "").Replace("\n", "").Replace(" ", "");byte[] publicInfoByte =Convert.FromBase64String(s);

Asn1Object pubKeyObj= Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入

AsymmetricKeyParameter pubKey =PublicKeyFactory.CreateKey(publicInfoByte);returnpubKey;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值