重出江湖|C#.net 加密大全

非对称加密:

RSA

[csharp]  view plain copy
  1. /* 作者:GhostBear  
  2.    博客地址:Http://blog.csdn.net/ghostbear  
  3. */  
  4. static string EnRSA(string data,string publickey)  
  5. {  
  6.     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();   
  7.     byte[] cipherbytes;   
  8.     rsa.FromXmlString(publickey);   
  9.     cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(data), false);   
  10.     return Convert.ToBase64String(cipherbytes);  
  11.       
  12. }  
  13.   
  14. static string DeRSA(string data,string privatekey)  
  15. {  
  16.      
  17.     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();   
  18.     byte[] cipherbytes; rsa.FromXmlString(privatekey);   
  19.     cipherbytes = rsa.Decrypt(Convert.FromBase64String(data), false);   
  20.     return Encoding.UTF8.GetString(cipherbytes);  
  21. }  


调用代码

[csharp]  view plain copy
  1.   Console.WriteLine("RSA非对称加密");  
  2. Byte[] iv = CreateKey(16);  
  3. Byte[] key = CreateKey(27);  
  4. string inputRSA_1 = inputString();  
  5. RSA rsa = RSA.Create();  
  6.   
  7. string enData = EnRSA(inputRSA_1,rsa.ToXmlString(false));  
  8.   
  9.   
  10. Console.WriteLine("加密后的数据:{0}", enData);  
  11. Console.WriteLine("解密后的数据:{0}", DeRSA(enData,rsa.ToXmlString(true)));  


 

DSA(数字签名)

[csharp]  view plain copy
  1.  /* 作者:GhostBear  
  2.    博客地址:Http://blog.csdn.net/ghostbear  
  3. */  
  4.  static string EnDSA(string data,string publickey)  
  5.    {  
  6.        DSA dsa = DSA.Create();  
  7.        Byte[] result;  
  8.        dsa.FromXmlString(publickey);  
  9.        SHA1 sha1 = SHA1.Create();  
  10.        result = dsa.CreateSignature(sha1.ComputeHash(Convert.FromBase64String(data)));  
  11.        return Convert.ToBase64String(result);  
  12.   
  13.          
  14.    }  
  15.   
  16.    static bool DeDSA(string data,string privatekey,string originalData)  
  17.    {  
  18.        //Byte[] result;  
  19.        DSA dsa = DSA.Create();  
  20.        dsa.FromXmlString(privatekey);  
  21.        SHA1 sha1 = SHA1.Create();  
  22.        return dsa.VerifySignature(sha1.ComputeHash(Convert.FromBase64String(originalData)),Convert.FromBase64String(data));  
  23.   
  24.    }  

调用代码

[csharp]  view plain copy
  1.   Console.WriteLine("DSA数字签名");  
  2. string inputDSA_1 = inputString();  
  3. string inputDSA_2 = inputDSA_1;  
  4. DSA dsa = DSA.Create();  
  5. string enData = EnDSA(inputDSA_1, dsa.ToXmlString(true));  
  6.   
  7. Console.WriteLine("加密后的数据:{0}", enData);  
  8. Console.WriteLine("解密后的数据:{0}", DeDSA(enData, dsa.ToXmlString(false), inputDSA_2));  


 

 

ECDsa

[csharp]  view plain copy
  1. /* 作者:GhostBear  
  2.    博客地址:Http://blog.csdn.net/ghostbear  
  3. */  
  4. static string EnECDsa(string data, CngKey key)  
  5.  {  
  6.      ECDsaCng ecdsa = new ECDsaCng(key);  
  7.        
  8.        
  9.      SHA1 sha1 = SHA1.Create();  
  10.      byte[] result;  
  11.   
  12.      result = ecdsa.SignHash(sha1.ComputeHash(Convert.FromBase64String(data)));  
  13.   
  14.      return Convert.ToBase64String(result);  
  15.   
  16.  }  
  17.   
  18.  static bool DeECDsa(string data, CngKey key,string originalData)  
  19.  {  
  20.      ECDsaCng ecdsa = new ECDsaCng(key);  
  21.      SHA1 sha1 = SHA1.Create();  
  22.   
  23.      return ecdsa.VerifyHash(sha1.ComputeHash(Convert.FromBase64String(originalData)), Convert.FromBase64String(data));  
  24.  }  


调用代码

[csharp]  view plain copy
  1.   Console.WriteLine("ECDsa数字签名");  
  2. string inputDSA_1 = inputString();  
  3. string inputDSA_2 = inputDSA_1;  
  4. CngKey key = CngKey.Create(CngAlgorithm.ECDsaP256);  
  5.   
  6. string enData = EnECDsa(inputDSA_1, key);  
  7.   
  8. Console.WriteLine("加密后的数据:{0}", enData);  
  9. Console.WriteLine("解密后的数据:{0}", DeECDsa(enData, key, inputDSA_2));  

对称加密:

公共代码

[csharp]  view plain copy
  1. static byte[] CreateKey(int num)  
  2. {  
  3.     byte[] result = new byte[num];  
  4.     Random rand = new Random();  
  5.     for (int i = 0; i < num; i++)  
  6.     {  
  7.         result[i] = (Byte)rand.Next(1, 256);  
  8.     }  
  9.     return result;  
  10.   
  11. }  


 

DES

[csharp]  view plain copy
  1. /* 
  2.   作者:GhostBear 
  3.   博客地址:Http://blog.csdn.net/ghostbear 
  4. */  
  5.   
  6. /// <summary>  
  7. /// DES加密算法必须使用Base64的Byte对象  
  8. /// </summary>  
  9. /// <param name="data">待加密的字符数据</param>  
  10. /// <param name="key">密匙,长度必须为64位(byte[8]))</param>  
  11. /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>  
  12. /// <returns>加密后的字符</returns>  
  13. static string EnDES(string data, byte[] key, byte[] iv)  
  14. {  
  15.     DES des = DES.Create();  
  16.     //这行代码很重要,需要根据不同的字符串选择不同的转换格式  
  17.     byte[] tmp = Encoding.Unicode.GetBytes(data);  
  18.     Byte[] encryptoData;  
  19.   
  20.     ICryptoTransform encryptor = des.CreateEncryptor(key, iv);  
  21.     using (MemoryStream memoryStream = new MemoryStream())  
  22.     {  
  23.         using (var cs = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))  
  24.         {  
  25.             using (StreamWriter writer = new StreamWriter(cs))  
  26.             {  
  27.                 writer.Write(data);  
  28.                 writer.Flush();  
  29.             }  
  30.         }  
  31.         encryptoData = memoryStream.ToArray();  
  32.     }  
  33.     des.Clear();  
  34.   
  35.     return Convert.ToBase64String(encryptoData);  
  36.   
  37. }  
  38.   
  39. /// <summary>  
  40. /// DES解密算法  
  41. /// </summary>  
  42. /// <param name="data">待加密的字符数据</param>  
  43. /// <param name="key">密匙,长度必须为64位(byte[8]))</param>  
  44. /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>  
  45. /// <returns>加密后的字符</returns>  
  46. static string DeDes(string data, Byte[] key, Byte[] iv)  
  47. {  
  48.     string resultData = string.Empty;  
  49.     //这行代码很重要  
  50.     Byte[] tmpData = Convert.FromBase64String(data);//转换的格式挺重要  
  51.     DES des = DES.Create();  
  52.   
  53.     ICryptoTransform decryptor = des.CreateDecryptor(key, iv);  
  54.     using (var memoryStream = new MemoryStream(tmpData))  
  55.     {  
  56.         using (var cs = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))  
  57.         {  
  58.   
  59.             StreamReader reader = new StreamReader(cs);  
  60.             resultData = reader.ReadLine();  
  61.         }  
  62.   
  63.     }  
  64.   
  65.     return resultData;  
  66.   
  67.   
  68. }  


 

调用代码

[csharp]  view plain copy
  1.             //DES对称加密  
  2.             Console.WriteLine("DES对称加密");  
  3.             Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };  
  4.             Byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };  
  5.             string inputDES_1 = inputString();  
  6.             string enData = EnDES(inputDES_1, key, iv);<p>  
  7.             Console.WriteLine("加密后的数据:{0}", enData);  
  8.             Console.WriteLine("解密后的数据:{0}", DeDes(enData, key, iv));</p>  
  9.    

 

 

TripleDES(3DES)

[csharp]  view plain copy
  1. /*     作者:GhostBear *    
  2.        博客地址:Http://blog.csdn.net/ghostbear  
  3. */  
  4.  /// <summary>  
  5.  /// TripleDES加密  
  6.  /// </summary>  
  7.  /// <param name="data">待加密的字符数据</param>  
  8.  /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24])</param>  
  9.  /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>  
  10.  /// <returns>加密后的字符</returns>  
  11.  static string EnTripleDES(string data, Byte[] key, Byte[] iv)  
  12.  {  
  13.      Byte[] tmp = null;  
  14.      Byte[] tmpData = Encoding.Unicode.GetBytes(data);  
  15.   
  16.      TripleDES tripleDes = TripleDES.Create();  
  17.   
  18.   
  19.      ICryptoTransform encryptor = tripleDes.CreateEncryptor(key, iv);  
  20.      using (MemoryStream ms = new MemoryStream())  
  21.      {  
  22.          using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))  
  23.          {  
  24.              StreamWriter writer = new StreamWriter(cs);  
  25.              writer.Write(data);  
  26.              writer.Flush();//这句很重要,在对流操作结束后必须用这句话强制将缓冲区中的数据全部写入到目标对象中  
  27.          }  
  28.          tmp = ms.ToArray();  
  29.      }  
  30.      return Convert.ToBase64String(tmp);  
  31.   
  32.   
  33.  }  
  34.   
  35.  /// <summary>  
  36.  /// TripleDES解密  
  37.  /// </summary>  
  38.  /// <param name="data">待加密的字符数据</param>  
  39.  /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24])</param>  
  40.  /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>  
  41.  /// <returns>加密后的字符</returns>  
  42.  static string DeTripleDES(string data, Byte[] key, Byte[] iv)  
  43.  {  
  44.      Byte[] tmp = Convert.FromBase64String(data);  
  45.      string result = string.Empty;  
  46.   
  47.      TripleDES tripleDES = TripleDES.Create();  
  48.      ICryptoTransform decryptor = tripleDES.CreateDecryptor(key, iv);  
  49.   
  50.      using (MemoryStream ms = new MemoryStream(tmp))  
  51.      {  
  52.          using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))  
  53.          {  
  54.              StreamReader reader = new StreamReader(cs);  
  55.              result = reader.ReadLine();  
  56.          }  
  57.      }  
  58.      tripleDES.Clear();  
  59.      return result;  
  60.   
  61.   
  62.   
  63.   
  64.    
  65.  }  


调用代码

[csharp]  view plain copy
  1.      
  2.               TripleDES对称加密  
  3.             Console.WriteLine("TripleDES对称加密");  
  4.             Byte[] iv = CreateKey(8);  
  5.             Byte[] key = CreateKey(32);  
  6.             string inputDES_1 = inputString();              
  7.             string enData = EnTripleDES(inputDES_1, key, iv);  
  8.             Console.WriteLine("加密后的数据:{0}", enData);  
  9.             Console.WriteLine("解密后的数据:{0}", DeTripleDES(enData, key, iv)); <  

 

AES

[csharp]  view plain copy
  1.  /*     作者:GhostBear   
  2.         博客地址:Http://blog.csdn.net/ghostbear  
  3. */  
  4.  /// <summary>  
  5.  /// AES加密  
  6.  /// </summary>  
  7.  /// <param name="data">待加密的字符数据</param>  
  8.  /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>  
  9.  /// <param name="iv">iv向量,长度必须为128位(byte[16])</param>  
  10.  /// <returns>加密后的字符</returns>  
  11.  static string EnAES(string data, byte[] key, byte[] iv)  
  12.  {  
  13.      Aes aes = Aes.Create();  
  14.      byte[] tmp = null;  
  15.   
  16.      ICryptoTransform encryptor = aes.CreateEncryptor(key, iv);  
  17.      using (MemoryStream ms = new MemoryStream())  
  18.      {  
  19.          using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))  
  20.          {  
  21.              StreamWriter writer = new StreamWriter(cs);  
  22.              writer.Write(data);  
  23.              writer.Flush();  
  24.              writer.Close();  
  25.          }  
  26.          tmp = ms.ToArray();  
  27.      }  
  28.      return Convert.ToBase64String(tmp);  
  29.  }  
  30.   
  31.  /// <summary>  
  32.  /// AES解密  
  33.  /// </summary>  
  34.  /// <param name="data">待加密的字符数据</param>  
  35.  /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>  
  36.  /// <param name="iv">iv向量,长度必须为128位(byte[16])</param>  
  37.  /// <returns>加密后的字符</returns>  
  38.  static string DeAES(string data, byte[] key, byte[] iv)  
  39.  {  
  40.      string result = string.Empty;  
  41.      Aes aes = Aes.Create();  
  42.   
  43.      ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);  
  44.      using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data)))  
  45.      {  
  46.          using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))  
  47.          {  
  48.              StreamReader reader = new StreamReader(cs);  
  49.              result = reader.ReadLine();  
  50.              reader.Close();  
  51.          }  
  52.      }  
  53.      aes.Clear();  
  54.      return result;  
  55.  }  


调用代码

[csharp]  view plain copy
  1. <p>            //AES对称加密  
  2.             Console.WriteLine("AES对称加密");  
  3.             Byte[] iv = CreateKey(16);  
  4.             Byte[] key = CreateKey(24);  
  5.             string inputDES_1 = inputString();</p><p>            string enData =EnAES(inputDES_1, key, iv);</p><p>  
  6.             Console.WriteLine("加密后的数据:{0}", enData);  
  7.             Console.WriteLine("解密后的数据:{0}", DeAES(enData, key, iv));</p>  
  8.    

 

Rijndael

[csharp]  view plain copy
  1. /*     作者:GhostBear   
  2.        博客地址:Http://blog.csdn.net/ghostbear  
  3. */  
  4.  /// <summary>  
  5.  /// Rijndael加密  
  6.  /// </summary>  
  7.  /// <param name="data">需要加密的字符数据</param>  
  8.  /// <param name="key">密匙,长度可以为:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param>  
  9.  /// <param name="iv">iv向量,长度为128(byte[16])</param>  
  10.  /// <returns>加密后的字符</returns>  
  11.  static string EnRijndael(string data, byte[] key, byte[] iv)  
  12.  {  
  13.      Rijndael rijndael = Rijndael.Create();  
  14.      byte[] tmp = null;  
  15.      ICryptoTransform encryptor = rijndael.CreateEncryptor(key, iv);  
  16.   
  17.      using (MemoryStream ms = new MemoryStream())  
  18.      {  
  19.          using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))  
  20.          {  
  21.              StreamWriter writer = new StreamWriter(cs);  
  22.              writer.Write(data);  
  23.              writer.Flush();  
  24.          }  
  25.          tmp = ms.ToArray();  
  26.      }  
  27.      return Convert.ToBase64String(tmp);  
  28.  }  
  29.   
  30.  /// <summary>  
  31.  /// Rijndael解密  
  32.  /// </summary>  
  33.  /// <param name="data">需要加密的字符数据</param>  
  34.  /// <param name="key">密匙,长度可以为:64位(byte[8]),128位(byte[16]),192位(byte[24]),256位(byte[32])</param>  
  35.  /// <param name="iv">iv向量,长度为128(byte[16])</param>  
  36.  /// <returns>解密后的字符</returns>  
  37.  static string DeRijndael(string data, byte[] key, byte[] iv)  
  38.  {  
  39.      string result = string.Empty;  
  40.      Rijndael rijndael = Rijndael.Create();  
  41.      ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv);  
  42.   
  43.      using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data)))  
  44.      {  
  45.          using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))  
  46.          {  
  47.              StreamReader reader = new StreamReader(cs);  
  48.              result = reader.ReadLine();  
  49.              reader.Close();  
  50.          }  
  51.      }  
  52.      return result;  
  53.  }  

 

调用代码

[csharp]  view plain copy
  1. //Rijndael对称加密  
  2. Console.WriteLine("Rijndael对称加密");  
  3. Byte[] iv = CreateKey(16);  
  4. Byte[] key = CreateKey(32);  
  5. string inputDES_1 = inputString();  
  6.   
  7. string enData = EnRijndael(inputDES_1, key, iv);  
  8.   
  9.   
  10. Console.WriteLine("加密后的数据:{0}", enData);  
  11. Console.WriteLine("解密后的数据:{0}", DeRijndael(enData, key, iv));  
哈希加密:

MD5

[csharp]  view plain copy
  1. /* 
  2.      作者:GhostBear 
  3.  *   博客地址:Http://blog.csdn.net/ghostbera 
  4.  */  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Text;  
  8.   
  9. using System.Security.Cryptography;  
  10.   
  11. namespace EncryptAndDecrypt  
  12. {  
  13.     public class MD5  
  14.     {  
  15.         public byte[] Hash(byte[] data)  
  16.         {  
  17.             System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();  
  18.   
  19.             return md5.ComputeHash(data);  
  20.         }  
  21.     }  
  22. }  


 

SHA1

[csharp]  view plain copy
  1. /* 
  2. 作者:GhostBear 
  3. 博客地址:Http://blog.csdn.net/ghostbera 
  4. */  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Text;  
  8.   
  9. using System.Security.Cryptography;  
  10.   
  11. namespace EncryptAndDecrypt  
  12. {  
  13.     public class SHA1  
  14.     {  
  15.         public byte[] Hash(byte[] data)  
  16.         {  
  17.             System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();  
  18.             return sha1.ComputeHash(data);  
  19.         }  
  20.     }  
  21. }  


 

SHA256

[csharp]  view plain copy
  1. /* 
  2. 作者:GhostBear 
  3. 博客地址:Http://blog.csdn.net/ghostbera 
  4. */  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Text;  
  8.   
  9. using System.Security.Cryptography;  
  10.   
  11. namespace EncryptAndDecrypt  
  12. {  
  13.     public class SHA256  
  14.     {  
  15.   
  16.   
  17.         public byte[] Hash(byte[] data)  
  18.         {  
  19.             System.Security.Cryptography.SHA256 sha256=  System.Security.Cryptography.SHA256.Create();  
  20.             return sha256.ComputeHash(data);  
  21.         }  
  22.     }  
  23. }  


 

SHA384

[csharp]  view plain copy
  1. /* 
  2. 作者:GhostBear 
  3. 博客地址:Http://blog.csdn.net/ghostbera 
  4. */  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Text;  
  8.   
  9. using System.Security.Cryptography;  
  10. namespace EncryptAndDecrypt  
  11. {  
  12.     public class SHA384  
  13.     {  
  14.   
  15.         public byte[] Hash(byte[] data)  
  16.         {  
  17.             System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create();  
  18.             return sha384.ComputeHash(data);  
  19.         }  
  20.     }  
  21. }  


 

SHA512

[csharp]  view plain copy
  1. /* 
  2. 作者:GhostBear 
  3. 博客地址:Http://blog.csdn.net/ghostbera 
  4. */  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Text;  
  8.   
  9. using System.Security.Cryptography;  
  10.   
  11. namespace EncryptAndDecrypt  
  12. {  
  13.     public class SHA512  
  14.     {  
  15.   
  16.         public byte[] Hash(byte[] data)  
  17.         {  
  18.             System.Security.Cryptography.SHA512 sha512 = System.Security.Cryptography.SHA512.Create();  
  19.             return sha512.ComputeHash(data);  
  20.         }  
  21.     }  
  22. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值