程序源代码:工程 下载:https://k-n.lanzoux.com/i5dRjiu7zrc
[C#] 纯文本查看 复制代码 using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Encryptionanddecryption
{
public partial class Form1 : Form
{
string publicKey = "WvJ2hkXoDhFRAkBB"; //密钥随便改为相同长度的
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bool isA= FileHelper.IsRunAsAdmin();
if (isA==false)
{
MessageBox.Show("请以管理员身份运行!");
Application.ExitThread();
System.Environment.Exit(System.Environment.ExitCode);
System.Threading.Thread.CurrentThread.Abort();
Process.GetCurrentProcess().Kill();
return ;
}
label1.Text = FileHelper.GetNetworkAdpaterID(true);
if (label1.Text == "机器码")
{
Application.ExitThread();
System.Environment.Exit(System.Environment.ExitCode);
System.Threading.Thread.CurrentThread.Abort();
Process.GetCurrentProcess().Kill();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(@"D:\key.kps"))
{
File.Delete(@"D:\key.kps");//判断 密钥文件是否存在,存在就把他删了
}
byte[] key = FileHelper.GetKey(FileHelper.GetNetworkAdpaterID(false));//根据机器码生成秘钥
string key1 = Encoding.Default.GetString(key);//把byte转成string
key1 = FileHelper.Encrypt(publicKey, key1);//把秘钥加密一下
File.WriteAllText(@"D:\key.kps", key1);//写入秘钥文件
key1 = FileHelper.Decrypt(publicKey, key1);//解密秘钥下面要用
string path;
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false; //不允许多选
dialog.Title = "选择要加密的文件"; //窗口标题
dialog.Filter = "*.*|*.*"; //选择所有文件
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = dialog.FileName; //获取要加密文件路径
}
else
{
MessageBox.Show("没有选择文件!");
return;
}
byte[] fileB = FileHelper.FileToByte(path);
string fileS = Encoding.Default.GetString(fileB);
string[] a = new string[fileS.Length + 1];
int i = 0;
foreach (char s in fileS)
{
i++;
a[i] = FileHelper.Encrypt(key1, s);
}
string ssss = string.Join(string.Empty, a);
FileHelper.ByteToFile(Encoding.Default.GetBytes(ssss), path + ".已加密");
int index = this.dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[0].Value = path;
dataGridView1.Rows[index].Cells[1].Value = FileHelper.GetFileType(path);
if (File.Exists(path + ".已加密"))
{
dataGridView1.Rows[index].Cells[2].Value = "已加密完成";
}
else
{
dataGridView1.Rows[index].Cells[2].Value = "加密失败";
}
}
以上代码为一个C#的文件加密解密器
加密已经写好,求大佬帮助完善下解密部分
|