/// <summary>
/// DES¼ÓÃÜÎļþ
/// </summary>
/// <param name="inFilePath">´ý¼ÓÃÜÎļþ</param>
/// <param name="outFilePath">¼ÓÃܺóµÄÎļþ</param>
/// <param name="encryptKey">¼ÓÃÜÃÜÔ¿</param>
/// <returns></returns>
public static bool EncryptDES(string inFilePath,string outFilePath,string encryptKey)
{
byte[] rgbIV= Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
//¶ÁÈëµÄÁ÷
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//´ýдµÄÁ÷
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//´´½¨Ò»¸ö±äÁ¿À´°ïÖú¶Áд
byte[] byteIn = new byte[100]; //ÁÙʱ´æ·Å¶ÁÈëµÄÁ÷
long readLen = 0; //¶ÁÈëÁ÷µÄ³¤¶È
long totalLen = inFs.Length; //×ܹ²¶ÁÈëÁ÷µÄ³¤¶È
int everyLen; //ÿ´Î¶ÁÈëÁ÷¶¯³¤¶È
//¶ÁÈëInFs£¬¼ÓÃܺóдÈëOutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while(readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//¼ÓÃܳɹ¦
}
catch
{
return false;//¼ÓÃÜʧ°Ü
}
}
/// <summary>
/// DES½âÃÜÎļþ
/// </summary>
/// <param name="inFilePath">´ý½âÃÜÎļþ</param>
/// <param name="outFilePath">´ý¼ÓÃÜÎļþ</param>
/// <param name="decryptKey">½âÃÜÃÜÔ¿</param>
/// <returns></returns>
public static bool DecryptDES(string inFilePath,string outFilePath,string decryptKey)
{
byte[] rgbIV= Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
//¶ÁÈëµÄÁ÷
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//´ýдµÄÁ÷
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//´´½¨Ò»¸ö±äÁ¿À´°ïÖú¶Áд
byte[] byteIn = new byte[100]; //ÁÙʱ´æ·Å¶ÁÈëµÄÁ÷
long readLen = 0; //¶ÁÈëÁ÷µÄ³¤¶È
long totalLen = inFs.Length; //×ܹ²¶ÁÈëÁ÷µÄ³¤¶È
int everyLen; //ÿ´Î¶ÁÈëÁ÷¶¯³¤¶È
//¶ÁÈëInFs£¬½âÃܺóдÈëOutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while(readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//½âÃܳɹ¦
}
catch
{
return false;//½âÃÜʧ°Ü
}
}
/// DES¼ÓÃÜÎļþ
/// </summary>
/// <param name="inFilePath">´ý¼ÓÃÜÎļþ</param>
/// <param name="outFilePath">¼ÓÃܺóµÄÎļþ</param>
/// <param name="encryptKey">¼ÓÃÜÃÜÔ¿</param>
/// <returns></returns>
public static bool EncryptDES(string inFilePath,string outFilePath,string encryptKey)
{
byte[] rgbIV= Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
//¶ÁÈëµÄÁ÷
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//´ýдµÄÁ÷
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//´´½¨Ò»¸ö±äÁ¿À´°ïÖú¶Áд
byte[] byteIn = new byte[100]; //ÁÙʱ´æ·Å¶ÁÈëµÄÁ÷
long readLen = 0; //¶ÁÈëÁ÷µÄ³¤¶È
long totalLen = inFs.Length; //×ܹ²¶ÁÈëÁ÷µÄ³¤¶È
int everyLen; //ÿ´Î¶ÁÈëÁ÷¶¯³¤¶È
//¶ÁÈëInFs£¬¼ÓÃܺóдÈëOutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while(readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//¼ÓÃܳɹ¦
}
catch
{
return false;//¼ÓÃÜʧ°Ü
}
}
/// <summary>
/// DES½âÃÜÎļþ
/// </summary>
/// <param name="inFilePath">´ý½âÃÜÎļþ</param>
/// <param name="outFilePath">´ý¼ÓÃÜÎļþ</param>
/// <param name="decryptKey">½âÃÜÃÜÔ¿</param>
/// <returns></returns>
public static bool DecryptDES(string inFilePath,string outFilePath,string decryptKey)
{
byte[] rgbIV= Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
//¶ÁÈëµÄÁ÷
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//´ýдµÄÁ÷
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//´´½¨Ò»¸ö±äÁ¿À´°ïÖú¶Áд
byte[] byteIn = new byte[100]; //ÁÙʱ´æ·Å¶ÁÈëµÄÁ÷
long readLen = 0; //¶ÁÈëÁ÷µÄ³¤¶È
long totalLen = inFs.Length; //×ܹ²¶ÁÈëÁ÷µÄ³¤¶È
int everyLen; //ÿ´Î¶ÁÈëÁ÷¶¯³¤¶È
//¶ÁÈëInFs£¬½âÃܺóдÈëOutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while(readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//½âÃܳɹ¦
}
catch
{
return false;//½âÃÜʧ°Ü
}
}
/// DES加密文件
/// </summary>
/// <param name="inFilePath">待加密文件</param>
/// <param name="outFilePath">加密后的文件</param>
/// <param name="encryptKey">加密密钥</param>
/// <returns></returns>
public static bool EncryptDES(string inFilePath,string outFilePath,string encryptKey)
{
byte[] rgbIV= Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
//读入的流
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//待写的流
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//创建一个变量来帮助读写
byte[] byteIn = new byte[100]; //临时存放读入的流
long readLen = 0; //读入流的长度
long totalLen = inFs.Length; //总共读入流的长度
int everyLen; //每次读入流动长度
//读入InFs,加密后写入OutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while(readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//加密成功
}
catch
{
return false;//加密失败
}
}
/// <summary>
/// DES解密文件
/// </summary>
/// <param name="inFilePath">待解密文件</param>
/// <param name="outFilePath">待加密文件</param>
/// <param name="decryptKey">解密密钥</param>
/// <returns></returns>
public static bool DecryptDES(string inFilePath,string outFilePath,string decryptKey)
{
byte[] rgbIV= Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
//读入的流
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//待写的流
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//创建一个变量来帮助读写
byte[] byteIn = new byte[100]; //临时存放读入的流
long readLen = 0; //读入流的长度
long totalLen = inFs.Length; //总共读入流的长度
int everyLen; //每次读入流动长度
//读入InFs,解密后写入OutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while(readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//解密成功
}
catch
{
return false;//解密失败
}
}
========================public class EncDec
{
// Encrypt a byte array into a byte array using a key and an IV
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
// Create a MemoryStream to accept the encrypted bytes
MemoryStream ms = new MemoryStream(); // TripleDES alg = TripleDES.Create();
Rijndael alg = new RijndaelManaged(); //Rijndael.Create(); alg.Key = Key;
alg.IV = IV; CryptoStream cs = new CryptoStream(ms,
alg.CreateEncryptor(), CryptoStreamMode.Write); // Write the data and make it do the encryption
cs.Write(clearData, 0, clearData.Length);
cs.Flush();
cs.Close(); byte[] encryptedData = ms.ToArray();
ms.Flush();
ms.Close();
return encryptedData;
}
// Encrypt a string into a string using a password
// Uses Encrypt(byte[], byte[], byte[])
public static string Encrypt(string clearText, string Password)
{
// First we need to turn the input string into a byte array.
byte[] clearBytes =
System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76}); byte[] encryptedData = Encrypt(clearBytes,
pdb.GetBytes(32), pdb.GetBytes(16)); return Convert.ToBase64String(encryptedData); }
// Encrypt bytes into bytes using a password
// Uses Encrypt(byte[], byte[], byte[]) public static byte[] Encrypt(byte[] clearData, string Password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76}); return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16)); } // Encrypt a file into another file using a password
public static void Encrypt(string fileIn,
string fileOut, string Password)
{ // First we are going to open the file streams
FileStream fsIn = new FileStream(fileIn,
FileMode.Open, FileAccess.Read);
FileStream fsOut = new FileStream(fileOut,
FileMode.OpenOrCreate, FileAccess.Write); // Then we are going to derive a Key and an IV from the
// Password and create an algorithm
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76}); Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16); CryptoStream cs = new CryptoStream(fsOut,
alg.CreateEncryptor(), CryptoStreamMode.Write); int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead; do
{
// read a chunk of data from the input file
bytesRead = fsIn.Read(buffer, 0, bufferLen); // encrypt it
cs.Write(buffer, 0, bytesRead);
} while(bytesRead != 0); // close everything // this will also close the unrelying fsOut stream
cs.Close();
fsIn.Close();
} // Decrypt a byte array into a byte array using a key and an IV
public static byte[] Decrypt(byte[] cipherData,
byte[] Key, byte[] IV)
{
// Create a MemoryStream that is going to accept the
// decrypted bytes
MemoryStream ms = new MemoryStream(); Rijndael alg = new RijndaelManaged(); //Rijndael.Create(); alg.Key = Key;
alg.IV = IV; CryptoStream cs = new CryptoStream(ms,
alg.CreateDecryptor(), CryptoStreamMode.Write); // Write the data and make it do the decryption
cs.Write(cipherData, 0, cipherData.Length);
cs.FlushFinalBlock();
cs.Close(); byte[] decryptedData = ms.ToArray(); return decryptedData;
} // Decrypt a string into a string using a password
// Uses Decrypt(byte[], byte[], byte[]) public static string Decrypt(string cipherText, string Password)
{
// First we need to turn the input string into a byte array.
// We presume that Base64 encoding was used
byte[] cipherBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
0x64, 0x76, 0x65, 0x64, 0x65, 0x76}); byte[] decryptedData = Decrypt(cipherBytes,
pdb.GetBytes(32), pdb.GetBytes(16)); return System.Text.Encoding.Unicode.GetString(decryptedData);
} // Decrypt bytes into bytes using a password
// Uses Decrypt(byte[], byte[], byte[]) public static byte[] Decrypt(byte[] cipherData, string Password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76}); return Decrypt(cipherData, pdb.GetBytes(32), pdb.GetBytes(16));
} // Decrypt a file into another file using a password
public static void Decrypt(string fileIn,
string fileOut, string Password)
{
// First we are going to open the file streams
FileStream fsIn = new FileStream(fileIn,
FileMode.Open, FileAccess.Read);
FileStream fsOut = new FileStream(fileOut,
FileMode.OpenOrCreate, FileAccess.Write);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create(); alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16); CryptoStream cs = new CryptoStream(fsOut,
alg.CreateDecryptor(), CryptoStreamMode.Write);
int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead; do
{
// read a chunk of data from the input file
bytesRead = fsIn.Read(buffer, 0, bufferLen); // Decrypt it
cs.Write(buffer, 0, bytesRead); } while(bytesRead != 0); // close everything
cs.Close(); // this will also close the unrelying fsOut stream
fsIn.Close();
}
}