/V5_WinLibs/Core/DESHelper1.cs
https://github.com/lsamu/V5_DataCollection · C# · 82 lines · 59 code · 7 blank · 16 comment · 1 complexity · 7ce63e3672641ab30266e5c5425a7569 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- namespace V5_WinLibs.Core {
- /// <summary>
- /// Des¶Ô³Æ¼ÓÃÜ ½âÃÜ
- /// </summary>
- public class DESHelper1 {
- #region ========¼ÓÃÜ========
- /// <summary>
- /// DES¼ÓÃÜ·½·¨
- /// </summary>
- /// <param name="strPlain">Ã÷ÎÄ</param>
- /// <param name="strDESKey">ÃÜÔ¿</param>
- /// <param name="strDESIV">ÏòÁ¿ Ö»ÄÜ8λ</param>
- /// <returns>ÃÜÎÄ</returns>
- public static string Encrypt(string source, string _DESKey) {
- StringBuilder sb = new StringBuilder();
- using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) {
- byte[] key = ASCIIEncoding.ASCII.GetBytes(_DESKey);
- byte[] iv = ASCIIEncoding.ASCII.GetBytes(_DESKey);
- byte[] dataByteArray = Encoding.UTF8.GetBytes(source);
- des.Mode = System.Security.Cryptography.CipherMode.CBC;
- des.Key = key;
- des.IV = iv;
- string encrypt = "";
- using (MemoryStream ms = new MemoryStream())
- using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) {
- cs.Write(dataByteArray, 0, dataByteArray.Length);
- cs.FlushFinalBlock();
- foreach (byte b in ms.ToArray()) {
- sb.AppendFormat("{0:X2}", b);
- }
- encrypt = sb.ToString();
- }
- return encrypt;
- }
- }
- #endregion
- #region ========½âÃÜ========
- /// <summary>
- /// ½âÃÜÊý¾Ý
- /// </summary>
- /// <param name="Text">¼ÓÃܵÄ×Ö·û´®</param>
- /// <param name="sKey">¼ÓÃÜKey Ö»ÄÜ8λ</param>
- /// <returns></returns>
- public static string Decrypt(string Text, string sKey) {
- try {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- int len;
- len = Text.Length / 2;
- byte[] inputByteArray = new byte[len];
- int x, i;
- for (x = 0; x < len; x++) {
- i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
- inputByteArray[x] = (byte)i;
- }
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- return Encoding.UTF8.GetString(ms.ToArray());
- }
- catch (Exception) {
- return string.Empty;
- }
- }
- #endregion
- }
- }