{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode);
RC2CryptoServiceProvider csp=new RC2CryptoServiceProvider();
CryptoStream cryStream=new CryptoStream(fs,csp.CreateEncryptor(),CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
}
想把指定的文件FileName加密后再写如原文件。怎么实现
7 个解决方案
#1
编程/JAVA/.NET/C++ 群号:38850938
#2
void Encrypt(string FileName)
{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode);
RC2CryptoServiceProvider csp=new RC2CryptoServiceProvider();
CryptoStream cryStream=new CryptoStream(fs,csp.CreateEncryptor(),CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
}
using(FileStream fs = File.Open(fileName))
{
using(CryptoStream cryStream = new CryptoStream(fs,csp.CreateEncryptor(),CryptoStreamMode.Write))
{
string tempFile = fileName + ".enc";
FileStream output = new FileStream(tempFile, ...);
byte[1024] buffer = new byte[1024];
int length = cryStream.Read(buffer, 0, buffer.length);
while(length > 0)
{
output.Write(buffer, 0, length);
length = cryStream.Read(buffer, 0, buffer.length);
}
output.Close();
output.Dispose();
cryStream.Close();
}
fs.Close();
}
File.Delete(fileName); //删除原文件
File.Move(tempFile, fileName); //重命名回原文件名
#3
首先多谢楼上的;但发现上面程序有不少问题比如
int length = cryStream.Read(buffer, 0, buffer.length); 规定死了长度,不适于文件备份。
想做一个文件加密,然后再解密,把密匙存入一文件,解密时再加载密匙。现在加密已实现,但不会用密匙解密:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace Examples.System.Net
{
public class CryptoFile
{
public static void Main(string[] args)
{
CryptoFile a = new CryptoFile();
a.Encrypt("j:\\a.txt");
}
void Encrypt(string FileName)
{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode.OpenOrCreate);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
byte[] keys=new byte[csp.Key.Length];
CryptoStream cryStream = new CryptoStream(fs, csp.CreateEncryptor(), CryptoStreamMode.Read);
StreamReader reader=new StreamReader(cryStream);
string s=reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
//将密文写入原文件
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
for (int i = 0; i < csp.Key.Length; i++)
{
keys[i] = csp.Key[i];
}
string k =(string)keys.ToString();
//存密匙,此处有问题。不知用什么方法
File.WriteAllText("j:\\b.xml", k);
}
void Decrypt(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
CryptoStream cryStream = new CryptoStream(fs, csp.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryStream);
string s = reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
}
}
}
int length = cryStream.Read(buffer, 0, buffer.length); 规定死了长度,不适于文件备份。
想做一个文件加密,然后再解密,把密匙存入一文件,解密时再加载密匙。现在加密已实现,但不会用密匙解密:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace Examples.System.Net
{
public class CryptoFile
{
public static void Main(string[] args)
{
CryptoFile a = new CryptoFile();
a.Encrypt("j:\\a.txt");
}
void Encrypt(string FileName)
{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode.OpenOrCreate);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
byte[] keys=new byte[csp.Key.Length];
CryptoStream cryStream = new CryptoStream(fs, csp.CreateEncryptor(), CryptoStreamMode.Read);
StreamReader reader=new StreamReader(cryStream);
string s=reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
//将密文写入原文件
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
for (int i = 0; i < csp.Key.Length; i++)
{
keys[i] = csp.Key[i];
}
string k =(string)keys.ToString();
//存密匙,此处有问题。不知用什么方法
File.WriteAllText("j:\\b.xml", k);
}
void Decrypt(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
CryptoStream cryStream = new CryptoStream(fs, csp.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryStream);
string s = reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
}
}
}
#4
int length = cryStream.Read(buffer, 0, buffer.length); 规定死了长度,不适于文件备份。
-----------------------------------
这是不是可规定死了长度,这是分块来读,
int length = cryStream.Read(buffer, 0, buffer.length);
while(length > 0)
{
output.Write(buffer, 0, length);
length = cryStream.Read(buffer, 0, buffer.length);
}
这样可以防止文件太大而造成内存溢出。最终文件是被读完的。
-----------------------------------
这是不是可规定死了长度,这是分块来读,
int length = cryStream.Read(buffer, 0, buffer.length);
while(length > 0)
{
output.Write(buffer, 0, length);
length = cryStream.Read(buffer, 0, buffer.length);
}
这样可以防止文件太大而造成内存溢出。最终文件是被读完的。
#5
你的代码问题多多:
1. 你把加密后的密文写入原文件就不管了,这个原文件肯定费了,因为这个文件可能变成了前面一部分是秘文,后面一部分是原文件未被覆盖的部分。
你应该fs.SetLength(encodedOverview.Length)把后面的原文部分删掉。
2. 你的所有的Stream使用都没有关闭和释放,这样会造成内存占用不同增长。
Stream用完之后就应该释放掉,如
fs.Close();
fs.Dispose();
3. File.WriteAllText("j:\\b.xml", k); 存文件 (Sorry,我没见过有人这样写文件的),你就用StreamWriter来存文件
using(StreamWriter writer = new ....)
{
writer.WriteLine(...);
}
4. 你加密解密都是一次性处理, 这样大文件肯定不行,必须分块来做,参考我的代码
1. 你把加密后的密文写入原文件就不管了,这个原文件肯定费了,因为这个文件可能变成了前面一部分是秘文,后面一部分是原文件未被覆盖的部分。
你应该fs.SetLength(encodedOverview.Length)把后面的原文部分删掉。
2. 你的所有的Stream使用都没有关闭和释放,这样会造成内存占用不同增长。
Stream用完之后就应该释放掉,如
fs.Close();
fs.Dispose();
3. File.WriteAllText("j:\\b.xml", k); 存文件 (Sorry,我没见过有人这样写文件的),你就用StreamWriter来存文件
using(StreamWriter writer = new ....)
{
writer.WriteLine(...);
}
4. 你加密解密都是一次性处理, 这样大文件肯定不行,必须分块来做,参考我的代码
#6
的确,加密时有问题。又改了一下,不过还是有问题。
现在是想实现一个加密与解密文件的功能。有一个密码参数password。下面的程序你可以看一下直接改。或是你从新帮忙写个可以么?等着用呀。
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test1.txt","password");
Decrypt("j:\\test1.txt", "password");
// Console.WriteLine(Decrypt("j:\\test1.txt","password"));
Console.Read();
}
static void Encrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
string s = reader.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
writer.Write(s);
//cryStream.Write(data,0,data.Length);
fs.Seek(0,0);
fs.Write(data, 0, data.Length);
fs.Close();
}
static void Decrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, System.Text.Encoding.Unicode);
string s = sr.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
fs.Write(data, 0, data.Length);
//String str = sr.ReadToEnd();
//cs.Close();
fs.Close();
//sw.Close();
//byte[] data = new UTF8Encoding(true).GetBytes(str);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
//byte[] data = Convert.FromBase64String(str);
}
}
}
现在是想实现一个加密与解密文件的功能。有一个密码参数password。下面的程序你可以看一下直接改。或是你从新帮忙写个可以么?等着用呀。
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test1.txt","password");
Decrypt("j:\\test1.txt", "password");
// Console.WriteLine(Decrypt("j:\\test1.txt","password"));
Console.Read();
}
static void Encrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
string s = reader.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
writer.Write(s);
//cryStream.Write(data,0,data.Length);
fs.Seek(0,0);
fs.Write(data, 0, data.Length);
fs.Close();
}
static void Decrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, System.Text.Encoding.Unicode);
string s = sr.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
fs.Write(data, 0, data.Length);
//String str = sr.ReadToEnd();
//cs.Close();
fs.Close();
//sw.Close();
//byte[] data = new UTF8Encoding(true).GetBytes(str);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
//byte[] data = Convert.FromBase64String(str);
}
}
}
#7
出来了 分享:
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test.txt","password");
Decrypt("j:\\test.txt", "pssword");
}
static void Encrypt(string filename, string password)
{
//从密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
//把明文以二进制流的形式读出到一个byte数组
BinaryReader reader = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] buffer = new byte[fs.Length];
int length = reader.Read(buffer, 0, buffer.Length);
fs.Close();
//把密文写回文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cryStream.Write(buffer, 0, buffer.Length);
//关闭流
cryStream.Close();
fs.Close();
}
static void Decrypt(string filename, string password)
{
try
{
//根据用户的密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
//用产生的密匙对文件解密
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
//把解密后的流写入byte[]数组
BinaryReader sr = new BinaryReader(cs);
byte[] buffer = new byte[fs.Length];
int length = sr.Read(buffer, 0, buffer.Length);
fs.Close();
//把byte[]的数据写回原文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
sr.Close();
cs.Close();
fs.Close();
}
catch(Exception)
{
Console.Write("密码有误");
}
}
}
}
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test.txt","password");
Decrypt("j:\\test.txt", "pssword");
}
static void Encrypt(string filename, string password)
{
//从密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
//把明文以二进制流的形式读出到一个byte数组
BinaryReader reader = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] buffer = new byte[fs.Length];
int length = reader.Read(buffer, 0, buffer.Length);
fs.Close();
//把密文写回文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cryStream.Write(buffer, 0, buffer.Length);
//关闭流
cryStream.Close();
fs.Close();
}
static void Decrypt(string filename, string password)
{
try
{
//根据用户的密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
//用产生的密匙对文件解密
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
//把解密后的流写入byte[]数组
BinaryReader sr = new BinaryReader(cs);
byte[] buffer = new byte[fs.Length];
int length = sr.Read(buffer, 0, buffer.Length);
fs.Close();
//把byte[]的数据写回原文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
sr.Close();
cs.Close();
fs.Close();
}
catch(Exception)
{
Console.Write("密码有误");
}
}
}
}
#1
编程/JAVA/.NET/C++ 群号:38850938
#2
void Encrypt(string FileName)
{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode);
RC2CryptoServiceProvider csp=new RC2CryptoServiceProvider();
CryptoStream cryStream=new CryptoStream(fs,csp.CreateEncryptor(),CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
}
using(FileStream fs = File.Open(fileName))
{
using(CryptoStream cryStream = new CryptoStream(fs,csp.CreateEncryptor(),CryptoStreamMode.Write))
{
string tempFile = fileName + ".enc";
FileStream output = new FileStream(tempFile, ...);
byte[1024] buffer = new byte[1024];
int length = cryStream.Read(buffer, 0, buffer.length);
while(length > 0)
{
output.Write(buffer, 0, length);
length = cryStream.Read(buffer, 0, buffer.length);
}
output.Close();
output.Dispose();
cryStream.Close();
}
fs.Close();
}
File.Delete(fileName); //删除原文件
File.Move(tempFile, fileName); //重命名回原文件名
#3
首先多谢楼上的;但发现上面程序有不少问题比如
int length = cryStream.Read(buffer, 0, buffer.length); 规定死了长度,不适于文件备份。
想做一个文件加密,然后再解密,把密匙存入一文件,解密时再加载密匙。现在加密已实现,但不会用密匙解密:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace Examples.System.Net
{
public class CryptoFile
{
public static void Main(string[] args)
{
CryptoFile a = new CryptoFile();
a.Encrypt("j:\\a.txt");
}
void Encrypt(string FileName)
{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode.OpenOrCreate);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
byte[] keys=new byte[csp.Key.Length];
CryptoStream cryStream = new CryptoStream(fs, csp.CreateEncryptor(), CryptoStreamMode.Read);
StreamReader reader=new StreamReader(cryStream);
string s=reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
//将密文写入原文件
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
for (int i = 0; i < csp.Key.Length; i++)
{
keys[i] = csp.Key[i];
}
string k =(string)keys.ToString();
//存密匙,此处有问题。不知用什么方法
File.WriteAllText("j:\\b.xml", k);
}
void Decrypt(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
CryptoStream cryStream = new CryptoStream(fs, csp.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryStream);
string s = reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
}
}
}
int length = cryStream.Read(buffer, 0, buffer.length); 规定死了长度,不适于文件备份。
想做一个文件加密,然后再解密,把密匙存入一文件,解密时再加载密匙。现在加密已实现,但不会用密匙解密:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace Examples.System.Net
{
public class CryptoFile
{
public static void Main(string[] args)
{
CryptoFile a = new CryptoFile();
a.Encrypt("j:\\a.txt");
}
void Encrypt(string FileName)
{
FileInfo f = new FileInfo(FileName);
FileStream fs = f.Open(FileMode.OpenOrCreate);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
byte[] keys=new byte[csp.Key.Length];
CryptoStream cryStream = new CryptoStream(fs, csp.CreateEncryptor(), CryptoStreamMode.Read);
StreamReader reader=new StreamReader(cryStream);
string s=reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
//将密文写入原文件
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
for (int i = 0; i < csp.Key.Length; i++)
{
keys[i] = csp.Key[i];
}
string k =(string)keys.ToString();
//存密匙,此处有问题。不知用什么方法
File.WriteAllText("j:\\b.xml", k);
}
void Decrypt(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
CryptoStream cryStream = new CryptoStream(fs, csp.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryStream);
string s = reader.ReadToEnd();
byte[] encodedOverview = new UTF8Encoding(true).GetBytes(s);
fs.Seek(0, 0);
fs.Write(encodedOverview, 0, encodedOverview.Length);
}
}
}
#4
int length = cryStream.Read(buffer, 0, buffer.length); 规定死了长度,不适于文件备份。
-----------------------------------
这是不是可规定死了长度,这是分块来读,
int length = cryStream.Read(buffer, 0, buffer.length);
while(length > 0)
{
output.Write(buffer, 0, length);
length = cryStream.Read(buffer, 0, buffer.length);
}
这样可以防止文件太大而造成内存溢出。最终文件是被读完的。
-----------------------------------
这是不是可规定死了长度,这是分块来读,
int length = cryStream.Read(buffer, 0, buffer.length);
while(length > 0)
{
output.Write(buffer, 0, length);
length = cryStream.Read(buffer, 0, buffer.length);
}
这样可以防止文件太大而造成内存溢出。最终文件是被读完的。
#5
你的代码问题多多:
1. 你把加密后的密文写入原文件就不管了,这个原文件肯定费了,因为这个文件可能变成了前面一部分是秘文,后面一部分是原文件未被覆盖的部分。
你应该fs.SetLength(encodedOverview.Length)把后面的原文部分删掉。
2. 你的所有的Stream使用都没有关闭和释放,这样会造成内存占用不同增长。
Stream用完之后就应该释放掉,如
fs.Close();
fs.Dispose();
3. File.WriteAllText("j:\\b.xml", k); 存文件 (Sorry,我没见过有人这样写文件的),你就用StreamWriter来存文件
using(StreamWriter writer = new ....)
{
writer.WriteLine(...);
}
4. 你加密解密都是一次性处理, 这样大文件肯定不行,必须分块来做,参考我的代码
1. 你把加密后的密文写入原文件就不管了,这个原文件肯定费了,因为这个文件可能变成了前面一部分是秘文,后面一部分是原文件未被覆盖的部分。
你应该fs.SetLength(encodedOverview.Length)把后面的原文部分删掉。
2. 你的所有的Stream使用都没有关闭和释放,这样会造成内存占用不同增长。
Stream用完之后就应该释放掉,如
fs.Close();
fs.Dispose();
3. File.WriteAllText("j:\\b.xml", k); 存文件 (Sorry,我没见过有人这样写文件的),你就用StreamWriter来存文件
using(StreamWriter writer = new ....)
{
writer.WriteLine(...);
}
4. 你加密解密都是一次性处理, 这样大文件肯定不行,必须分块来做,参考我的代码
#6
的确,加密时有问题。又改了一下,不过还是有问题。
现在是想实现一个加密与解密文件的功能。有一个密码参数password。下面的程序你可以看一下直接改。或是你从新帮忙写个可以么?等着用呀。
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test1.txt","password");
Decrypt("j:\\test1.txt", "password");
// Console.WriteLine(Decrypt("j:\\test1.txt","password"));
Console.Read();
}
static void Encrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
string s = reader.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
writer.Write(s);
//cryStream.Write(data,0,data.Length);
fs.Seek(0,0);
fs.Write(data, 0, data.Length);
fs.Close();
}
static void Decrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, System.Text.Encoding.Unicode);
string s = sr.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
fs.Write(data, 0, data.Length);
//String str = sr.ReadToEnd();
//cs.Close();
fs.Close();
//sw.Close();
//byte[] data = new UTF8Encoding(true).GetBytes(str);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
//byte[] data = Convert.FromBase64String(str);
}
}
}
现在是想实现一个加密与解密文件的功能。有一个密码参数password。下面的程序你可以看一下直接改。或是你从新帮忙写个可以么?等着用呀。
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test1.txt","password");
Decrypt("j:\\test1.txt", "password");
// Console.WriteLine(Decrypt("j:\\test1.txt","password"));
Console.Read();
}
static void Encrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
string s = reader.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
writer.Write(s);
//cryStream.Write(data,0,data.Length);
fs.Seek(0,0);
fs.Write(data, 0, data.Length);
fs.Close();
}
static void Decrypt(string filename, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, System.Text.Encoding.Unicode);
string s = sr.ReadToEnd();
byte[] data = System.Text.Encoding.Unicode.GetBytes(s);
fs.Write(data, 0, data.Length);
//String str = sr.ReadToEnd();
//cs.Close();
fs.Close();
//sw.Close();
//byte[] data = new UTF8Encoding(true).GetBytes(str);
//string s = Convert.ToBase64String(data);
//byte[] data1 = Convert.FromBase64String(s);
//byte[] data = Convert.FromBase64String(str);
}
}
}
#7
出来了 分享:
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test.txt","password");
Decrypt("j:\\test.txt", "pssword");
}
static void Encrypt(string filename, string password)
{
//从密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
//把明文以二进制流的形式读出到一个byte数组
BinaryReader reader = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] buffer = new byte[fs.Length];
int length = reader.Read(buffer, 0, buffer.Length);
fs.Close();
//把密文写回文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cryStream.Write(buffer, 0, buffer.Length);
//关闭流
cryStream.Close();
fs.Close();
}
static void Decrypt(string filename, string password)
{
try
{
//根据用户的密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
//用产生的密匙对文件解密
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
//把解密后的流写入byte[]数组
BinaryReader sr = new BinaryReader(cs);
byte[] buffer = new byte[fs.Length];
int length = sr.Read(buffer, 0, buffer.Length);
fs.Close();
//把byte[]的数据写回原文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
sr.Close();
cs.Close();
fs.Close();
}
catch(Exception)
{
Console.Write("密码有误");
}
}
}
}
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
Encrypt( "j:\\test.txt","password");
Decrypt("j:\\test.txt", "pssword");
}
static void Encrypt(string filename, string password)
{
//从密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
//把明文以二进制流的形式读出到一个byte数组
BinaryReader reader = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] buffer = new byte[fs.Length];
int length = reader.Read(buffer, 0, buffer.Length);
fs.Close();
//把密文写回文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
CryptoStream cryStream = new CryptoStream(fs, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cryStream.Write(buffer, 0, buffer.Length);
//关闭流
cryStream.Close();
fs.Close();
}
static void Decrypt(string filename, string password)
{
try
{
//根据用户的密码派生密匙
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
//用DES加密算法
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
//用产生的密匙对文件解密
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
CryptoStream cs = new CryptoStream(fs, desc.CreateDecryptor(key, key), CryptoStreamMode.Read);
//把解密后的流写入byte[]数组
BinaryReader sr = new BinaryReader(cs);
byte[] buffer = new byte[fs.Length];
int length = sr.Read(buffer, 0, buffer.Length);
fs.Close();
//把byte[]的数据写回原文件
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
sr.Close();
cs.Close();
fs.Close();
}
catch(Exception)
{
Console.Write("密码有误");
}
}
}
}