C# RSA加密解密 签名实现
class RSACryptoItem { public RSACryptoServiceProvider Provider; public List<byte> PubKeyBytes; } public class RSAManager { private RSACryptoItem item; public RSAManager() { item = GenRSACryptoItem(); } private RSACryptoItem GenRSACryptoItem() { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); List<byte> pubKeyBytes = new List<byte>(provider.ExportCspBlob(false)); return new RSACryptoItem { Provider = provider, PubKeyBytes = pubKeyBytes, }; } /// <summary> /// 使用公钥加密 /// </summary> /// <param name="inBytes"></param> /// <param name="publicKey"></param> /// <returns></returns> public static byte[] EncryptDataByPublicKey(byte[] inBytes, string publicKey) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publicKey); return provider.Encrypt(inBytes, false); } /// <summary> /// 使用私钥解密 /// </summary> /// <param name="inBytes"></param> /// <param name="privateKey"></param> /// <returns></returns> public static byte[] DecryptDataByPrivateKey(byte[] inBytes,string privateKey) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privateKey); return provider.Decrypt(inBytes, false); } /// <summary> /// 生成数字签名 /// </summary> /// <param name="originalText">原文</param> /// <param name="privateKey"></param> /// <returns></returns> public static string GenSign(string originalText,string privateKey) { byte[] byteData = Encoding.UTF8.GetBytes(originalText); RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privateKey); //使用SHA1进行摘要算法,生成签名 byteData = provider.SignData(byteData, new SHA1CryptoServiceProvider()); return Convert.ToBase64String(byteData); } /// <summary> /// 验证签名 /// </summary> /// <param name="originalText">原文</param> /// <param name="SignedData">签名</param> /// <param name="publicKey">公钥</param> /// <returns></returns> public static bool VerifySigned(string originalText, string signedData, string publicKey) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publicKey); byte[] byteData = Encoding.UTF8.GetBytes(originalText); byte[] signData = Convert.FromBase64String(signedData); return provider.VerifyData(byteData, new SHA1CryptoServiceProvider(), signData); } public byte[] EncryptData(byte[] inBytes) { if (item != null) return item.Provider.Encrypt(inBytes, false); return null; } public byte[] DecryptData(byte[] inBytes) { if (item != null) return item.Provider.Decrypt(inBytes,false); return null; } }
使用例子: static void Main(string[] args) { //rsa使用方法1 //RSAManager rsaManager = new RSAManager(); string str = "hello world!!!"; //byte[] encryptStr = rsaManager.EncryptData(Encoding.UTF8.GetBytes(str)); //Console.WriteLine("Encrypt Data:"); //foreach (var b in encryptStr) //{ // Console.Write(b); //} //Console.WriteLine(); //Console.WriteLine("Decrypt Data:"); //Console.WriteLine(Encoding.UTF8.GetString(rsaManager.DecryptData(encryptStr))); //rsa使用方法2 //RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); //string publicKey = provider.ToXmlString(false); //string privateKey = provider.ToXmlString(true); //byte[] encryptStr = RSAManager.EncryptDataByPublicKey(Encoding.UTF8.GetBytes(str), publicKey); //Console.WriteLine("Encrypt Data:"); //foreach (var b in encryptStr) //{ // Console.Write(b); //} //Console.WriteLine(); //Console.WriteLine("Decrypt Data:"); //Console.WriteLine(Encoding.UTF8.GetString(RSAManager.DecryptDataByPrivateKey(encryptStr,privateKey))); //使用数字签名 RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); string publicKey = provider.ToXmlString(false); string privateKey = provider.ToXmlString(true); string originalText = "hello world!!!"; string signStr = RSAManager.GenSign(originalText, privateKey); Console.WriteLine("sign:\r\n" + signStr +"\r\n"); //Console.WriteLine($"VerifySigned:\r\n{RSAManager.VerifySigned(originalText, signStr, publicKey)}"); Console.WriteLine($"VerifySigned:\r\n{RSAManager.VerifySigned("error string code", signStr, publicKey)}"); Console.ReadKey(); }