马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

用C#写DES加密算法

[复制链接]
查看5412 | 回复2 | 2013-11-30 15:51:24 | 显示全部楼层 |阅读模式
希望多多指教
[C#] 纯文本查看 复制代码
using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace desfile
{
    public partial class Form1 : Form
    {
        string fileName;
        string desFileName;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c://";
            openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog.FileName;
                textBox1.Text = fileName;
            }  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            desFileName = fileName + "E";
            FileStream fin = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(desFileName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);
            byte[] bin = new byte[1000];
            long rdlen = 0;
            //This is the total number of bytes written.

            long totlen = fin.Length;
            int len;
            DES des = new DESCryptoServiceProvider();
            byte[] desKey = ASCIIEncoding.ASCII.GetBytes("luckyprg");
            //the keys length must be morethan 8
            byte[] desIV = ASCIIEncoding.ASCII.GetBytes("luckyprg");

            CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 1000);
                encStream.Write(bin, 0, len);
                rdlen = rdlen + len;
            }
            encStream.Close();
            fout.Close();
            fin.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            desFileName = fileName + "D";
            FileStream fin = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(desFileName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);
            byte[] bin = new byte[1000];
            long rdlen = 0;
            //This is the total number of bytes written.

            long totlen = fin.Length;
            int len;
            DES des = new DESCryptoServiceProvider();
            byte[] desKey = ASCIIEncoding.ASCII.GetBytes("luckyprg");
            //the keys length must be morethan 8
            byte[] desIV = ASCIIEncoding.ASCII.GetBytes("luckyprg");

            CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 1000);
                encStream.Write(bin, 0, len);
                rdlen = rdlen + len;
            }
            encStream.Close();
            fout.Close();
            fin.Close();
        }
    }
}

ibcadmin | 2013-11-30 18:17:59 | 显示全部楼层
不错 好样的 加精华
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
Sam-lin | 2013-12-6 08:42:10 | 显示全部楼层
ibcadmin 发表于 2013-11-30 18:17
不错 好样的 加精华

多多指教
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则