2013-10-23 60 views
0

我嘗試使用RSA和C#加密並解密XML文件,而當我非常接近時,出現了問題。一旦解密,幾乎所有的文件都在那裏,但最終還是有一個呃逆。它或者是文件末尾的差距,或者更多的數據被追加到文件的最後。使用C#和RSA加密/解密文件

這裏是我的加密方法:

public static bool Encrypt(ProcessingHolder ph) 
    { 
     FileInfo inFile = ph.encryptedFI; 
     FileInfo outFile = ph.unEncryptedFI; 

     X509Certificate2 daCert = new X509Certificate2(keyFP, daCertPassword); 
     RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)daCert.PrivateKey; 

     bool done = false; 
     FileStream fs = null; 
     FileStream fso = null; 

     try 
     { 
      //opens the file to encrypt into a filestream object 
      fs = inFile.OpenRead(); 

      //240 is what the iOS side is using 
      //algorithm that calculates max bytes ((KeySize - 384)/8) + 37 
      //(returns 245) 
      int chunkSize = 245; 

      fso = outFile.OpenWrite(); 
      byte[] buffer = new byte[chunkSize]; 
      int totalRead = 0; 


      while (totalRead < fs.Length) 
      { 
       int readBytes = fs.Read(buffer,0, chunkSize); 

       totalRead += readBytes; 

       //check to see if the final chunk of data is less than 245 so as not to write empty buffer 
       if (readBytes < chunkSize) buffer = new byte[readBytes]; 
       //byte[] encr = new byte[readBytes]; 

       //actual encryption 
       //encr = RSA.Encrypt(buffer, false); 

       byte[] encr = RSA.Encrypt(buffer, false); 
       fso.Write(encr, 0, encr.Length); 
      } 
      fso.Flush(); 
      fso.Close(); 
      fs.Close(); 
      done = true; 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine("Decrypt failed with message " + ex.Message); 
      done = false; 
     } 
     finally 
     { 
      if (fs != null) fs.Close(); 
      if (fso != null) fso.Close(); 
     } 
     return done; 
    } 
} 

,這裏是我的解密方法:

public static bool Decrypt(ProcessingHolder ph) 
    { 
     FileInfo inFile = ph.encryptedFI; 
     FileInfo outFile = ph.unEncryptedFI; 

     X509Certificate2 daCert = new X509Certificate2(keyFP, daCertPassword); 
     RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)daCert.PrivateKey; 

     bool done = false; 
     FileStream fs = null; 
     FileStream fso = null; 

     try 
     { 
      fs = inFile.OpenRead(); 
      int chunkSize = 256; 

      fso = outFile.OpenWrite(); 
      byte[] buffer = new byte[chunkSize]; 
      int totalRead = 0; 

      while (totalRead < fs.Length) 
      { 
       int readBytes = fs.Read(buffer, 0, chunkSize); 
       totalRead += readBytes; 

       //check to see if the final chunk of data is less than 245 so as not to write empty buffer 
       //if (readBytes < chunkSize) buffer = new byte[readBytes]; 

       byte[] decr = RSA.Decrypt(buffer, false); 
       fso.Write(decr, 0, decr.Length); 
      } 
      fso.Flush(); 
      fso.Close(); 
      fs.Close(); 
      done = true; 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine("Decrypt failed with message " + ex.Message); 
      done = false; 
     } 
     finally 
     { 
      if (fs != null) fs.Close(); 
      if (fso != null) fso.Close(); 
     } 
     return done; 
    } 

撞我的頭靠在牆上這裏,在此先感謝

+0

我讀*塊*並*打嗝*應該是這種情況。 – Sinatr

+0

我繼承了這個應用程序,我完全不熟悉加密。你的諷刺並不會讓我失望,它只是沒有任何幫助。 – TrevorGoodchild

+0

1)不要將文件分成塊進行RSA加密。創建一個隨機AES-Key,用RSA加密該密鑰,用AES加密該文件。 2)如果您將'false'作爲第二個參數傳遞給'Encrypt'以打開某些攻擊。我強烈建議將其設置爲「true」以啓用OAEP。 – CodesInChaos

回答

0

加密期間會發生什麼如果該文件不是該塊大小的長度的倍數? IE瀏覽器。一個500字節長的文件將讀取兩組245,但它們剩下10個字節?這可能會丟失最後的幾個字節或添加額外的值?

也許你需要一個頭在解密文件的字節添加到該文件與大小,以便解密知道在哪裏可以在加密過程中的方式停止,墊出來的最後一塊