Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1864525
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: C/C++

2007-08-23 13:12:34

开发环境: C# .NET 2005

//第一次写C#程序,发现这个M$的环境还是比较好上手和使用的...
//参考了别人的模块

主要代码如下:
Form1.cs //主窗口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBoxKey.Text = "Hello, World!";
        }

        private void buttonClearCT_Click(object sender, EventArgs e)
        {
            richTextBoxClearText.Text = "";
        }

        private void buttonClearCryptograph_Click(object sender, EventArgs e)
        {
            richTextBoxCryptograph.Text = "";
        }

        private void buttonOpenClearText_Click(object sender, EventArgs e)
        {
            if (openFileDialogClearText.ShowDialog() == DialogResult.OK)
            {
                richTextBoxClearText.LoadFile(openFileDialogClearText.FileName, RichTextBoxStreamType.PlainText);
            }
            else
            {
                MessageBox.Show("Error in file open!");
                return;
            }
        }

        private void buttonEncrypt_Click(object sender, EventArgs e)
        {
            String strDESKey;
            String strClearText;

            if (richTextBoxClearText.Text.Length == 0)
            {
                MessageBox.Show("Please input Clear Text!");
                return;
            }

            //Get Key

            if (textBoxKey.Text.Length == 0)
            {
                MessageBox.Show("Use the default key:\"Hello, World!\"");
                strDESKey = "Hello, World!";
            }
            else
            {
                strDESKey = textBoxKey.Text;
            }

            //Get Clear Text

            strClearText = richTextBoxClearText.Text;

            EncryptString EncryptNow = new EncryptString();
            String CryptographText = EncryptNow.Encrypt(strDESKey, strClearText);
            if (CryptographText.Equals(null))
            {
                MessageBox.Show("Error in Encrypt!");
                return;
            }
            else
            {
                richTextBoxCryptograph.Text = CryptographText;
            }
        }

        private void buttonDecrypt_Click(object sender, EventArgs e)
        {
            String strCryptographText;
            String strDESKey;

            if (richTextBoxCryptograph.Text.Length == 0)
            {
                MessageBox.Show("Please input Cryptograph!");
                return;
            }

            if (textBoxKey.Text.Length == 0)
            {
                MessageBox.Show("Use the default key:\"Hello, World!\"");
                strDESKey = "Hello, World!";
            }
            else
            {
                strDESKey = textBoxKey.Text;
            }

            strCryptographText = richTextBoxCryptograph.Text;

            DecryptDES DecryptNow = new DecryptDES();
            String ClearText = DecryptNow.Decrypt(strDESKey, strCryptographText);
            if (ClearText.Equals(string.Empty))
            {
                MessageBox.Show("Error in Decrypt!");
                return;
            }
            else
            {
                richTextBoxClearText.Text = ClearText;
            }
        }

        private void buttonOpenCryptograph_Click(object sender, EventArgs e)
        {
            if (openFileDialogCrytograph.ShowDialog() == DialogResult.OK)
            {
                richTextBoxCryptograph.LoadFile(openFileDialogCrytograph.FileName, RichTextBoxStreamType.PlainText);
            }
            else
            {
                MessageBox.Show("Error in file open!");
                return;
            }
        }

        private void buttonSaveClearText_Click(object sender, EventArgs e)
        {
            if (saveFileDialogClearText.ShowDialog() == DialogResult.OK)
            {
                richTextBoxClearText.SaveFile(saveFileDialogClearText.FileName, RichTextBoxStreamType.PlainText);
            }
            else
            {
                MessageBox.Show("Error in file saving!");
                return;
            }
        }

        private void buttonSaveCrypt_Click(object sender, EventArgs e)
        {
            if (saveFileDialogCryptograph.ShowDialog() == DialogResult.OK)
            {
                richTextBoxCryptograph.SaveFile(saveFileDialogCryptograph.FileName, RichTextBoxStreamType.PlainText);
            }
            else
            {
                MessageBox.Show("Error in file saving!");
                return;
            }
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}



EncryptString.cs //加密
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace WindowsApplication1
{
    class EncryptString
    {
        private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        public String Encrypt(String Key, String ClearText)
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
            byte[] rgbIV = IV;
            byte[] clearTextArray = Encoding.UTF8.GetBytes(ClearText);
            try
            {
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(clearTextArray, 0, clearTextArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}


DecryptDES.cs //解密
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace WindowsApplication1
{
    class DecryptDES
    {
        private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        public String Decrypt(String Key, String DecryptText)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
                byte[] rgbIV = IV;
                byte[] decryptArray = Convert.FromBase64String(DecryptText);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(decryptArray, 0, decryptArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}


BTW:不知道如何控制publish的信息...所以不上传最终打包结果了,做为练习程序,也没必要地说...





阅读(4659) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

icymoon2009-05-17 17:53:13

我记得支持啊,N年前的事情了。。。

icymoon2009-05-17 17:53:13

我记得支持啊,N年前的事情了。。。

chinaunix网友2009-05-15 15:49:49

不支持中文

chinaunix网友2009-05-15 15:49:49

不支持中文