110,536
社区成员
发帖
与我相关
我的任务
分享
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;
}