下面这段代码,我要实现将加密后的文件保存在原路径,并且实现秘钥可以由textbox中输入获取 请问一下应该要怎么修改呢 改了好多次了还是不行,直接将key2=textbox也不行,需求帮助:目的是做一个文件加解密系统,不同的用户登陆进行加密后,其他用户在登录的时候不能够进行解密,只有自己能够对自己进行加密的文件进行解密
MyDES des1 = new MyDES();
String jiami;
string encryptedString = str1, key2, iv ;
key2 = textBox2.Text;
iv = "11111111";
byte[] btKey = Encoding.UTF8.GetBytes(key2);
byte[] btIV = Encoding.UTF8.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(encryptedString);
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
jiami = Encoding.UTF8.GetString(ms.ToArray());
}
string key = jiami;
char[] cc = jiami.ToCharArray();
char[] st1 = new char[8];
for (int i = 0; i < 8; i++)
{
st1[i] = cc[i];
}
char[] st2 = new char[8];
for (int i = 0; i < 8; i++)
{
st2[i] = cc[i];
}
String key1 = new string(st1);
String iv1 = new string(st2);
String se, sf, sg;
sg = "D://TyTest";
if (!Directory.Exists(sg))
{
try
{
Directory.CreateDirectory(sg);
}
catch(Exception ex)
{
MessageBox.Show("文件路径创建错误!");
return;
}
}
if (listBox2.Items.Count==0)
{
MessageBox.Show("没有选中文件");
return;
}
int flag = 0;
for (int i = 0; i < listBox2.Items.Count; i++)
{
se = listBox2.Items[i].ToString();
sf = Path.GetFileNameWithoutExtension(se);//返回不具有扩展名的文件名
string extension = System.IO.Path.GetExtension(se);//返回读取文件的扩展名
int h = string.Compare(extension, ".PPP");//将扩展名与.ppp比较
if (h == 0)
{
MessageBox.Show("此为已加密过的文件");
flag = 1;
continue;
}
else
{
try
{
int hh = string.Compare(extension, ".ZIP",true);
if (hh == 0)
{
}
else
{
string filePath = Path.GetDirectoryName(se) + "\\"; // 获取当前文件的路径;myPath为文件的路径
string fileName = sf; //获取用户输入的新的文件名
string fileExtension = ".zip"; //获取文件的后缀名
string NewFile = filePath + fileName + fileExtension;
File.Copy(se, NewFile, true);
File.Delete(se);
}
string[] FileProperties1 = new string[2];
FileProperties1[0] = textBox1.Text + "\\" + sf + ".zip";//待解压的文件
FileProperties1[1] = "D:/TyTest/新建文件夹/";//解压后放置的目标目录
UnZipClass UnZc = new UnZipClass();
UnZc.UnZip(FileProperties1);
string strFileName = "D:/TyTest/新建文件夹" + "/name.txt";
string strFileName1 = "D:/TyTest/新建文件夹" + "/扩展.txt";
/* System.IO.File.SetAttributes(strFileName, System.IO.FileAttributes.Normal);
FileStream fs3 = new FileStream(strFileName, FileMode.Open);
StreamReader sw3 = new StreamReader(fs3);
sw3.BaseStream.Seek(0, SeekOrigin.Begin);
String strLine = sw3.ReadLine();
sw3.Close();
fs3.Close();*/
string strLine = System.IO.File.ReadAllText(strFileName);
// System.IO.File.SetAttributes(strFileName, System.IO.FileAttributes.Normal);
FileStream fs4 = new FileStream(strFileName1, FileMode.Open);
StreamReader sw4 = new StreamReader(fs4);
sw4.BaseStream.Seek(0, SeekOrigin.Begin);
String strLine4 = sw4.ReadLine();
sw4.Close();
fs4.Close();
String strFileName5 = "D:\\TyTest\\";
bool result;
FileStream fin = new FileStream(textBox1.Text + "\\" + sf + strLine4, FileMode.Open, FileAccess.Read);
byte[] Data = new byte[fin.Length];
try
{
fin.Read(Data, 0, Data.Length);
fin.Seek(0, SeekOrigin.Begin);
}
catch
{
}
finally
{
if (fin != null)
fin.Close();
}
System.IO.File.SetAttributes(strFileName5 + str2 + "RsaKey.dat", System.IO.FileAttributes.Normal);
FileStream fkeyin = new FileStream(strFileName5 + str2 +"RsaKey.dat", FileMode.Open, FileAccess.Read);
long keyL = fkeyin.Length;
byte[] key_buf = new byte[keyL];
fkeyin.Read(key_buf, 0, (int)keyL);
string strPublicKey;
strPublicKey = System.Text.Encoding.Default.GetString(key_buf);
RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider();
RSA1.FromXmlString(strPublicKey);
byte[] data = Convert.FromBase64String(strLine);
SHA1 sh = new SHA1CryptoServiceProvider();
result = RSA1.VerifyData(Data, sh, data);
if (result)
{
string filePath1 = Path.GetDirectoryName(se) + "\\"; // 获取当前文件的路径;myPath为文件的路径
string fileName1 = sf; //获取用户输入的新的文件名
string fileExtension1 = ".PPP"; //获取文件的后缀名
string NewFile1 = filePath1 + fileName1 + fileExtension1;
File.Copy(filePath1 + sf + ".zip", NewFile1, true);
File.Delete(filePath1 + sf + ".zip");
File.Delete("D:\\TyTest\\新建文件夹" + "\\" + "name" + ".txt");
File.Delete("D:\\TyTest\\新建文件夹" + "\\" + "扩展" + ".txt");
File.Delete("D:\\TyTest\\新建文件夹\\" + sf);
flag = 1;
MessageBox.Show("此文件不是你加密的");
continue;
}
string filePath2 = Path.GetDirectoryName(se) + "\\"; // 获取当前文件的路径;myPath为文件的路径
string fileName2 = sf; //获取用户输入的新的文件名
string fileExtension2 = extension; //获取文件的后缀名
string NewFile2 = filePath2 + fileName2 + fileExtension2;
File.Copy(filePath2 + sf + ".zip", NewFile2, true);
File.Delete(filePath2 + sf + ".zip");
}
catch
{
string filePath3 = Path.GetDirectoryName(se) + "\\"; // 获取当前文件的路径;myPath为文件的路径
string fileName3 = sf; //获取用户输入的新的文件名
string fileExtension3 = extension; //获取文件的后缀名
string NewFile3 = filePath3 + fileName3 + fileExtension3;
File.Copy(filePath3 + fileName3+".zip", NewFile3, true);
File.SetAttributes(str3 + str2 + "RsaKey.dat", FileAttributes.ReadOnly | FileAttributes.Hidden);
File.Delete(filePath3 + fileName3 + ".zip");
}
}
MyDES.FileEncryptor(se, sg + "\\" + sf + extension, key1, iv1);
FileStream fs2 = new FileStream("D:\\TyTest\\新建文件夹" + "\\" + "扩展" + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw1 = new StreamWriter(fs2);
sw1.WriteLine(extension + "\r\n");
sw1.Close();
fs2.Close();
var cspPas = new CspParameters();
cspPas.KeyContainerName =str2+ "rsa_key";
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider(cspPas);
SHA1 sh1 = new SHA1CryptoServiceProvider();
FileStream fin1 = new FileStream(sg + "\\" + sf + extension, FileMode.Open, FileAccess.Read);
byte[] Data1 = new byte[fin1.Length];
try
{
fin1.Read(Data1, 0, Data1.Length);
fin1.Seek(0, SeekOrigin.Begin);
}
catch
{
}
finally
{
if (fin1 != null)
fin1.Close();
}
byte[] signData = RSA2.SignData(Data1, sh1);
string str5 =Convert.ToBase64String(signData);
FileStream fs1 = new FileStream("D:\\TyTest\\新建文件夹" + "\\" + "name" + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(str5);
sw.Close();
fs1.Close();
string hhh= Path.GetFileNameWithoutExtension(sg + "\\" + sf + extension);
String sourcePath = sg + "\\" + sf + extension;
String targetPath = "D:\\TyTest\\新建文件夹\\" + hhh + extension;
bool isrewrite = true; // true=覆盖已存在的同名文件,false则反之
System.IO.File.Copy(sourcePath, targetPath, isrewrite);
File.Delete(sg + "\\" + sf + extension);
string[] FileProperties = new string[2];
FileProperties[0] = "D:/TyTest/新建文件夹";//待压缩文件目录C:/unzipped/
FileProperties[1] = sg + "\\" + sf+".PPP"; //压缩后的目标文件
ZipFloClass Zc = new ZipFloClass();
Zc.ZipFile(FileProperties[0], FileProperties[1]);
File.Delete("D:\\TyTest\\新建文件夹" + "\\" + "扩展" + ".txt");
File.Delete("D:\\TyTest\\新建文件夹" + "\\" + "name" + ".txt");
File.Delete("D:\\TyTest\\新建文件夹\\" + hhh + extension);
if (f==2)
{
FileInfo fi = new FileInfo(se);
File.Delete(se);
}
}
if (flag == 0)
{
MessageBox.Show("加密完成");
listBox2.Items.Clear();
}
else
{
MessageBox.Show("文件加密不完全成功");
listBox2.Items.Clear();
}
}