wtuqi
2/23/2020 - 5:29 AM

DES加密解密

/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="pToEncrypt">待加密的字符串</param>
/// <param name="sKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string DesEncrypt(string pToEncrypt, string sKey)
{
    StringBuilder ret = new StringBuilder();
    try
    {
        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();
        foreach (byte b in ms.ToArray())
        {
            ret.AppendFormat("{0:X2}", b);
        }
        return ret.ToString();
    }
    catch
    {
        return pToEncrypt;
    }
}

/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="pToDecrypt">待解密的字符串</param>
/// <param name="sKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DesDecrypt(string pToDecrypt, string sKey)
{
    MemoryStream ms = new MemoryStream();
    try
    {
        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);
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }
    catch
    {
        return pToDecrypt;
    }
}
//DES加密:使用一个 56 位的密钥以及附加的8位奇偶校验位,产生最大64位的分组大小。这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半。
//使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。DES使用16个循环,使用异或,置换,代换,移位操作四种基本运算。
byte[] buffer;
//先定义一个全局的字节数组和实例化一个全局的DESCryptoServiceProvider对象
DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider();

//DES加密:
private string DEncrypton(String express)
{
MemoryStream ms = new MemoryStream();//先创建 一个内存流
CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateEncryptor(), CryptoStreamMode.Write);//将内存流连接到加密转换流
StreamWriter sw = new StreamWriter(cryStream);
sw.WriteLine(express);//将要加密的字符串写入加密转换流
sw.Close();
cryStream.Close();
buffer = ms.ToArray();//将加密后的流转换为字节数组
return Convert.ToBase64String(buffer);//将加密后的字节数组转换为字符串
}
//DES解密:
private string Descrypt(string ciphertext)
{
MemoryStream ms = new MemoryStream(buffer);//将加密后的字节数据加入内存流中
CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateDecryptor(), CryptoStreamMode.Read);//内存流连接到解密流中
StreamReader sr = new StreamReader(cryStream);
ciphertext = sr.ReadLine();//将解密流读取为字符串
sr.Close();
cryStream.Close();
ms.Close();
return ciphertext;
}
//不限制解密机器[通用]
public string Decrypt(string data, string Key) //解密
{
    try
     {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        byte[] byEnc;
        try
        {
            byEnc = Convert.FromBase64String(data);
        }
        catch
        {
            return null;
        }
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream(byEnc);
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cst);
        return sr.ReadToEnd();
    }
    catch
    {
        return "加密锁已失效";
    }
}

/// 加密数据
public string Encryption(string data, string Key) //加密
{
    try
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        int i = cryptoProvider.KeySize;
        MemoryStream ms = new MemoryStream();
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
        StreamWriter sw = new StreamWriter(cst);
        sw.Write(data);
        sw.Flush();
        cst.FlushFinalBlock();
        sw.Flush();
        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    }       
    catch
    {
        return "不能识别的加密锁";
    }
}