2011-07-12 32 views
0

Rijndael暗号化を使用してすべてのタイプのファイルを暗号化します。最新の.xlsxファイルと.docxファイルは、開こうとすると(暗号化と復号化の後に)エラーをスローしています。 Excel 2003がファイルを開こうとするときにエラーが発生しました:「コンバータがファイルを開けませんでした」 Excel 2003でxlsxファイルを開くことができます。C#AESまたはRijndaelを使用してxlsx、docxファイルを暗号化および復号化

AESを使用するコードを変更しましたが、同じ種類の問題(この場合はファイルFirefoxのダウンロードリストにはダウンロードされません)。私は暗号化/復号化されたファイルのバイトサイズ/長さに注意を払うためにここで提案を読んだが、これを修正する方法については紛失している。xlsファイルをアップロードすると、解読されたファイルが出てきて、xlsが保存されて正常に開くので、これらの長さは動作するファイルとは異なるので、これが問題であるかどうかテストする方法はわかりません。 xlsx/docxファイルの暗号化エラーを引き起こす可能性のある問題が誰かに見つかるかどうかを確認するためのコードを含めています。私はコードを最小限に抑えました。もし構文エラーがあればそれが原因でしょう。

Excel 2007をインストールして、暗号化および復号化された.xlsxファイルがExcel 2007で開かれるかどうかを確認します。ファイルを開こうとすると、「Excelは 'myfile.xlsx 'あなたはこのワークブックの内容を回復しますか? " Excel 2007は、「Excelがファイルレベルの検証と修復を完了しました。このワークブックの一部が修復または破棄された可能性があります」というメッセージでファイルを修復/修復できます。したがって、暗号化/復号化では無効なファイルが作成されますが、Excel 2007ではこれを修復できます。 Excel 2003コンバータはファイルで何もできません。

public byte [] Encrypt(byte [] bytes) 
     { 
      if (myRijndael == null) 
       myRijndael = new RijndaelManaged(); 
      ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV); 
      MemoryStream msEncrypt = new MemoryStream(); 
      CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write); 
      csEncrypt.Write(bytes, 0, bytes.Length); 
      csEncrypt.FlushFinalBlock(); 
      return msEncrypt.ToArray(); 
     } 

public byte [] Decrypt(byte [] encrypted, string text) 
     { 
      if (myRijndael == null) 
{ 
        myRijndael = new RijndaelManaged(); 
} 
      ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV); 
      MemoryStream msDecrypt = new MemoryStream(encrypted); 
      CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); 
      byte [] fromEncrypt = new byte[encrypted.Length]; 
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); 
      return fromEncrypt; 
} 


Usage: 
ENCRYPT: 
ClcCrypto crypt; // Our class for saving keys etc. 
ClcCrypto(CLC.WebUtil.ClcCrypto.GetDecryptionKey(), Group.IV); 
BinaryReader br = new BinaryReader(NewFile);// NewFile is Stream from filMyFile.PostedFile.InputStream 
byte[] EncryptedContents = crypt.Encrypt(br.ReadBytes((int)NewFile.Length)); 
FileStream fs = File.Create(DestFileName); 
BinaryWriter bw = new BinaryWriter(fs); 
bw.Write(EncryptedContents); 
bw.Close(); 
fs.Close(); 
br.Close(); 

DECRYPT (file download): 
byte[] baOut = null; 
baOut = fiOut.GetFileData(out lLength); // See below for method 
Response.AddHeader("content-disposition", "attachment; filename=" + FileName)); 
Response.ContentType = fiOut.MimeType; 
Response.AddHeader("content-length", lLength.ToString()); 
Response.BinaryWrite(baOut); 
Response.End(); 

public byte[] GetFileData(out long intFileSize) 
{ 
FileStream fsOut = new FileStream(FilePath, FileMode.Open, FileAccess.Read); 
     intFileSize = fsOut.Length; 
     byte[] Buffer = null; 
     ClcCrypto crypt; 
     crypt = new CLC.WebUtil.ClcCrypto(CLC.WebUtil.ClcCrypto.GetDecryptionKey(), IV); 
     BinaryReader br = new BinaryReader(fsOut); 
     Buffer = crypt.Decrypt(br.ReadBytes((int)fsOut.Length), null); 
     br.Close(); 
     fsOut.Close(); 
     return Buffer; 
} 

答えて

0

は切り捨て問題のように聞こえます。おそらく、あなたがメモリストリームを作成している方法のためです。私はそれが次

MemoryStream msDecrypt = new MemoryStream(); 
CryptoStream csDecrypt = new CryptoStream(
    msDecrypt, 
    decryptor, 
    CryptoStreamMode.Write); 
csStream.Write(encrypted, 0, encrypted.Length); 
csStream.Flush(); 
return csDecrypt.ToArray(); 

のように見える解読とき、私は追加される余分なヌルバイトがあることを推測するだろう、と別のアプローチを使用して、これを緩和することができます。

関連する問題