这是加密和解密程序* 在C#中 该程序是可行的,并且能够执行以弹出Windows应用程序窗体,但是问题出在我按下“开始”按钮之后,没有结果显示给我该文件已加密到我选择的新路径。代码内有什么问题吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Advanced_Encryption_Decryption
{
public partial class EncryptDecrypt : Form
{
byte[] abc;
byte[,] table;
public EncryptDecrypt()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog od = new OpenFileDialog();
od.Multiselect = false;
if(od.ShowDialog() == DialogResult.OK)
{
tbPath.Text = od.FileName;
}
}
private void btnEncryption_CheckedChanged(object sender, EventArgs e)
{
if(rbEncryption.Checked)
{
rbDecryption.Checked = false;
}
}
private void btnDecryption_CheckedChanged(object sender, EventArgs e)
{
if (rbDecryption.Checked)
rbEncryption.Checked = false;
}
private void EncryptDecrypt_Load(object sender, EventArgs e)
{
rbEncryption.Checked = true;
//Initialize ABC and Table
abc = new byte[256];
for (int i = 0; i < 256; i++)
abc[i] = Convert.ToByte(i);
table = new byte[256, 256];
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
{
table[i, j] = abc[(i + j) % 256];
}
}
private void btnStart_Click(object sender, EventArgs e)
{
//Check Input Values
if(!File.Exists(tbPath.Text))
{
MessageBox.Show("File Does Not Exist");
return;
}
if(String.IsNullOrEmpty(tbPassword.Text))
{
MessageBox.Show("Password Empty. PLease Enter Your Password");
return;
}
//Encryption and Decryption
//Get File Content and Key for Encryption or Decryption
try
{
byte[] fileContent = File.ReadAllBytes(tbPath.Text);
byte[] passwordTmp = Encoding.ASCII.GetBytes(tbPassword.Text);
byte[] keys = new byte[fileContent.Length];
for (int i = 0; i < fileContent.Length; i++)
keys[i] = passwordTmp[i % passwordTmp.Length];
//Encryption
byte[] result = new byte[fileContent.Length];
if (rbEncryption.Checked)
{
for (int i = 0; i < fileContent.Length; i++)
{
byte value = fileContent[i];
byte key = keys[i];
int valueIndex = -1, keyIndex = -1;
for (int j = 0; j < 256; j++)
if (abc[j] == value)
{
valueIndex = j;
break;
}
for (int j = 0; j < 256; j++)
if (abc[j] == key)
{
keyIndex = j;
break;
}
result[i] = table[keyIndex, valueIndex];
}
}
//Decryption
else
{
for (int i = 0; i < fileContent.Length; i++)
{
byte value = fileContent[i];
byte key = keys[i];
int valueIndex = -1, keyIndex = -1;
for (int j = 0; j < 256; j++)
if (abc[j] == value)
{
valueIndex = j;
break;
}
for (int j = 0; j < 256; j++)
if (table[keyIndex, j] == value)
{
valueIndex = j;
break;
}
result[i] = abc[valueIndex];
}
}
//Save Result to New File with the SAME EXTENTION
String fileExt = Path.GetExtension(tbPath.Text);
SaveFileDialog sd = new SaveFileDialog();
sd.Filter = "Files (*" + fileExt + ") | *" + fileExt;
if(sd.ShowDialog() == DialogResult.OK)
{
File.WriteAllBytes(sd.FileName, result);
}
}
catch
{
MessageBox.Show("File is IN USE. Close other program is using
this file and TRY AGAIN");
return;
}
}
}
}