西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)C#.NET → C#實(shí)現(xiàn)DES加密算法與DES解密代碼

C#實(shí)現(xiàn)DES加密算法與DES解密代碼

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來源:本站整理時(shí)間:2010/11/23 9:44:25字體大小:A-A+

作者:佚名點(diǎn)擊:673次評(píng)論:0次標(biāo)簽: DES加密算法 DES

  • 類型:圖像瀏覽大。13.0M語言:英文 評(píng)分:3.6
  • 標(biāo)簽:
立即下載
DES ( data encryption Standard) 是一種世界標(biāo)準(zhǔn)的加密形式, 已15 年歷史了,雖然有些老, 可還算是比較可靠的算法。在七十的初期, 隨著計(jì)算機(jī)之間的通信發(fā)展, 需要有一種標(biāo)準(zhǔn)密碼算法為了限制不同算法的激增使他們之間不能互相對(duì)話。為解決這個(gè)問題, 美國國家安全局(N.S.A ) 進(jìn)行招標(biāo)。 I.B.M 公司研發(fā)了一種算法, 稱為:Lucifer。 經(jīng)過幾年的研討和修改, 這種算法, 成為了今天的D.E.S,1976 年11月23 日, 終于被美國國家安全局采用。 

 
///<summary>
   
/// DES加密與解密
   
///</summary>
    publicclass DESEncrypt
    {

       
#region DES加密

       
///<summary>
       
/// 使用默認(rèn)密鑰加密
       
///</summary>
       
///<param name="strText"></param>
       
///<returns></returns>
        publicstaticstring Encrypt(string strText)
        {
           
return Encrypt(strText, "TSF");
        }

       
///<summary>
       
/// 使用給定密鑰加密
       
///</summary>
       
///<param name="strText"></param>
       
///<param name="sKey">密鑰</param>
       
///<returns></returns>
        publicstaticstring Encrypt(string strText, string sKey)
        {
            DESCryptoServiceProvider des
=new DESCryptoServiceProvider();
           
byte[] inputByteArray = Encoding.Default.GetBytes(strText);
            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

       
#region DES解密

       
///<summary>
       
/// 使用默認(rèn)密鑰解密
       
///</summary>
       
///<param name="strText"></param>
       
///<returns></returns>
        publicstaticstring Decrypt(string strText)
        {
           
return Decrypt(strText, "TSF");
        }

       
///<summary>
       
/// 使用給定密鑰解密
       
///</summary>
       
///<param name="strText"></param>
       
///<param name="sKey"></param>
       
///<returns></returns>
        publicstaticstring Decrypt(string strText, string sKey)
        {
            DESCryptoServiceProvider des
=new DESCryptoServiceProvider();
           
int len = strText.Length /2;
           
byte[] inputByteArray =newbyte[len];
           
int x, i;
           
for (x =0; x < len; x++)
            {
                i
= Convert.ToInt32(strText.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
    }

D.E.S 是分塊加密的,將明文分割成 64 BITS 的塊, 然后他們一個(gè)個(gè)接起來 。他使用56位密鑰對(duì)64位的數(shù)據(jù)塊進(jìn)行加密,并對(duì)64bits的數(shù)據(jù)塊進(jìn)行16輪編碼。和每輪編碼時(shí),一個(gè)48bits的“每輪”密鑰值由56bits的完整密鑰得出來。DES用軟件進(jìn)行解碼需要用非常長(zhǎng)時(shí)間,而用硬件解碼速度非?,1977年,人們估計(jì)要耗資兩千萬美元才能建成一個(gè)專門計(jì)算機(jī)用于DES的解密,而且需要12個(gè)小時(shí)的破解才能得到結(jié)果。所以,當(dāng)時(shí)DES被認(rèn)為是一種十分強(qiáng)壯的加密方法。但今天, 只需 二十萬美圓就能制造一臺(tái)破譯DES的特別的計(jì)算機(jī),所以目前 DES 對(duì)需求“強(qiáng)壯”加密的場(chǎng)合已不再適用了。

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)