C#如何对文件夹加密和解密

xwh0318 2008-07-22 05:45:00
C#如何对文件夹加密和解密
...全文
1492 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cquwtz 2012-08-14
  • 打赏
  • 举报
回复
看不到啊
a7920 2011-12-05
  • 打赏
  • 举报
回复
闲逛来学习下
slang98 2011-11-10
  • 打赏
  • 举报
回复
闲逛来学习下
mycjzlove 2011-03-04
  • 打赏
  • 举报
回复
学习下啊。。。。
jpvyiu 2010-08-07
  • 打赏
  • 举报
回复
顶一个
w450534843 2010-06-22
  • 打赏
  • 举报
回复
学习~
chenhong108 2009-07-31
  • 打赏
  • 举报
回复
我也想知道.
MoonDrunkLotus 2009-07-03
  • 打赏
  • 举报
回复
这个加密与解密在同一个应用程序没问题,若把文件上传到服务器上时加密,下载到客户端解密就不行了!大侠们有没有解决办法!
JonesVale 2009-04-29
  • 打赏
  • 举报
回复
这些加密都是无法得到想要的解密,当在同一个个位置不发生改变的时候就可以解密,加密如果与解密在不同点,就不能解密了,即使保存了加密的KEY与IV再下次的时候使用同样的KEY与IV也无法完成解密动作。也就是说这些都是不是我们想要的加密与解密。
zcdg909 2009-04-23
  • 打赏
  • 举报
回复

这样调用:Encrypt(@"D:\dpp\cx", @"D:\dpp\pp", "123");
但出现错误:
对路径“D:\dpp\cx”的访问被拒绝。

错误在语句:
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
A-雷子 2009-04-23
  • 打赏
  • 举报
回复
学习地
Ador3 2008-07-22
  • 打赏
  • 举报
回复
一些简单的加解密

public static void Encrypt(string pathIn, string pathOut, string password)
{
try
{
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
outfs.SetLength(0);
byte[] myBytes = new byte[100];
long myInLength = 0;
long myLength = fs.Length;
System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
while (myInLength < myLength)
{
int myLen = fs.Read(myBytes, 0, 100);
cs.Write(myBytes, 0, myLen);
myInLength += myLen;
}
cs.Close();
fs.Close();
outfs.Close();
}
catch (IOException ioex)
{
Log.debug("Encrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
}
}

public static void Decrypt(string pathIn, string pathOut, string password)
{
try
{
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
outfs.SetLength(0);
long myLength = fs.Length;
long myInLength = 0;
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);
byte[] myBytes = new byte[100];
while (myInLength < myLength)
{
int myLen = fs.Read(myBytes, 0, 100);
cs.Write(myBytes, 0, myLen);
myInLength += myLen;
}
cs.Close();
fs.Close();
outfs.Close();
}
catch (IOException ioex)
{
Log.debug("Dncrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
}
}

public static string EncryptBy16MD5(string strProclaimed)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string strCryptograph = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(strProclaimed)), 4, 8);
return strCryptograph;
}

/// <summary>
/// md5 32位加密
/// </summary>
/// <param name="strProclaimed">明文</param>
/// <returns>密文</returns>
public static string EncryptBy32MD5(string strProclaimed)
{
MD5 md5 = MD5.Create();
string strCryptograph = "";
byte[] bytCode = md5.ComputeHash(Encoding.UTF8.GetBytes(strProclaimed));
for (int i = 0; i < bytCode.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
strCryptograph = strCryptograph + bytCode[i].ToString("x");
}
return strCryptograph;
}

110,536

社区成员

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

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

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