C# DES加密解密_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# DES加密解密

C# DES加密解密

 2017/11/21 17:04:33  杜子烟  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Security.Cryptography;usingSystem.Text;namespaceConsoleApp1{publicclassEncryptUtil{///<summary>///MD5加密码字符串///</summary>///<
  • 标签:C# 加密解密
class="brush:csharp;gutter:true;">using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp1
{
    public class EncryptUtil
    {
        /// <summary>
        /// MD5加密码字符串
        /// </summary>
        /// <param name="sInputString">消息体</param>
        /// <returns>MD5签名字符</returns>
        public static string MD5Encrypt(string sInputString)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            string encoded = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(sInputString))).Replace("-", "");
            return encoded;
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="sInputString">加密后的消息体</param>
        /// <param name="sKey">24位的密钥</param>
        /// <returns>解密后的消息体</returns>
        public static string DESEncryptBase64(string sInputString, string sKey)
        {
            if (string.IsNullOrEmpty(sInputString) || string.IsNullOrEmpty(sKey) || sKey.Length != 24)
            {
                return string.Empty;
            }

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;
            string key = sKey.Substring(0, 12);
            string iv = sKey.Remove(0, 12);
            byte[] byKey = Convert.FromBase64String(key);
            byte[] byIV = Convert.FromBase64String(iv);
            byte[] inputByteArray = Encoding.UTF8.GetBytes(sInputString);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            return Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="sInputString">待加密消息体</param>
        /// <param name="sKey">24位的密钥</param>
        /// <returns>加密后的消息体</returns>
        public static string DESDecryptBase64(string sInputString, string sKey)
        {
            if (string.IsNullOrEmpty(sInputString) || string.IsNullOrEmpty(sKey) || sKey.Length != 24)
            {
                return string.Empty;
            }

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;

            string key = sKey.Substring(0, 12);
            string iv = sKey.Remove(0, 12);
            byte[] byKey = Convert.FromBase64String(key);
            byte[] byIV = Convert.FromBase64String(iv);
            byte[] inputByteArray = Convert.FromBase64String(sInputString);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            Encoding encoding = new UTF8Encoding();
            return encoding.GetString(ms.ToArray());
        }

    }

}

  

发表评论
用户名: 匿名