`
wyf
  • 浏览: 433470 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
社区版块
存档分类
最新评论

DES加密解密

阅读更多
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;

namespace Common
{
    /// <summary>
    /// DESEncryptor 的摘要说明。
    /// </summary>
    public class DESEncryptor
    {
        #region 私有成员
        /// <summary>
        /// 输入字符串
        /// </summary>
        private string inputString = null;
        /// <summary>
        /// 输出字符串
        /// </summary>
        private string outString = null;
        /// <summary>
        /// 输入文件路径
        /// </summary>
        private string inputFilePath = null;
        /// <summary>
        /// 输出文件路径
        /// </summary>
        private string outFilePath = null;
        /// <summary>
        /// 加密密钥
        /// </summary>
        private string encryptKey = null;
        /// <summary>
        /// 解密密钥
        /// </summary>
        private string decryptKey = null;
        /// <summary>
        /// 提示信息
        /// </summary>
        private string noteMessage = null;
        #endregion
        #region 公共属性
        /// <summary>
        /// 输入字符串
        /// </summary>
        public string InputString
        {
            get { return inputString; }
            set { inputString = value; }
        }
        /// <summary>
        /// 输出字符串
        /// </summary>
        public string OutString
        {
            get { return outString; }
            set { outString = value; }
        }
        /// <summary>
        /// 输入文件路径
        /// </summary>
        public string InputFilePath
        {
            get { return inputFilePath; }
            set { inputFilePath = value; }
        }
        /// <summary>
        /// 输出文件路径
        /// </summary>
        public string OutFilePath
        {
            get { return outFilePath; }
            set { outFilePath = value; }
        }
        /// <summary>
        /// 加密密钥
        /// </summary>
        public string EncryptKey
        {
            get { return encryptKey; }
            set { encryptKey = value; }
        }
        /// <summary>
        /// 解密密钥
        /// </summary>
        public string DecryptKey
        {
            get { return decryptKey; }
            set { decryptKey = value; }
        }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string NoteMessage
        {
            get { return noteMessage; }
            set { noteMessage = value; }
        }
        #endregion
        #region 构造函数
        public DESEncryptor()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion
        #region DES加密字符串
        /// <summary>
        /// 加密字符串
        /// 注意:密钥必须为8位
        /// </summary>
        /// <param name="strText">字符串</param>
        /// <param name="encryptKey">密钥</param>
        public void DesEncrypt()
        {
            byte[] byKey = null;
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                this.outString = Convert.ToBase64String(ms.ToArray());
            }
            catch (System.Exception error)
            {
                this.noteMessage = error.Message;
            }
        }
        #endregion
        #region DES解密字符串
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="this.inputString">加了密的字符串</param>
        /// <param name="decryptKey">密钥</param>
        public void DesDecrypt()
        {
            byte[] byKey = null;
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            byte[] inputByteArray = new Byte[this.inputString.Length];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(this.inputString);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = new System.Text.UTF8Encoding();
                this.outString = encoding.GetString(ms.ToArray());
            }
            catch (System.Exception error)
            {
                this.noteMessage = error.Message;
            }
        }
        #endregion
        #region DES加密文件
        /// <summary>
        /// DES加密文件
        /// </summary>
        /// <param name="this.inputFilePath">源文件路径</param>
        /// <param name="this.outFilePath">输出文件路径</param>
        /// <param name="encryptKey">密钥</param>
        public void FileDesEncrypt()
        {
            byte[] byKey = null;
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
                FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
                fout.SetLength(0);
                //Create variables to help with read and write.
                byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
                long rdlen = 0;              //This is the total number of bytes written.
                long totlen = fin.Length;    //This is the total length of the input file.
                int len;                     //This is the number of bytes to be written at a time.
                DES des = new DESCryptoServiceProvider();
                CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);


                //Read from the input file, then encrypt and write to the output file.
                while (rdlen < totlen)
                {
                    len = fin.Read(bin, 0, 100);
                    encStream.Write(bin, 0, len);
                    rdlen = rdlen + len;
                }

                encStream.Close();
                fout.Close();
                fin.Close();


            }
            catch (System.Exception error)
            {
                this.noteMessage = error.Message.ToString();

            }
        }
        #endregion
        #region DES解密文件
        /// <summary>
        /// 解密文件
        /// </summary>
        /// <param name="this.inputFilePath">加密了的文件路径</param>
        /// <param name="this.outFilePath">输出文件路径</param>
        /// <param name="decryptKey">密钥</param>
        public void FileDesDecrypt()
        {
            byte[] byKey = null;
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
                fout.SetLength(0);
                //Create variables to help with read and write.
                byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
                long rdlen = 0;              //This is the total number of bytes written.
                long totlen = fin.Length;    //This is the total length of the input file.
                int len;                     //This is the number of bytes to be written at a time.
                DES des = new DESCryptoServiceProvider();
                CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);


                //Read from the input file, then encrypt and write to the output file.
                while (rdlen < totlen)
                {
                    len = fin.Read(bin, 0, 100);
                    encStream.Write(bin, 0, len);
                    rdlen = rdlen + len;
                }

                encStream.Close();
                fout.Close();
                fin.Close();
            }
            catch (System.Exception error)
            {
                this.noteMessage = error.Message.ToString();
            }
        }
        #endregion
        #region MD5
        /// <summary>
        /// MD5 Encrypt
        /// </summary>
        /// <param name="strText">text</param>
        /// <returns>md5 Encrypt string</returns>
        public void MD5Encrypt()
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.inputString));
            this.outString = System.Text.Encoding.Default.GetString(result);
        }
        #endregion
    }
}

 

分享到:
评论

相关推荐

    C语言实现DES加密解密算法

    DES加密解密算法的C语言实现 DES(Data Encryption Standard)是一种对称密钥 BLOCK 加密算法,使用 56 位密钥对 64 位数据块进行加密。下面是 DES 加密解密算法的 C 语言实现的知识点总结。 字节与二进制转换 在...

    3DES加密解密工具

    标题中的“3DES加密解密工具”指的是一个用于执行三重数据加密标准(3DES,Triple DES)的软件工具,这种工具通常用于保护敏感数据的安全,确保信息在传输过程中的机密性。3DES是DES(Data Encryption Standard,...

    VB实现DES加密解密算法,vb加密和解密,VBA

    这需要创建一个.NET类库项目,实现DES加密解密功能,然后在VBA中通过CreateObject或早绑定的方式调用这些函数。 以下是VB.NET中实现DES加密解密的简单示例代码: ```vbnet Imports System.IO Imports System....

    用 MFC 实现 DES 加密解密算法

    在实现DES加密解密的过程中,首先需要理解DES的基本步骤,包括初始置换、扩展置换、8轮Feistel网络、逆扩展置换和逆初始置换。在MFC环境中,这些步骤可以通过定义类和函数来实现。例如,可以创建一个名为`CDES`的类...

    DES加密 解密 方法 MFC

    DES加密 解密 方法: DESr DESw 支持3DES加密 解密 类中利用函数重载的方式 实现两种加密方式 加密后为16进制字符串 使用方法: 实例化一个对象 然后就可以随便用了。 如 DES加密 解密 CString sd,sd2; yxyDES2 ...

    MFC实现DES加密解密实现

    在MFC环境下实现DES加密解密,你需要了解以下几个关键点: 1. **密钥扩展**:首先,56位的密钥需要经过一个称为PC-1的置换表进行转换,然后每轮使用不同的子密钥产生器(C1到C8)生成48位的子密钥。 2. **初始置换...

    DES加密解密(c++实现)

    DES加密解密(c++实现)

    DES加密解密实验报告

    DES加密解密实验旨在帮助学生理解其工作原理,并深入研究DES的弱点,特别是关于弱密钥的问题。 实验的核心是DES算法的执行流程,主要包括以下几个步骤: 1. **初始置换(IP)**:这是加密过程的第一步,用于打乱...

    des加密解密工具 .exe文件

    des加密解密工具

    DES加密解密算法 C语言源代码

    这篇C语言源代码实现了DES加密解密算法,特别适合于资源有限的环境,如STM32或51单片机。STM32系列是基于ARM Cortex-M内核的微控制器,而51单片机则是早期广泛应用的8位微处理器。在这些平台上,由于内存(RAM)资源...

    C++ 代码实现DES加密解密源代码类

    在C++中实现DES加密解密,可以创建一个类来封装相关的操作,这样有利于代码的复用和维护。 描述中提到的"单倍双倍加密算法的实现",可能是指使用DES算法进行一次或两次加密的过程。单次DES加密使用同一个密钥对数据...

    des加密解密_Des加密解密_DES加密_

    在给定的“des加密例程”中,可能包含了一个调用动态链接库(DLL)实现DES加密解密的示例代码。DLL是Windows操作系统中的一种共享库,可以被多个程序同时调用,节省内存资源并便于代码复用。这个示例可能涉及以下...

    DES加密解密VB6.0源代码

    在VB6.0(Visual Basic 6.0)环境中实现DES加密解密是编程中常见的一种需求,主要用于保护敏感数据的安全。下面将详细阐述DES加密解密原理及其在VB6.0中的实现方法。 1. **DES加密原理**: - **初始置换**:将明文...

    des加密解密工具

    在描述中提到的"DES加密解密工具"是一款专用于执行DES加密和解密操作的应用程序。这款工具的优点在于用户可以本地运行,避免了在线加解密可能带来的密钥泄露风险,确保了数据安全性。用户只需要输入相应的密钥和待...

    3DES加密解密

    标题中的“3DES加密解密”指的是在信息技术领域中,使用三重数据加密标准(3DES,Triple Data Encryption Standard)进行数据加密和解密的过程。3DES是一种加强版的DES加密算法,它通过三次应用DES的加密过程来提高...

    C# DES加密解密

    本篇文章将深入探讨C#中如何实现DES加密解密,并结合给出的链接资源进行详细解释。 1. **DES算法概述** DES是一种块加密算法,它使用56位的密钥对64位的数据块进行加密。虽然56位的密钥长度现在看来相对较短,但在...

    des加密解密源代码

    DES加密解密过程,C++实现

    VC++6.0 DES加密解密示例

    VC++6.0 DES加密解密示例工程,包含两个加密解密类库,实例化后即可使用

    java和javascript之间的DES加密解密

    Java和JavaScript之间的DES加密解密是信息安全领域中的一个重要话题,主要涉及到数据的保护和通信的安全。DES(Data Encryption Standard)是一种古老的对称加密算法,尽管它在安全性上已不被视为最佳选择,但在某些...

    DES加密解密一套JAVA&IOS

    本资源"DES加密解密一套JAVA&IOS"提供了一套跨平台的解决方案,允许JAVA和iOS应用之间进行互操作性的加密和解密操作。 在JAVA平台上,DES加密和解密通常通过Java的`javax.crypto`包来实现。这个包提供了`Cipher`类...

Global site tag (gtag.js) - Google Analytics