马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#DES16位加密解密类

[复制链接]
查看4108 | 回复0 | 2015-2-15 09:26:21 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace EallNum.Helper
{

    public class FI_DesTools
    {
        private FI_DesTools() 
        {
        } 
 
        private static string key = "×××××"; 
 
        /// <summary> 
        /// 对称加密解密的密钥 
        /// </summary> 
        public static string Key
        { 
            get 
            { 
                return key;
            } 
            set 
            { 
                key = value;
            } 
        } 
 
        /// <summary> 
        /// DES加密 
        /// </summary> 
        /// <param name="encryptString"></param> 
        /// <returns></returns> 
        public static string DesEncrypt(string strEncryptString) 
        {
            StringBuilder strRetValue = new StringBuilder();

            try
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); 
                byte[] keyIV = keyBytes;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strEncryptString); 
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            
                provider.Mode = CipherMode.ECB;//兼容其他语言的Des加密算法  
                provider.Padding = PaddingMode.Zeros;//自动补0
                          
                MemoryStream mStream = new MemoryStream(); 
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write); 
                cStream.Write(inputByteArray, 0, inputByteArray.Length); 
                cStream.FlushFinalBlock(); 

                //不使用base64编码
                //return Convert.ToBase64String(mStream.ToArray()); 

                //组织成16进制字符串            
                foreach (byte b in mStream.ToArray())
                {
                    strRetValue.AppendFormat("{0:X2}", b);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return strRetValue.ToString();
        } 
 
        /// <summary> 
        /// DES解密 
        /// </summary> 
        /// <param name="decryptString"></param> 
        /// <returns></returns>         
        public static string DesDecrypt(string strDecryptString)
        {
            string strRetValue = "";

            try
            {   
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIV = keyBytes;

                //不使用base64解码
                //byte[] inputByteArray = Convert.FromBase64String(decryptString);

                //16进制转换为byte字节
                byte[] inputByteArray = new byte[strDecryptString.Length / 2];
                for (int x = 0; x < strDecryptString.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(strDecryptString.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

                provider.Mode = CipherMode.ECB;//兼容其他语言的Des加密算法  
                provider.Padding = PaddingMode.Zeros;//自动补0  

                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                //需要去掉结尾的null字符
                //strRetValue = Encoding.UTF8.GetString(mStream.ToArray());
                strRetValue = Encoding.UTF8.GetString(mStream.ToArray()).TrimEnd('\0');
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            
            return strRetValue;
        }
    }
}
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则