原文地址:http://docode.top/Article/Detail/10003 目錄: 1、.Net(C#)平臺下Des加密解密源代碼 2、.Net(C#)平臺下Aes加密解密源代碼 3、.Net(C#)平臺下Sha1加密解密源代碼 4、.Net(C#)平臺下MD5加密解密源代碼 5、總結 ...
原文地址:http://docode.top/Article/Detail/10003
目錄:
5、總結
一、.Net(C#)平臺下Des加密解密源代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
public class DesEncryptHelper
{
/// <summary>
/// Des預設密鑰向量
/// </summary>
public static string DesIv
{
get
{
return "20160602" ; // 此處可自定義,8個字元長度
}
}
/// <summary>
/// Des加解密鑰必須8位
/// </summary>
public static string DesKey
{
get
{
return "20160602" ; // 此處可自定義,8個字元長度
}
}
/// <summary>
/// 獲取Des8位密鑰
/// </summary>
/// <param name="key">Des密鑰字元串</param>
/// <param name="encoding">編碼類型</param>
/// <returns>Des8位密鑰</returns>
static byte [] GetDesKey( string key, Encoding encoding)
{
if ( string .IsNullOrEmpty(key))
{
throw new ArgumentNullException( "key" , "Des密鑰不能為空" );
}
if (key.Length > 8)
{
key = key.Substring(0, 8);
}
if (key.Length < 8)
{
// 不足8補全
key = key.PadRight(8, '0' );
}
return encoding.GetBytes(key);
}
/// <summary>
/// Des加密
/// </summary>
/// <param name="source">源字元串</param>
/// <param name="encoding">編碼類型</param>
/// <returns>加密後的字元串</returns>
public string EncryptDes( string source, Encoding encoding = null )
{
return EncryptDes(source, DesKey, DesIv, encoding);
}
/// <summary>
/// Des加密
/// </summary>
/// <param name="source">源字元串</param>
/// <param name="key">des密鑰,長度必須8位</param>
/// <param name="iv">密鑰向量</param>
/// <param name="encoding">編碼類型</param>
/// <returns>加密後的字元串</returns>
public static string EncryptDes( string source, string key, string iv, Encoding encoding = null )
{
if (encoding == null ) encoding = Encoding.UTF8;
byte [] rgbKeys = GetDesKey(key, encoding),
rgbIvs = GetDesKey(iv, encoding),
inputByteArray = encoding.GetBytes(source);
using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream,
desProvider.CreateEncryptor(rgbKeys, rgbIvs), CryptoStreamMode.Write))
{
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
// 1.第一種
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
memoryStream.Flush();
memoryStream.Close();
desProvider.Clear();
string result = Convert.ToBase64String(memoryStream.ToArray());
return result;
// 2.第二種
//StringBuilder result = new StringBuilder();
//foreach (byte b in memoryStream.ToArray())
//{
// result.AppendFormat("{0:X2}", b);
//}
//cryptoStream.FlushFinalBlock();
//cryptoStream.Close();
//memoryStream.Flush();
//memoryStream.Close();
//desProvider.Clear();
//return result.ToString();
}
}
}
}
/// <summary>
/// Des解密
/// </summary>
/// <param name="source">源字元串</param>
/// <param name="encoding">編碼類型</param>
/// <returns>解密後的字元串</returns>
public static string DecryptDes( string source, Encoding encoding = null )
{
return DecryptDes(source, DesKey, DesIv, encoding);
}
/// <summary>
/// Des解密
/// </summary>
/// <param name="source">源字元串</param>
/// <param name="key">des密鑰,長度必須8位</param>
/// <param name="iv">密鑰向量</param>
/// <param name="encoding">編碼類型</param>
/// <returns>解密後的字元串</returns>
public static string DecryptDes( string source, string key, string iv, Encoding encoding = null )
{
if (encoding == null ) encoding = Encoding.UTF8;
byte [] rgbKeys = GetDesKey(key, encoding),
rgbIvs = GetDesKey(iv, encoding),
inputByteArray = Convert.FromBase64String(source);
using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream,
desProvider.CreateDecryptor(rgbKeys, rgbIvs), CryptoStreamMode.Write))
{
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
memoryStream.Flush();
memoryStream.Close();
desProvider.Clear();
byte [] result = memoryStream.ToArray();
return encoding.GetString(result);
}
}
}
}
}
|