| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #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==";
- /// <summary>
- /// 加密报文数据
- /// </summary>
- /// <param name="content">待加密的报文</param>
- /// <returns>密文</returns>
- 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());
- }
- }
- }
- /// <summary>
- /// 解密报文数据
- /// </summary>
- /// <param name="ciphertext">待解密的报文</param>
- /// <returns>明文</returns>
- 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;
- }
- }
- }
|