這樣寫的... public class EncryptKit { /// <summary> /// Call this function to remove the key from memory after use for security /// </summary> /// <param name="Destination"></param> /// <param name="Length"></param> /// <returns></returns> [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(IntPtr Destination, int Length); /// <summary> /// Function to Generate a 64 bits Key. /// </summary> /// <returns>返回生成的密钥</returns> public static string GenerateKey() { // Create an instance of Symetric Algorithm. Key and IV is generated automatically. //DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // Use the Automatically generated key for Encryption. //return ASCIIEncoding.ASCII.GetString(desCrypto.Key); byte[] key = new byte[] { 10, 20, 3, 94, 22, 90, 44, 31 }; return ASCIIEncoding.UTF8.GetString(key); } /// <summary> /// 加密文件 /// </summary> /// <param name="sInputFilename">要加密的文件</param> /// <param name="sOutputFilename">加密后保存的文件</param> /// <param name="sKey">密钥</param> public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) { using (FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); fsInput.Close(); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.UTF8.GetBytes(sKey); DES.IV = ASCIIEncoding.UTF8.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsEncrypted.Close(); } } /// <summary> /// /// </summary> /// <param name="sInputFilename">要解密的文件</param> /// <param name="sOutputFilename">解决后保存的文件</param> /// <param name="sKey">密钥</param> public 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.UTF8.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEncoding.UTF8.GetBytes(sKey); //Create a file stream to read the encrypted file back. using (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(); } } }
我有找到這些,也有寫了測試發現對.dll,.lab的文件加密后,再解密出來的文件就無法使用了...
對.txt是沒問題...
找个小一点的文件,1K左右的
加密之前,分析一下byte数组
解密之后,再看byte数组,看到底一致不一致
public class EncryptKit
{
/// <summary>
/// Call this function to remove the key from memory after use for security
/// </summary>
/// <param name="Destination"></param>
/// <param name="Length"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
/// <summary>
/// Function to Generate a 64 bits Key.
/// </summary>
/// <returns>返回生成的密钥</returns>
public static string GenerateKey()
{
// Create an instance of Symetric Algorithm. Key and IV is generated automatically.
//DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
// Use the Automatically generated key for Encryption.
//return ASCIIEncoding.ASCII.GetString(desCrypto.Key); byte[] key = new byte[] { 10, 20, 3, 94, 22, 90, 44, 31 };
return ASCIIEncoding.UTF8.GetString(key);
}
/// <summary>
/// 加密文件
/// </summary>
/// <param name="sInputFilename">要加密的文件</param>
/// <param name="sOutputFilename">加密后保存的文件</param>
/// <param name="sKey">密钥</param>
public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
using (FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
fsInput.Close();
FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.OpenOrCreate,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
DES.IV = ASCIIEncoding.UTF8.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsEncrypted.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="sInputFilename">要解密的文件</param>
/// <param name="sOutputFilename">解决后保存的文件</param>
/// <param name="sKey">密钥</param>
public 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.UTF8.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.UTF8.GetBytes(sKey); //Create a file stream to read the encrypted file back.
using (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();
}
}
}
//加密文件示例
protected void Button1_Click(object sender, EventArgs e)
{
string myFile = TextBox1.Text;
string myPassword = TextBox2.Text;
string myEnFile = TextBox3.Text;
try
{
byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] myKey = System.Text.Encoding.UTF8.GetBytes(myPassword);
FileStream myInStream = new FileStream(myFile, FileMode.Open, FileAccess.Read);
FileStream myOutStream = new FileStream(myEnFile, FileMode.OpenOrCreate, FileAccess.Write);
myOutStream.SetLength(0);
byte[] myBytes = new byte[100];
long myInLength = 0;
long myLength = myInStream.Length;
DES myProvider = new DESCryptoServiceProvider();//指定DES
CryptoStream myCryptoStream = new CryptoStream(myOutStream, myProvider.CreateEncryptor(myKey, myIV), CryptoStreamMode.Write);
while (myInLength < myLength)
{
int mylen = myInStream.Read(myBytes, 0, 100);
myCryptoStream.Write(myBytes, 0, mylen);
myInLength += mylen;
}//codego.net/tags/15/1/
myCryptoStream.Close();
myInStream.Close();
myOutStream.Close();
Response.Write("<script>alert('加密文件成功!');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}
}
//解密示例
protected void Button1_Click(object sender, EventArgs e)
{
string str1 = TextBox1.Text;
string strPwd = TextBox2.Text;
string str2 = TextBox3.Text;
try
{
byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] myKey = System.Text.Encoding.UTF8.GetBytes(strPwd);
FileStream myFileIn = new FileStream(str1, FileMode.Open, FileAccess.Read);
FileStream myFileOut = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
myFileOut.SetLength(0);
byte[] myBytes = new byte[100];
long myLength = myFileIn.Length;
long myInLength = 0;
DES myProvider = new DESCryptoServiceProvider();
CryptoStream myDeStream = new CryptoStream(myFileOut, myProvider.CreateDecryptor(myKey, myIV), CryptoStreamMode.Write);
while (myInLength < myLength)
{
int mylen = myFileIn.Read(myBytes, 0, 100);
myDeStream.Write(myBytes, 0, mylen);
myInLength += mylen;
}
myDeStream.Close();
myFileOut.Close();
myFileIn.Close();
Response.Write("<script>alert('解密文件成功!');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "');</script>");
}