相比较于Des 对称可逆加密性能要差 加密解密速度不快 安全性好 公开加密key,保证数据的安全传递 公开解密key,保证数据的不可抵赖 公钥就是公开的key 私钥就是不公开的key C#内置实现了公钥加密 私钥解密;想换需要用第三方的DLL-BouncyCastle 使用方法
KeyValuePair<string, string> encryptDecrypt = RsaEncrypt.GetKeyPair(); string rsaEn1 = RsaEncrypt.Encrypt("net", encryptDecrypt.Key); string rsaDe1 = RsaEncrypt.Decrypt(rsaEn1, encryptDecrypt.Value);
相比较于前面这一种Des 对称可逆加密性能要差 加密解密速度不快 安全性好 公开加密key,保证数据的安全传递 公开解密key,保证数据的不可抵赖 公钥就是公开的key 私钥就是不公开的key C#内置实现了公钥加密 私钥解密;想换需要用第三方的DLL-BouncyCastle 使用方法 KeyValuePair<string, string> encryptDecrypt = RsaEncrypt.GetKeyPair(); string rsaEn1 = RsaEncrypt.Encrypt("net", encryptDecrypt.Key); string rsaDe1 = RsaEncrypt.Decrypt(rsaEn1, encryptDecrypt.Value); /// <summary> /// RSA ECC /// 可逆非对称加密 /// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。 /// </summary> public class RsaEncrypt { /// <summary> /// 获取加密/解密对 /// 给你一个,是无法推算出另外一个的 /// /// Encrypt Decrypt /// </summary> /// <returns>Encrypt Decrypt</returns> public static KeyValuePair<string, string> GetKeyPair() { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); string publicKey = RSA.ToXmlString(false); string privateKey = RSA.ToXmlString(true); return new KeyValuePair<string, string>(publicKey, privateKey); } /// <summary> /// 加密:内容+加密key /// /// </summary> /// <param name="content"></param> /// <param name="encryptKey">加密key</param> /// <returns></returns> public static string Encrypt(string content, string encryptKey) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(encryptKey); UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] DataToEncrypt = ByteConverter.GetBytes(content); byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false); return Convert.ToBase64String(resultBytes); } /// <summary> /// 解密 内容+解密key /// </summary> /// <param name="content"></param> /// <param name="decryptKey">解密key</param> /// <returns></returns> public static string Decrypt(string content, string decryptKey) { byte[] dataToDecrypt = Convert.FromBase64String(content); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(decryptKey); byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false); UnicodeEncoding ByteConverter = new UnicodeEncoding(); return ByteConverter.GetString(resultBytes); } /// <summary> /// 可以合并在一起的,每次产生一组新的密钥 /// </summary> /// <param name="content"></param> /// <param name="encryptKey">加密key</param> /// <param name="decryptKey">解密key</param> /// <returns>加密后结果</returns> private static string Encrypt(string content, out string publicKey, out string privateKey) { RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); publicKey = rsaProvider.ToXmlString(false); privateKey = rsaProvider.ToXmlString(true); UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] DataToEncrypt = ByteConverter.GetBytes(content); byte[] resultBytes = rsaProvider.Encrypt(DataToEncrypt, false); return Convert.ToBase64String(resultBytes); } }
标签:加密,string,RSA,解密,RSACryptoServiceProvider,key From: https://www.cnblogs.com/FanJunCheng521/p/17168476.html