C#写Des加密解密算法

马上就要实习了,求大大们介绍工作。QQ:1028962069

源码地址 http://download.csdn.net/detail/h1028962069/8618367
我只写关键代码。
界面如下
这里写图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace DES加密解密
{
    class desende
    {


        public string Encrypt(string PlainText, string KEY_64, string IV_64)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;
            MemoryStream ms = new MemoryStream();
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cst);
            sw.Write(PlainText);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }



        public string Decrypt(string CypherText, string KEY_64, string IV_64)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(CypherText);
            }
            catch
            {
                return null;
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(byEnc);
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
        }




    }
}

调用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;


namespace DES加密解密
{
    public partial class Form1 : Form
    {

        public Form1()
        {

            InitializeComponent();
        }
        desende ed = new desende();

      //必须8个字符(64Bit)

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                string KEY_64 = textBox2.Text; //必须是8个字符(64Bit)
                string IV_64 = textBox4.Text;//变量
                string str_plain_text = textBox1.Text;


                string str_cypher_text =
                    ed.Encrypt(str_plain_text, KEY_64, IV_64);
                textBox3.Text = str_cypher_text;
            }
            else
                MessageBox.Show("请先输入八位key和Iv"); 

        }

        private void button2_Click(object sender, EventArgs e)
        {

            string str_plain_text = textBox1.Text;
            string KEY_64 = textBox2.Text; //必须是8个字符(64Bit)
            string IV_64 = textBox4.Text;//变量
            if (textBox1.Text != "")
            MessageBox.Show("请先清空明文字符串看加密变化");
            else
            textBox1.Text= ed.Decrypt( textBox3.Text , KEY_64, IV_64);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/h1028962069/article/details/45180409
今日推荐