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();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章