#region << 版 本 注 释 >>
/**
* 文件名: DESDZFP
*【功 能】
*
*
* 版本 变更时间 部门 作者 变更内容
* ───────────────────────────────────────────────────────────────────────────────────
* V1.0.0 $DATE$ 陈嘉俊 初版
*
*
* Copyright(c)$YEAR$ Zhejiang Aisino Co.,Ltd. All Rights Reserved.
* LICENSE INFORMATION
*/
#endregion
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
namespace SiteCore.nuonuo
{
[Guid("88433774-746B-4B6D-B043-471EBEC31718")]
public interface IDESDZFP
{
string Encrypt(string content);
string Decrypt(string ciphertext);
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("2AA75AD0-92E8-421D-9B49-C192BF13A606")]
public class DESDZFP
{
private const string Password = "LmMGStGtOpF4xNyvYt54EQ==";
///
/// 加密报文数据
///
/// 待加密的报文
/// 密文
public static string Encrypt(string content)
{
if (string.IsNullOrEmpty(content))
{
throw new Exception("明文为空!");
}
byte[] BKey = new byte[8];
byte[] BIV = new byte[8];
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
byte[] bytes = Convert.FromBase64String(Password);
Buffer.BlockCopy(bytes, 0, BKey, 0, 8);
Buffer.BlockCopy(bytes, 8, BIV, 0, 8);
byte[] encrypt = Encoding.UTF8.GetBytes(content);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5Hash = md5.ComputeHash(encrypt, 0, encrypt.Length);
byte[] totalByte = CombineBytes(md5Hash, encrypt);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(BKey, BIV), CryptoStreamMode.Write))
{
cs.Write(totalByte, 0, totalByte.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
}
}
///
/// 解密报文数据
///
/// 待解密的报文
/// 明文
public static string Decrypt(string ciphertext)
{
if (string.IsNullOrEmpty(ciphertext))
{
throw new Exception("密文为空!");
}
ciphertext = ciphertext.Replace(" ", "+");
byte[] BKey = new byte[8];
byte[] BIV = new byte[8];
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
byte[] bytes = Convert.FromBase64String(Password);
Buffer.BlockCopy(bytes, 0, BKey, 0, 8);
Buffer.BlockCopy(bytes, 8, BIV, 0, 8);
byte[] totalByte = null;
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(ciphertext);
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(BKey, BIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
totalByte = ms.ToArray();
}
}
if (totalByte.Length <= 16)
{
throw new Exception("密文格式错误!");
}
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5Hash = md5.ComputeHash(totalByte, 16, totalByte.Length - 16);
for (int i = 0; i < md5Hash.Length; i++)
{
if (md5Hash[i] != totalByte[i])
throw new Exception("Md5校验失败!");
}
return Encoding.UTF8.GetString(totalByte, 16, totalByte.Length - 16);
}
private static byte[] CombineBytes(byte[] bytes1, byte[] bytes2)
{
int len = bytes1.Length + bytes2.Length;
byte[] lenArr = new byte[len];
bytes1.CopyTo(lenArr, 0);
bytes2.CopyTo(lenArr, bytes1.Length);
return lenArr;
}
}
}