des加密解密源碼 C# key值問題分析
公司協(xié)議安全需求、需要對傳輸內(nèi)容做des、md5加密。
因?yàn)槭切氯?、剛交給我這個任務(wù)的時候有點(diǎn)眩暈。就開始在網(wǎng)上找各種des加密的內(nèi)容。因?yàn)椴欢詾樾枰言硪哺忝靼?,最后誤了時間、把自己也搞糊涂了。當(dāng)然,邏輯能力強(qiáng)、有興趣的朋友可以試著去搞搞。
先貼加密、解密的源碼:
/// <summary>
/// 加密數(shù)據(jù)
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text, string sKey) {
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray()) {
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
#endregion
/// <summary>
/// 解密數(shù)據(jù)
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text, string sKey) {
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++) {
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i; }
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray()); }
#endregion
因?yàn)槭堑谝淮谓佑|des并且公司協(xié)議文檔的需求、讓我對這段代碼里面迷糊的有:
1:倆個參數(shù)
Text 是要加密的內(nèi)容
sKey是作為加密內(nèi)容的密鑰。當(dāng)然加密、解密時候的sKey值,是要保持一致的。
2:des對象的key值
這個key值和IV值是固定的8位長度,一定要牢記。因?yàn)樵蹅兊膮?shù)sKey是不定長度的、所以采取了一個方式就是對其進(jìn)行MD5加密、然后再截取他的前8位。這是為了在解密的時候保證key一致。不然會解密出錯。
最后,我說一下做為新人,我感覺牢記的幾個地方,或許是大大們眼中寫des必需的幾點(diǎn)~~別噴我啊、
幾個必要的對象:
DESCryptoServiceProvider 沒有它你想怎么des呢、嘿嘿
MemoryStream 存儲在內(nèi)存的流對象
CryptoStream 定義將數(shù)據(jù)流鏈接到加密轉(zhuǎn)換流。通過它寫入MemoryStream對象當(dāng)中
最后轉(zhuǎn)換成String。
相關(guān)文章
C# 漢字轉(zhuǎn)拼音(全拼和首字母)實(shí)例
這篇文章介紹了C# 漢字轉(zhuǎn)拼音(全拼和首字母)實(shí)例代碼,有需要的朋友可以參考一下2013-10-10
C#采用Winform實(shí)現(xiàn)類似Android的Listener
這篇文章主要介紹了C#采用Winform實(shí)現(xiàn)類似Android的Listener,很實(shí)用的技巧,需要的朋友可以參考下2014-08-08
基于WPF實(shí)現(xiàn)Message消息提醒控件
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)Meesage消息提醒控件,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2023-07-07

