DES加密解密工具

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace DESPwd
{
    public class DESUtil
    {
        static DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        public static DESCryptoServiceProvider DES
        {
            get { return des; }
        }
        const string EncryptionKey = "诺丽科技";
        const string EncryptionIV = "kell";
        public static string Encoder(string input)
        {
            byte[] SourceData = Encoding.Unicode.GetBytes(input);
            byte[] returnData = null;
            try
            {
                des.Key = ASCIIEncoding.Unicode.GetBytes(EncryptionKey);
                des.IV = ASCIIEncoding.Unicode.GetBytes(EncryptionIV);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(SourceData, 0, SourceData.Length);
                cs.FlushFinalBlock();
                returnData = ms.ToArray();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Encoding.Unicode.GetString(returnData);
        }
        public static string Decoder(string input)
        {
            byte[] SourceData = Encoding.Unicode.GetBytes(input);
            byte[] returnData = null;
            try
            {
                DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
                desProvider.Key = Encoding.Unicode.GetBytes(EncryptionKey);
                desProvider.IV = Encoding.Unicode.GetBytes(EncryptionIV);
                MemoryStream ms = new MemoryStream();
                ICryptoTransform encrypto = desProvider.CreateDecryptor();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
                cs.Write(SourceData, 0, SourceData.Length);
                cs.FlushFinalBlock();
                returnData = ms.ToArray();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Encoding.Unicode.GetString(returnData);
        }
    }
}
using System;
using System.Windows.Forms;

namespace DESPwd
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox9.Text = DESUtil.Encoder(textBox8.Text);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox11.Text = DESUtil.Decoder(textBox9.Text);
        }
    }
}
原文地址:https://www.cnblogs.com/Jeely/p/11720798.html