【绝迹篇】RSA加密算法(私钥加签公钥验签)

时间:2024-09-14 09:08:08

对于上上篇博客中我讲的一个故事,本文引用: https://www.cnblogs.com/ButterflyEffect/p/9851403.html

故事中提到的关于加密会出现,私钥加密,公钥解密的情况,这种情况我们将它称为【私钥加签,公钥验签】

说句好理解的话:

既然是加密,那肯定是不希望别人知道我的消息,所以只有我才能解密,所以可得出【公钥负责加密,私钥负责解密】

同理,既然是签名,那肯定是不希望有人冒充我发消息,只有我才能发布这个签名,所以可得出【私钥负责加签,公钥负责验签】

由以上得出关于安全性的问题,再举个好例子:

现在有A(私钥A、公钥A),B(私钥B、公钥B) ,A向B发送消息,用私钥A加签、用公钥B加密,发送给B,B用私钥B解密,然后用公钥A验签。

这样的话就能保证是A发的消息,并且只有B自己才能解密。这样是非常安全的!!!

话不多少,下面直接撸代码,以下代码是实现【私钥加签公钥验签】的功能,公钥加密私钥解密的功能在我其它博客有讲过...

 #region 私钥加密,公钥解密
/// <summary>
/// 私钥加密
/// </summary>
/// <param name="privateKey">RSA私钥 base64格式</param>
/// <param name="contentData">待加密的数据</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static string EncryptWithPrivateKey(string privateKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding")
{
return Convert.ToBase64String(EncryptWithPrivateKey(Convert.FromBase64String(privateKey), contentData, algorithm));
}
/// <summary>
/// 私钥加密
/// </summary>
/// <param name="privateKey">RSA私钥</param>
/// <param name="contentData">待加密的数据</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static byte[] EncryptWithPrivateKey(byte[] privateKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding")
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKey);
return Transform(privateKeyParam, contentData, algorithm, true);
}
/// <summary>
/// 公钥解密
/// </summary>
/// <param name="publicKey">RSA公钥 base64格式</param>
/// <param name="content">待解密数据 base64格式</param>
/// <param name="encoding">解密出来的数据编码格式,默认UTF-8</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static string DecryptWithPublicKey(string publicKey, string content, string encoding = "UTF-8", string algorithm = "RSA/ECB/PKCS1Padding")
{
return Encoding.GetEncoding("GB2312").GetString(DecryptWithPublicKey(Convert.FromBase64String(publicKey), Convert.FromBase64String(content), algorithm));
}
/// <summary>
/// 公钥解密
/// </summary>
/// <param name="publicKey">RSA公钥</param>
/// <param name="contentData">待解密数据</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static byte[] DecryptWithPublicKey(byte[] publicKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding")
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKey);
return Transform(publicKeyParam, contentData, algorithm, false);
}
#endregion
private static byte[] Transform(AsymmetricKeyParameter key, byte[] contentData, string algorithm, bool forEncryption)
{
var c = CipherUtilities.GetCipher(algorithm);
c.Init(forEncryption, new ParametersWithRandom(key));
return c.DoFinal(contentData);
}

顺带提下,这里的第三方RSA引用包可以在【程序包管理器控制台】输入命令提示

PM> Install-Package BouncyCastle

完成包的安装

static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string xmlPrivateKey = rsa.ToXmlString(true);//XML密钥
string xmlPublicKey = rsa.ToXmlString(false);//XML公钥 string str = "测试数据";
byte[] b = System.Text.Encoding.Default.GetBytes(str);//字符串转字节数组,byte[] string EncryptedData = EncryptWithPrivateKey(B(xmlPrivateKey), b);//返回加密后的base64格式数据
Console.WriteLine("私钥加密结果:" + EncryptedData);
string DecipheringData = DecryptWithPublicKey(A(xmlPublicKey), EncryptedData);//返回解密后的明文数据
Console.WriteLine("公钥解密结果:" + DecipheringData); Console.ReadLine();
} //XML格式转base64格式,公钥
public static string A(string xml)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml);
var p = rsa.ExportParameters(false);
RsaKeyParameters key = new RsaKeyParameters(false, new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent)); SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(key);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
string publicKey = Convert.ToBase64String(serializedPublicBytes);
return publicKey;
} //XML格式转base64格式,私钥
public static string B(string xml)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml);
var p = rsa.ExportParameters(true);
var key = new RsaPrivateCrtKeyParameters(
new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent), new BigInteger(1, p.D),
new BigInteger(1, p.P), new BigInteger(1, p.Q), new BigInteger(1, p.DP), new BigInteger(1, p.DQ),
new BigInteger(1, p.InverseQ)); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(key);
byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
string privateKey = Convert.ToBase64String(serializedPrivateBytes);
return privateKey;
}

最后加密测试可以在这个网站去测试 http://tool.chacuo.net/cryptrsapubkey