作者: 乔克斯
查看: 2543|回复: 3

主题标签Tag

[教程] 【DES加解密类】DES加密/解密类DESEncrypt.cs

[复制链接]
乔克斯 发表于 2014-8-13 16:37:22 | 显示全部楼层 |阅读模式
查看: 2543|回复: 3
DES加密/解密类DESEncrypt.cs
   一个DES加密解密类,有密钥加密和无密钥加密两种方法,无密钥加密实际上使用的是一个默认密钥:
[C#] 纯文本查看 复制代码
using System;
using System.Security.Cryptography;  
using System.Text;
namespace HelloCsharp.Utilities
{
        /// <summary>
        /// DES加密/解密类。
        /// </summary>
        public class DESEncrypt
        {
                public DESEncrypt()
                {                       
                }

                #region ========加密======== 
 
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
                public static string Encrypt(string Text) 
                {
                        return Encrypt(Text,"MATICSOFT");
                }
                /// <summary> 
                /// 加密数据 
                /// </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
                
                #region ========解密======== 
   
 
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
                public static string Decrypt(string Text) 
                {
                        return Decrypt(Text,"MATICSOFT");
                }
                /// <summary> 
                /// 解密数据 
                /// </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 


        }
}

回复 论坛版权

使用道具 举报

demo110 发表于 2016-2-20 15:29:08 | 显示全部楼层
恩 收藏了哦
yeyuxx 发表于 2016-2-27 11:03:28 | 显示全部楼层
非常好啊
回复

使用道具 举报

yeyuxx 发表于 2016-2-27 11:04:04 | 显示全部楼层
非常好啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-3-28 23:31, Processed in 0.618813 second(s), 33 queries , Gzip On.

快速回复 返回顶部 返回列表