• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

C#实现DES加密算法与DES解密代码

时间:2015年04月02日 15:41:04    来源:魔法猪系统重装大师官网    人气:10199
DES ( data encryption Standard) 是一种世界标准的加密形式, 已15 年历史了,虽然有些老, 可还算是比较可靠的算法。在七十的初期, 随着计算机之间的通信发展, 需要有一种标准密码算法为了限制不同算法的激增使他们之间不能互相对话。为解决这个问题, 美国国家安全局(N.S.A ) 进行招标。 I.B.M 公司研发了一种算法, 称为:Lucifer。 经过几年的研讨和修改, 这种算法, 成为了今天的D.E.S,1976 年11月23 日, 终于被美国国家安全局采用。 

 
///
   
/// DES加密与解密
   
///

    publicclass DESEncrypt
    {

       
#region DES加密

       
///
       
/// 使用默认密钥加密
       
///

       
///
       
///
        publicstaticstring Encrypt(string strText)
        {
           
return Encrypt(strText, "TSF");
        }

       
///
       
/// 使用给定密钥加密
       
///

       
///
       
///密钥
       
///
        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解密

       
///
       
/// 使用默认密钥解密
       
///

       
///
       
///
        publicstaticstring Decrypt(string strText)
        {
           
return Decrypt(strText, "TSF");
        }

       
///
       
/// 使用给定密钥解密
       
///

       
///
       
///
       
///
        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 的块, 然后他们一个个接起来 。他使用56位密钥对64位的数据块进行加密,并对64bits的数据块进行16轮编码。和每轮编码时,一个48bits的“每轮”密钥值由56bits的完整密钥得出来。DES用软件进行解码需要用非常长时间,而用硬件解码速度非常快,1977年,人们估计要耗资两千万美元才能建成一个专门计算机用于DES的解密,而且需要12个小时的破解才能得到结果。所以,当时DES被认为是一种十分强壮的加密方法。但今天, 只需 二十万美圆就能制造一台破译DES的特别的计算机,所以目前 DES 对需求“强壮”加密的场合已不再适用了。
实现,DES,加密,算法,与,解密,代码,DES,data,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部