因為C#的RSA加密解密只有公鑰加密,私鑰解密,沒有私鑰加密,公鑰解密。在網上查了很久也沒有很好的實現。BouncyCastle的文檔少之又少。很多人可能會說,C#也是可以的,通過Biginteger開源類來實現,不過那個是有一個文章,不過他加密出來的是16進制結果的。根本不能和JAVA互通。連加密出來的都不和C#原生的加密出來的結果格式一樣。所以還是沒有好的解決方法。
接下來還是不斷的找資料,找方法。找朋友找同事。個個都找。問題是有的,方法也是有的,所以總結各路大神之后寫了這個類。實現了私鑰加密,公鑰解密。並通過在線的校驗之后,發布上來。大家可以做一個DEMO,然后進去在線RSA加密解密校驗。
在線RSA,DES等加密解密地址:
http://tool.chacuo.net/cryptrsapubkey
下面直接粘貼代碼,不多說:
BouncyCastle相關DLL,估計不用我多說,大家可以百度下載。然后引用就可以了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto.Encodings;
namespace CryptionUtils
{
public class RSAForJava
{
public RSAForJava()
{
}
/// <summary>
/// KEY 結構體
/// </summary>
public struct RSAKEY
{
/// <summary>
/// 公鑰
/// </summary>
public string PublicKey
{
get;
set;
}
/// <summary>
/// 私鑰
/// </summary>
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 s)
{
s = s.Replace("\r", "").Replace("\n", "").Replace(" ","");
byte[] publicInfoByte = Convert.FromBase64String(s);
Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//這里也可以從流中讀取,從本地導入
AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
return pubKey;
}
private AsymmetricKeyParameter GetPrivateKeyParameter(string s)
{
s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
byte[] privateInfoByte = Convert.FromBase64String(s);
// Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//這里也可以從流中讀取,從本地導入
// PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
return priKey;
}
public string EncryptByPrivateKey(string s,string key)
{
//非對稱加密算法,加解密用
IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
//加密
try
{
engine.Init(true, GetPrivateKeyParameter(key));
byte[] byteData = System.Text.Encoding.UTF8.GetBytes(s);
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
return Convert.ToBase64String(ResultData);
//Console.WriteLine("密文(base64編碼):" + Convert.ToBase64String(testData) + Environment.NewLine);
}
catch (Exception ex)
{
return ex.Message;
}
}
public string DecryptByPublicKey(string s,string key)
{
s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
//非對稱加密算法,加解密用
IAsymmetricBlockCipher engine = new Pkcs1Encoding( new RsaEngine());
//解密
try
{
engine.Init(false, GetPublicKeyParameter(key));
byte[] byteData = Convert.FromBase64String(s);
var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
return System.Text.Encoding.UTF8.GetString(ResultData);
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}

