您现在的位置是:网站首页> 编程资料编程资料

.NET使用RSA加密解密的方法_实用技巧_

2023-05-24 513人已围观

简介 .NET使用RSA加密解密的方法_实用技巧_

本文实例为大家分享了.NET使用RSA加密解密的具体代码,供大家参考,具体内容如下

PassWordHelper.cs代码:

 using System; using System.IO; using System.Text; using System.Globalization; using System.Collections.Generic; using System.Security.Cryptography; namespace Utils { ///  /// 密码加密解密操作相关类 ///  public static class PassWordHelper { #region MD5 加密 ///  /// MD5加密 ///  public static string Md532(this string source) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; MD5 md5 = MD5.Create(); return HashAlgorithmBase(md5, source, encoding); } ///  /// 加盐MD5加密 ///  public static string Md532Salt(this string source, string salt) { return string.IsNullOrEmpty(source) ? source.Md532() : (source + "『" + salt + "』").Md532(); } #endregion #region SHA 加密 ///  /// SHA1 加密 ///  public static string Sha1(this string source) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; SHA1 sha1 = new SHA1CryptoServiceProvider(); return HashAlgorithmBase(sha1, source, encoding); } ///  /// SHA256 加密 ///  public static string Sha256(this string source) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; SHA256 sha256 = new SHA256Managed(); return HashAlgorithmBase(sha256, source, encoding); } ///  /// SHA512 加密 ///  public static string Sha512(this string source) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; SHA512 sha512 = new SHA512Managed(); return HashAlgorithmBase(sha512, source, encoding); } #endregion #region HMAC 加密 ///  /// HmacSha1 加密 ///  public static string HmacSha1(this string source, string keyVal) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACSHA1 hmacSha1 = new HMACSHA1(keyStr); return HashAlgorithmBase(hmacSha1, source, encoding); } ///  /// HmacSha256 加密 ///  public static string HmacSha256(this string source, string keyVal) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACSHA256 hmacSha256 = new HMACSHA256(keyStr); return HashAlgorithmBase(hmacSha256, source, encoding); } ///  /// HmacSha384 加密 ///  public static string HmacSha384(this string source, string keyVal) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACSHA384 hmacSha384 = new HMACSHA384(keyStr); return HashAlgorithmBase(hmacSha384, source, encoding); } ///  /// HmacSha512 加密 ///  public static string HmacSha512(this string source, string keyVal) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACSHA512 hmacSha512 = new HMACSHA512(keyStr); return HashAlgorithmBase(hmacSha512, source, encoding); } ///  /// HmacMd5 加密 ///  public static string HmacMd5(this string source, string keyVal) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACMD5 hmacMd5 = new HMACMD5(keyStr); return HashAlgorithmBase(hmacMd5, source, encoding); } ///  /// HmacRipeMd160 加密 ///  public static string HmacRipeMd160(this string source, string keyVal) { if (string.IsNullOrEmpty(source)) return null; var encoding = Encoding.UTF8; byte[] keyStr = encoding.GetBytes(keyVal); HMACRIPEMD160 hmacRipeMd160 = new HMACRIPEMD160(keyStr); return HashAlgorithmBase(hmacRipeMd160, source, encoding); } #endregion #region AES 加密解密 ///  /// AES加密 ///  /// 待加密字段 /// 密钥值 /// 加密辅助向量 ///  public static string AesStr(this string source, string keyVal, string ivVal) { var encoding = Encoding.UTF8; byte[] btKey = keyVal.FormatByte(encoding); byte[] btIv = ivVal.FormatByte(encoding); byte[] byteArray = encoding.GetBytes(source); string encrypt; Rijndael aes = Rijndael.Create(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(btKey, btIv), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } aes.Clear(); return encrypt; } ///  /// AES解密 ///  /// 待加密字段 /// 密钥值 /// 加密辅助向量 ///  public static string UnAesStr(this string source, string keyVal, string ivVal) { var encoding = Encoding.UTF8; byte[] btKey = keyVal.FormatByte(encoding); byte[] btIv = ivVal.FormatByte(encoding); byte[] byteArray = Convert.FromBase64String(source); string decrypt; Rijndael aes = Rijndael.Create(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(btKey, btIv), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = encoding.GetString(mStream.ToArray()); } } aes.Clear(); return decrypt; } ///  /// AES Byte类型 加密 ///  /// 待加密明文 /// 密钥值 /// 加密辅助向量 ///  public static byte[] AesByte(this byte[] data, string keyVal, string ivVal) { byte[] bKey = new byte[32]; Array.Copy(Encoding.UTF8.GetBytes(keyVal.PadRight(bKey.Length)), bKey, bKey.Length); byte[] bVector = new byte[16]; Array.Copy(Encoding.UTF8.GetBytes(ivVal.PadRight(bVector.Length)), bVector, bVector.Length); byte[] cryptograph; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write)) { cStream.Write(data, 0, data.Length); cStream.FlushFinalBlock(); cryptograph = mStream.ToArray(); } } } catch { cryptograph = null; } return cryptograph; } ///  /// AES Byte类型 解密 ///  /// 待解密明文 /// 密钥值 /// 加密辅助向量 ///  public static byte[] UnAesByte(this byte[] data, string keyVal, string ivVal) { byte[] bKey = new byte[32]; Array.Copy(Encoding.UTF8.GetBytes(keyVal.PadRight(bKey.Length)), bKey, bKey.Length); byte[] bVector = new byte[16]; Array.Copy(Encoding.UTF8.GetBytes(ivVal.PadRight(bVector.Length)), bVector, bVector.Length); byte[] original; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream(data)) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read)) { using (MemoryStream originalMemory = new MemoryStream()) { byte[] buffer = new byte[1024]; int readBytes; while ((readBytes = cStream.Read(buffer, 0, buffer.Length)) > 0) { originalMemory.Write(buffer, 0, readBytes); } original = originalMemory.ToArray(); } } } } catch { original = null; } return original; } #endregion #region RSA 加密解密 //密钥对,请配合密钥生成工具使用『 http://download.csdn.net/detail/downiis6/9464639 』 private const string PublicRsaKey = @"8Yvf/LjXRhCuOREk2CuSYvbD/RadwJ4sjHREIpQVKwkTlG3BtRgpnaMcoeLAesmwvpBWnqK4hBkYLxhRj+NEKnlGrJ+LkNMnZr0/4CMuulZFAnx7iQYaSq7Eh7kBKGLofc05CjZguYpnPNxHIv4VNx+a9tIh+hnhjrmkJLUm3l0=AQAB"; private const string PrivateRsaKey = @"8Yvf/LjXRhCuOREk2CuSYvbD/RadwJ4sjHREIpQVKwkTlG3BtRgpnaMcoeLAesmwvpBWnqK4hBkYLxhRj+NEKnlGrJ+LkNMnZr0/4CMuulZFAnx7iQYaSq7Eh7kBKGLofc05CjZguYpnPNxHIv4VNx+a9tIh+hnhjrmkJLUm3l0=AQAB

/xAaa/4dtDxcEAk5koSZBPjuxqvKJikpwLA1nCm3xxAUMDVxSwQyr+SHFaCnBN9kqaNkQCY6kDCfJXFWPOj0Bw==

8m8PFVA4sO0oEKMVQxt+ivDTHFuk/W154UL3IgC9Y6bzlvYewXZSzZHmxZXXM1lFtwoYG/k+focXBITsiJepew==ONVSvdt6rO2CKgSUMoSfQA9jzRr8STKE3i2lVG2rSIzZosBVxTxjOvQ18WjBroFEgdQpg23BQN3EqGgvqhTSQw==gfp7SsEM9AbioTDemHEoQlPly+FyrxE/9D8UAt4ErGX5WamxSaYntOGRqcOxcm1djEpULMNP90R0Wc7uhjlR+w==C0eBsp2iMOxWwKo+EzkHOP0H+YOitUVgjekGXmSt9a3TvikQNaJ5ATlqKsZaMGsnB6UIHei+kUaCusVX0HgQ2A==tPYxEfo9Nb3LeO+SJe3G1yO+w37NIwCdqYB1h15f2YUMSThNVmpKy1HnYpUp1RQDuVETw/duu3C9gJL8kAsZBjBrVZ0zC/JZsgvSNprfUK3Asc4FgFsGfQGKW1nvvgdMbvqr4ClB0R8czkki+f9Oc5ea/RMqXxlI+XjzMYDEknU=
"; /// /// RSA 加密 /// public static string Rsa(this string source) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(PublicRsaKey); var cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(source), true); return Convert.ToBase64String(cipherbytes); } /// /// RSA解密 /// public static string UnRsa(this string source) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(PrivateRsaKey); var cipherbytes = rsa.Decrypt(Convert.FromBase64String(source), true); return Encoding.UTF8.GetString(cipherbytes); } #endregion #region DES 加密解密 /// /// DES 加密 /// public static string Des(this string source, string keyVal, string ivVal) { try { byte[] data = Encoding.UTF8.GetBytes(source); var des = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(keyVal.Length > 8 ? keyVal.Substring(0, 8) : keyVal), IV = Encoding.ASCII.GetBytes(ivVal.Length > 8 ? ivVal.Substring(0, 8) : ivVal) }; var desencrypt = des.CreateEncryptor(); byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); return BitConverter.ToString(result); } catch { return "转换出错!"; } } /// /// DES 解密 /// public static string UnDes(this string source, string keyVal, string ivVal) { try { string[] sInput = source.Split("-".ToCharArray()); byte[] data = new byte[sInput.

-六神源码网