MD5Helper辅助类

摘要:
DES加密和解密publicclassMD5Helper{//DES加密///sKeypublicstringMD5Encryption(stringpToEncrypion,stringsKey){DESCryptoServiceProviders=newDESCrypt服务提供商();byte〔〕inputByteArray=Encoding。默认值。GetBytes

DES加密和解密

    public classMD5Helper
    {
        ///DES加密    
        ///sKey 
        public  string MD5Encrypt(string pToEncrypt, stringsKey)
        {
            DESCryptoServiceProvider des = newDESCryptoServiceProvider();
            byte[] inputByteArray =Encoding.Default.GetBytes(pToEncrypt);
            des.Key =ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV =ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = newMemoryStream();
            CryptoStream cs = newCryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = newStringBuilder();
            foreach (byte b inms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            returnret.ToString();
        }
        ///DES解密    
        public  string MD5Decrypt(string pToDecrypt, stringsKey)
        {
            DESCryptoServiceProvider des = newDESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            des.Key =ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV =ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = newMemoryStream();
            CryptoStream cs = newCryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = newStringBuilder();
            returnSystem.Text.Encoding.Default.GetString(ms.ToArray());
        }
        public  string MD5Encrypt(string pToEncrypt, string sKey, out stringmsg)
        {
            DESCryptoServiceProvider des = newDESCryptoServiceProvider();
            StringBuilder ret = newStringBuilder();
            try
            {
                byte[] inputByteArray =Encoding.Default.GetBytes(pToEncrypt);
                msg = "SUCC:";
                int length =sKey.Length;
                if (length < 8)
                { msg = "ERR:密钥长度不能小于8"; return ""; }
                int _dif = length - 8;
                int _sub_Index = _dif / 2 + _dif % 2;
                _sub_Index = _sub_Index <= 0 ? 0: _sub_Index;
                sKey = sKey.Substring(_sub_Index, 8);
                des.Key =ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV =ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = newMemoryStream();
                CryptoStream cs = newCryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                foreach (byte b inms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
            }
            catch(Exception ex)
            {
                msg = "ERR:" +ex.Message;
            }
            returnret.ToString();
        }
        ///DES解密    
        public  string MD5Decrypt(string pToDecrypt, string sKey, out stringmsg)
        {
            DESCryptoServiceProvider des = newDESCryptoServiceProvider();
            string _value = "";
            try
            {
                msg = "SUCC:";
                int length =sKey.Length;
                if (length < 8)
                { msg = "ERR:密钥长度不能小于8"; return ""; }
                int _dif = length - 8;
                int _sub_Index = _dif / 2 + _dif % 2;
                _sub_Index = _sub_Index <= 0 ? 0: _sub_Index;
                sKey = sKey.Substring(_sub_Index, 8);
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                des.Key =ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV =ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = newMemoryStream();
                CryptoStream cs = newCryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = newStringBuilder();
                _value =Encoding.Default.GetString(ms.ToArray());
            }
            catch(Exception ex)
            {
                msg = "ERR:" +ex.Message;
            }
            return_value;
        }
    }

调用方法

 MD5Helper md5 = newMD5Helper();
 string key = ConfigurationManager.AppSettings["MD5Encrypt"];
 loginPassword = md5.MD5Encrypt(loginPassword, key);

免责声明:文章转载自《MD5Helper辅助类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQLite安装 以及 SQLite header and source version mismatch错误解决 (In debian)esxi 主机用户功能说明下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MySQL通过自定义函数以及存储过程实现递归查询父级ID

1.存储过程定义  存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后调用不需要再次编译 2.为什么用存储过程实现树结构数据查询父级节点  1、能完成较复杂的运算与判断    2、可编程行强,灵活    3、SQL编程的代码可重复使用    4、预编译机制    5、减少...

cJSON 使用详解

由于c语言中,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json。如果有对应的数据结构就方便一些, 如python中用json.loads(json)就把json字符串转变为内建的数据结构处理起来比较方便。     cjson库文件下载:     sourceforge地址     一个重要概念:         在cjson中,js...

动态修改log4j.properties配置文件,无需重启就可以生效

1.log4j.properties自带有一个PropertyConfigurator的类,通过方法configureAndWatch实现 代码实现 import lombok.extern.slf4j.Slf4j; import org.apache.log4j.PropertyConfigurator; import java.io.FileInpu...

AutoMapper之ABP项目中的使用介绍

最近在研究ABP项目,昨天写了Castle Windsor常用介绍以及其在ABP项目的应用介绍 欢迎各位拍砖,有关ABP的介绍请看阳光铭睿 博客 AutoMapper只要用来数据转换,在园里已经有很多这方面文章了,本文主要介绍其在实际项目常用总结,以及在ABP项目中的应用介绍。AutoMapper应用非常简单,大家稍微看下文档就可以上手,但是性能不好啊,所...

消息队列之 ActiveMQ

简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效、可扩展、稳定、安全的企业级消息通信。 它的设计目标是提供标准的、面向消息的、多语言的应用集成消息通信中间件。ActiveMQ 实现了 JMS 1.1 并提供了很多附加的特性,比如 JMX 管理、主从管理、消息组通信、消息优先级、延迟接收...

Java数据解析之JSON(非原创)

文章大纲 一、JSON介绍二、常见框架介绍与实战三、Studio中GsonFormat插件使用四、项目源码下载(含参考资料)五、参考文档 一、JSON介绍 1. 简介   JSON 的全称是 JavaScript Object Notation,是一种轻量级的数据交换格 式。 2. 特点 (1)JSON 比 XML 数据传输的有效性要高出很多(2)JSO...