DES和MD5加密与解密

转载 2007年09月29日 17:56:00

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Windows.Forms;
/// <summary>
/// DesEncrypt 的摘要说明
/// </summary>
public class DesMd5
{
    public DesMd5()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    /*
//加密字符串,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了)
public  string DesEncrypt(string strText, string strEncrKey)

//解密字符串,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了)

public string  DesDecrypt(string strText,string sDecrKey)

//加密数据文件,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了)

public void DesEncryptFile(string m_InFilePath,string m_OutFilePath,string strEncrKey)

//解密数据文件,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了)

public void DesDecryptFile(string m_InFilePath,string m_OutFilePath,string sDecrKey)

//MD5加密

public string MD5Encrypt(string strText)

*/

 


/******************************************************************************************
 *                    Cryptography class is for Cryptography
                      author:Jim
        e_mail:tjq_tang@hotmail.com                                
 *        thanks:
 *        URI:
 *
 *
 *****************************************************************************************/


    /// <summary>
    /// Encrypt the string
    /// Attention:key must be 8 bits
    /// </summary>
    /// <param name="strText">string</param>
    /// <param name="strEncrKey">key</param>
    /// <returns></returns>
    public string DesEncrypt(string strText, string strEncrKey)
    {
        byte[] byKey = null;
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());


        }
        catch (System.Exception error)
        {
           // MessageBox.Show(error.Message);
            return "error:" + error.Message + "/r";
        }
    }
    /// <summary>
    /// Decrypt string
    /// Attention:key must be 8 bits
    /// </summary>
    /// <param name="strText">Decrypt string</param>
    /// <param name="sDecrKey">key</param>
    /// <returns>output string</returns>
    public string DesDecrypt(string strText, string sDecrKey)
    {
        byte[] byKey = null;
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        byte[] inputByteArray = new Byte[strText.Length];
        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(strText);
            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();
            return encoding.GetString(ms.ToArray());
        }
        catch (System.Exception error)
        {
           // MessageBox.Show(error.Message);
            return "error:" + error.Message + "/r";
        }
    }
    /// <summary>
    /// Encrypt files
    /// Attention:key must be 8 bits
    /// </summary>
    /// <param name="m_InFilePath">Encrypt file path</param>
    /// <param name="m_OutFilePath">output file</param>
    /// <param name="strEncrKey">key</param>
    public void DesEncryptFile(string m_InFilePath, string m_OutFilePath, string strEncrKey)
    {
        byte[] byKey = null;
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
            FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(m_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)
        {
            MessageBox.Show(error.Message, "错误信息");
           // MessageBox.Show(error.Message.ToString());

        }
    }
    /// <summary>
    /// Decrypt files
    /// Attention:key must be 8 bits
    /// </summary>
    /// <param name="m_InFilePath">Decrypt filepath</param>
    /// <param name="m_OutFilePath">output filepath</param>
    /// <param name="sDecrKey">key</param>
    public void DesDecryptFile(string m_InFilePath, string m_OutFilePath, string sDecrKey)
    {
        byte[] byKey = null;
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
            FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(m_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)
        {
            MessageBox.Show(error.Message, "错误信息");
            //MessageBox.Show("error:" + error.Message);
        }
    }
    /*
    /// <summary>
    /// MD5 Encrypt
    /// </summary>
    /// <param name="strText">text</param>
    /// <returns>md5 Encrypt string</returns>
    public string MD5Encrypt(string strText)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
        return System.Text.Encoding.Default.GetString(result);
    }
     */
    ///MD5加密
    public string MD5Encrypt(string pToEncrypt, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new 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);
        }
        ret.ToString();
        return ret.ToString();


    }

    ///MD5解密
    public string MD5Decrypt(string pToDecrypt, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        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 = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        StringBuilder ret = new StringBuilder();

        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }


    //哈希加密
    public string HashEncrypt(string plain)
    {
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(plain);
        System.Security.Cryptography.SHA1CryptoServiceProvider sha =
            new System.Security.Cryptography.SHA1CryptoServiceProvider();
        byte[] hash = sha.ComputeHash(buffer);
        System.Text.StringBuilder passwordBuilder = new System.Text.StringBuilder(32);
        foreach (byte hashByte in hash)
        {
            passwordBuilder.Append(hashByte.ToString("x2"));
        }

        return passwordBuilder.ToString();
    }
 

}

Oracle中定义DES加密解密及MD5加密函数

Oracle中定义DES加密解密及MD5加密函数 (1)DES加密函数 ? 1 2 3 4 5 6 7 ...
  • qq_20327293
  • qq_20327293
  • 2015年11月13日 16:50
  • 625

C#使用MD5加密,DES加密解密的一个类

没什么好说的,直接上类。 using System; using System.IO; using System.Security.Cryptography; using System.Text; ...
  • xianglikai1
  • xianglikai1
  • 2017年02月05日 15:58
  • 1473

Base64、MD5、DES、AES、RAS加密解密编码解码分析

概念: 编码 解码  防止中文乱码 加密 解密  安全性  将明文---密文(看不懂) Base64   概念:Base64编码可用于在HTTP环境下传递较长的标识信息 ...
  • qq_35681180
  • qq_35681180
  • 2016年11月30日 13:41
  • 757

加密算法—MD5、RSA、DES

最近因为要做一个加密的功能,简单了解了一下加密算法,现在比较常用的有三个加密算法MD5加密算法、RSA加密算法、DES加密算法。     MD5加密算法     定义:MD5算法是将任意长度的“字...
  • zt15732621796
  • zt15732621796
  • 2016年05月08日 18:20
  • 1307

Android加密和解密(DES,AES,MD5)

加密是通过加密算法和加密密钥将明文转变为密文的过程,解密是其逆过程。加密算法有很多种,一般可以分为对称加密(如DES、AES等)、非对称加密(如RSA等)和单向加密(如MD5等)3类算法。 1)...
  • gongzhiyao3739124
  • gongzhiyao3739124
  • 2016年10月13日 21:53
  • 4478

python中常用的base64 md5 aes des crc32等的加密解密

python中常用的base64 md5 aes des crc32等的加密解密转自:http://www.cnblogs.com/darkpig/p/5676076.html1.base64Pyth...
  • suzyu12345
  • suzyu12345
  • 2017年01月20日 15:37
  • 2396

VB.NET 的 DES 加密与解密(转载于nofort博客)

MD5和SHA的加密方式都是单向的,就算是我写的程序用户的密码用这两种方式保存后,我对着看也不知道他们的密码是什么,这用在保存密码上是不错的,但如果是一些需要能将密文还原的应用来说就不合适了,所以接下...
  • lpwmm
  • lpwmm
  • 2016年03月06日 23:19
  • 2223

关于用户密码的简单加密(DES+MD5)与复杂加密(自己想的)

用户信息的加密方式,
  • breaknull
  • breaknull
  • 2015年09月07日 14:27
  • 2286

使用MD5对用户密码加密与解密

MD5简介:           MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2...
  • e295166319
  • e295166319
  • 2016年12月12日 17:29
  • 2792

Java实现MD5加密与解密

我用到的md5是用来做登录注册页面输入密码后,存储加密后的密码到数据库保密用的。觉得简单好用所以放到上面来供大家以前学习使用,这个并不需要导入外部的包喔。 import java.securit...
  • wwz95
  • wwz95
  • 2017年11月13日 21:28
  • 233
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:DES和MD5加密与解密
举报原因:
原因补充:

(最多只允许输入30个字)