C#txt文件加密问题

Main_tank 2010-05-11 10:30:52


事情是这样的: 我想用C#加密一个txt文件,

假如加密文件内容为:hehaiqian彭志

加密后我再解密,就成了:hehaiqian��־(也就是后面的两个汉字解密不出来)

请教大侠指点。
...全文
955 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ss_ac 2012-02-29
  • 打赏
  • 举报
回复
请问楼主,您修改之后的opf是什么啊?
nierenyi 2011-08-20
  • 打赏
  • 举报
回复
楼主 请问下:我是吧登录信息保存在txt文档里,想吧密码加密,怎么加密保存,只需加密密码
GADFLYGIS 2011-05-27
  • 打赏
  • 举报
回复
不错,学习了啊
cxd870723 2010-05-19
  • 打赏
  • 举报
回复
谢谢楼主,我还有个问题,就是word用上面的程序无法实现怎么办?
Main_tank 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cxd870723 的回复:]
楼主解决没?我和你是一样的问题,如果解决的话,请回帖,谢谢
[/Quote]
解决了,这么用:
StreamReader b = new StreamReader(opf.FileName, Encoding.Default);
StreamWriter sw = new StreamWriter(sfd.FileName, false, Encoding.Default);
cxd870723 2010-05-18
  • 打赏
  • 举报
回复
楼主解决没?我和你是一样的问题,如果解决的话,请回帖,谢谢
s8848 2010-05-12
  • 打赏
  • 举报
回复
使用uartedit来看看啊
Main_tank 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mngzilin 的回复:]
不要用ascii,
用utf-8,gb2312/.....
[/Quote]

高手,是哪个地方改用utf-8啊??
a12321321321312321 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mngzilin 的回复:]
不要用ascii,
用utf-8,gb2312/.....
[/Quote]
同意,汉字变乱码肯定是你的编码没设置好。

StreamReader b = new StreamReader(opf.FileName, Encoding.Default);
StreamWriter sw = new StreamWriter(sfd.FileName, false, Encoding.Default);
mngzilin 2010-05-11
  • 打赏
  • 举报
回复
不要用ascii,
用utf-8,gb2312/.....
Pro_ah 2010-05-11
  • 打赏
  • 举报
回复
估计你的编码时用成了“ASCII”
用“Unicode”
Main_tank 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yupengcheng_net 的回复:]
不知道你的加密算法,怎么解密了!
[/Quote]

static void EncryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
FileStream fsInput = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);

FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.Create,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,
desencrypt,
CryptoStreamMode.Write);

byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}

static void DecryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

//Create a file stream to read the encrypted file back.
FileStream fsread = new FileStream(sInputFilename,
FileMode.Open,
FileAccess.Read);
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(fsread,
desdecrypt,
CryptoStreamMode.Read);
//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}

static void Main()
{
// Must be 64 bits, 8 bytes.
// Distribute this key to the user who will decrypt this file.
string sSecretKey;

// Get the Key for the file to Encrypt.
sSecretKey = "MainTank";

// For additional security Pin the key.
GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);

// Encrypt the file.
EncryptFile(@"C:\1.txt",
@"C:\2.txt",
sSecretKey);

// Decrypt the file.
DecryptFile(@"C:\2.txt",
@"C:\3.txt",
sSecretKey);

// Remove the Key from memory.
ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
gch.Free();

StreamReader sr = new StreamReader(@"C:\3.txt");

Console.WriteLine(sr.ReadToEnd());
Console.ReadKey();
}
}
网上找的一个算法,我觉得不是算法问题,就没贴出来。。
关键问题:英语字母能正确译码出来,但是汉字就不行。
wuyq11 2010-05-11
  • 打赏
  • 举报
回复
什么加密方法
编码问题
justsoloving 2010-05-11
  • 打赏
  • 举报
回复
帮顶!
孤芳-自赏 2010-05-11
  • 打赏
  • 举报
回复
不知道你的加密算法,怎么解密了!

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧