现在的位置: 首页 > 综合 > 正文

用c#编写一个高级文件加密工具

2013年07月07日 ⁄ 综合 ⁄ 共 4228字 ⁄ 字号 评论关闭

 

图:

呵呵,下面是源代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography; //need for 加密
using System.IO;
namespace Encrypt
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }
        
private string Crypto_file_name="";      //加密文件名
        private string Decrypto_file_name = "";  //解密文件名
        private string Save_file_name="";       //加密后得文件名
        
        
private void button3_Click(object sender, EventArgs e)
        {
            
if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                Crypto_file_name 
= openFileDialog1.FileName; 
                textBox1.Text 
= Crypto_file_name;
            }
        }
        
private void button2_Click(object sender, EventArgs e)
        {
            
if (Crypto_file_name != "" && Save_file_name != "")
            {
                
//使用Rijndael加密
                RijndaelManaged rm;
                rm 
= new RijndaelManaged();
                
//私钥和向量,很简单得.只要改变私钥,呵呵,加密得得数据就变了.没有私钥,很难解开.
                byte[] Key = { 0x010x020x030x040x050x060x070x080x090x100x110x120x130x140x150x16 };
                
byte[] IV = { 0x010x020x030x040x050x060x070x080x090x100x110x120x130x140x150x16 };
                FileStream src 
= new FileStream(Crypto_file_name, FileMode.Open);
                ICryptoTransform encoder 
= rm.CreateEncryptor(Key, IV); //定义基本的加密转换运算。
                CryptoStream str = new CryptoStream(src, encoder, CryptoStreamMode.Read);
                FileStream outFile 
= new FileStream(Save_file_name, FileMode.Create);
                
int i = 0;
                
while ((i = str.ReadByte()) != -1//转换为 int 的字节,或者如果从流的末尾读取则为 -1。 
                {
                    outFile.WriteByte((
byte)i);
                }
                src.Close();
                outFile.Close();
                label7.Text 
= "加密成功!";
            }
        }
        
private void button4_Click(object sender, EventArgs e)
        {
            
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Save_file_name 
= saveFileDialog1.FileName;
                textBox2.Text 
= Save_file_name;
            }
        }
        
private void button5_Click(object sender, EventArgs e)
        {
            
if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Decrypto_file_name 
= openFileDialog1.FileName;
                textBox4.Text 
= Decrypto_file_name;
            }
        }
        
private void button6_Click(object sender, EventArgs e)
        {
            
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Save_file_name 
= saveFileDialog1.FileName;
                textBox3.Text 
= Save_file_name;
            }
        }
        
private void button1_Click(object sender, EventArgs e)
        {
            
//使用Rijndael加密
            RijndaelManaged rm;
            rm 
= new RijndaelManaged();
            FileStream src 
= new FileStream(Decrypto_file_name, FileMode.Open);
            
byte[] Key = { 0x010x020x030x040x050x060x070x080x090x100x110x120x130x140x150x16 };
            
byte[] IV = { 0x010x020x030x040x050x060x070x080x090x100x110x120x130x140x150x16 };
            ICryptoTransform decoder 
= rm.CreateDecryptor(Key, IV);
            CryptoStream str 
= new CryptoStream(src, decoder, CryptoStreamMode.Read);
            FileStream outFile 
= new FileStream(Save_file_name, FileMode.Create);
            
int i = 0;
            
while ((i = str.ReadByte()) != -1)
            {
                outFile.WriteByte((
byte)i);
            }
            src.Close();
            outFile.Close();
            label5.Text 
= "解密成功!";
        }
        
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(
"http://greenery.blog.edu.cn");
        }
        
private void button7_Click(object sender, EventArgs e)
        {
            Close();
        }
        
private void button8_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

 

抱歉!评论已关闭.