2011-03-27 100 views
-1

我需要將解密集成到加密文件中,並且當您運行加密文件以要求輸入密碼時,之後文件將被解密。如何將解密過程集成到加密文件中?

的源代碼,我從codeproject

了我可以添加一個密碼請求,解密程序到加密程序? 加密程序:

/// <summary> 
/// This takes an input file and encrypts it into the output file 
/// </summary> 
/// <param name="inFile">the file to encrypt</param> 
/// <param name="outFile">the file to write the encrypted data to</param> 
/// <param name="password">the password for use as the key</param> 
/// <param name="callback">the method to call to notify of progress</param> 
public static void EncryptFile(string inFile, string outFile, string password, CryptoProgressCallBack callback) 
{ 
    using(FileStream fin = File.OpenRead(inFile), 
       fout = File.OpenWrite(outFile)) 
    { 
     long lSize = fin.Length; // the size of the input file for storing 
     int size = (int)lSize; // the size of the input file for progress 
     byte[] bytes = new byte[BUFFER_SIZE]; // the buffer 
     int read = -1; // the amount of bytes read from the input file 
     int value = 0; // the amount overall read from the input file for progress 

     // generate IV and Salt 
     byte[] IV = GenerateRandomBytes(16); 
     byte[] salt = GenerateRandomBytes(16); 

     // create the crypting object 
     SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt); 
     sma.IV = IV;    

     // write the IV and salt to the beginning of the file 
     fout.Write(IV,0,IV.Length); 
     fout.Write(salt,0,salt.Length); 

     // create the hashing and crypto streams 
     HashAlgorithm hasher = SHA256.Create(); 
     using(CryptoStream cout = new CryptoStream(fout,sma.CreateEncryptor(),CryptoStreamMode.Write), 
        chash = new CryptoStream(Stream.Null,hasher,CryptoStreamMode.Write)) 
     { 
      // write the size of the file to the output file 
      BinaryWriter bw = new BinaryWriter(cout); 
      bw.Write(lSize); 

      // write the file cryptor tag to the file 
      bw.Write(FC_TAG); 

      // read and the write the bytes to the crypto stream in BUFFER_SIZEd chunks 
      while((read = fin.Read(bytes,0,bytes.Length)) != 0) 
      { 
       cout.Write(bytes,0,read); 
       chash.Write(bytes,0,read); 
       value += read; 
       callback(0,size,value); 
      } 
      // flush and close the hashing object 
      chash.Flush(); 
      chash.Close(); 

      // read the hash 
      byte[] hash = hasher.Hash; 

      // write the hash to the end of the file 
      cout.Write(hash,0,hash.Length); 

      // flush and close the cryptostream 
      cout.Flush(); 
      cout.Close(); 
     } 
    } 
} 
+0

您鏈接到的網站具有隨附的解密方法。 – 2011-03-27 06:01:48

+0

@Andrew Marshall:是的,我知道,但我需要將此過程集成到我的程序中,這樣當您運行我的程序文件的加密版本時,它將需要密碼 – DmitryB 2011-03-27 06:05:52

+1

因此,然後創建一個GUI並調用該函數所需的參數取自GUI字段。 – 2011-03-27 06:07:19

回答

1

如果我明白了,你想有一個文件打開,提示輸入密碼,如果密碼正確,運行真正的程序?

如果是這樣,只是做一個快速的MD5哈希檢查(與鹽),看看這個:HOWTO: Encode a password using MD5 in C#,然後獲取嵌入代碼:How to embed and access resources by using Visual C#,然後運行它:How to programmatically compile code using C# compiler

除了基本的md5檢查(對於反射器不做任何事情),您可以使用對稱加密(使用諸如C# Symmetric Encryption之類的東西)來加密嵌入數據,然後使用輸入的密碼對其進行解密。

希望能夠幫助您開始使用