C#写Des加密解密算法

源码地址 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();
        }
    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值