2012-04-30 28 views
0

私はMS Office Word 2010ファイル(.docx)から読み込んでいます。そのファイルを使いこなし、新しいファイルに書き込んでいます。私が追加している唯一の文字は、ほとんどのキーボードで見つかった文字(文字、数字、句読点など)です。また、既存の文字を少しでも動かしています。ファイルへの書き込み中にEncoderFallbackExceptionが発生しました

 StreamReader sr = new StreamReader(File.OpenRead("fs.docx")); 
     string foo = sr.ReadToEnd(); 
     sr.Close(); 
     string foo2 = EncryptFile(foo); 
     StreamWriter sw = new StreamWriter(File.Create("sal.docx")); 
     sw.Write(foo2); // THIS IS WHERE THE EXCEPTION HAPPENS 
     sw.Close(); 
     foo = DecryptFile(foo2); 
     StreamWriter sww = new StreamWriter(File.Create("sal2.docx")); 
     sww.Write(foo); 
     sww.Close(); 

    public static string Salt(string Input) 
    { 
     Random rand = new Random(); 
     string Output = ""; 
     string BigSalt = ""; 
     int SaltIncrement = rand.Next(4, 8); 
     for (int i = 0; i < 10; i++) { 
      BigSalt += FindCipherPlainText.Substring(rand.Next(0, FindCipherPlainText.Length), 1); 
     } 
     Input = BigSalt + Input; 

     for (int i = Input.Length; i >= 0; i--) { 
      if ((decimal)i % SaltIncrement == 0) { 
       Input = Input.Insert(i, FindCipherPlainText.Substring(rand.Next(0, FindCipherPlainText.Length), 1)); 
      } 
     } 
     Input += FindCipherPlainText.Substring(rand.Next(0, FindCipherPlainText.Length), 1); 
     Input = ((SaltIncrement + 2) * 8).ToString().Substring(1, 1) + Input + ((SaltIncrement + 2) * 8).ToString().Substring(0, 1) + rand.Next(0, 10).ToString(); 
     return Input; 
    } 

    public static string Mix(string Input) { 
     string Output = ""; 
     if (Input.Length > 1) 
     { 
      if (Input.Length % 2 == 0) 
      { 
       Output = Input.Substring(Input.Length/2); 
       Output += Input.Substring(0, Input.Length/2); 
      } 
      else 
      { 
       Output = Input.Substring((Input.Length - 1)/2); 
       Output += Input.Substring(0, (Input.Length - 1)/2); 
      } 
     } 
     else { 
      return Input; 
     } 
     return Output; 
    } 

    public static string Shift(string Input) { 
     string Output = ""; 
     bool Found = false; 
     for (int i = 0; i < Input.Length; i++) { 
      Found = false; 
      for (int ii = 0; ii < FindCipherPlainText.Length; ii++) { 
       if (Input.Substring(i, 1) == FindCipherPlainText.Substring(ii, 1)) { 
        Output = Output.Insert(0, ReplaceCipherPlainText.Substring(ii, 1)); 
        Found = true; 
        break; 
       } 
      } 
      if (!Found) { 
       Output = Output.Insert(0, Input.Substring(i, 1)); 
      } 
     } 
     return Output; 
    } 

    public static string EncryptFile(string Input) { 
     return Mix( Salt( Shift( Mix( Input)))); 
    } 





System.Text.EncoderFallbackException was unhandled 
    Message=Unable to translate Unicode character \uDF23 at index 428 to specified code page. 
    Source=mscorlib 
    Index=428 

これは私のコードだけでなく、例外の詳細の一部である、と私は何EncryptFile(aboved説明)とDecryptFile()は、文字を追加し、周りにそれらを移動...誰がどんな考えを持っていませんなぜこれが起こっているのですか?

+0

てもらえ、「現実世界のアプリケーションではなく宿題やペットのプロジェクトで使用する暗号化標準(AES、3DES、...)場合'EncryptFile'のコードも投稿してください。あなたの問題の理由は、バイト単位の入力データの操作が、UTF-8に変換できない無効なUnicodeサロゲートペアを作成するということだと思います。 –

+0

完了。私はまた、Encrypt()が誤字文字だったのでEncrypt()をEncryptFile()に変更しました – Oztaco

答えて

5

この例外の理由は、文字スワップ機能が無効なsurrogate pairのUTF-16文字列を生成することになります。つまり、[D800-DBFF]というコードの文字が前にないコード[DC00-DFFF]の文字が少なくとも1文字あります。この文字列は、ファイルに書き込むことはできません。これは、ターゲットエンコーディングで無効な文字を表す方法がないためです。単純な例で、この問題を実証するために

は、ここで同じような状況をシミュレートするコードの一部です:

static void Main(string[] args) 
{ 
    // A perfectly valid surrogate pair with 1st character in the D800-DBFF range, 
    // and 2nd character in the DC00-DFFF range. 
    string validSurrogate = "\uD801\uDC01"; 

    // Creating an invalid surrogate pair just by swapping the two characters in the first string. 
    string invalidSurrogate = validSurrogate.Substring(1, 1) + validSurrogate[0]; 

    // This will work fine. 
    File.WriteAllText("valid.txt", validSurrogate); 

    // --! But this will crash !-- 
    File.WriteAllText("invalid.txt", invalidSurrogate); 
} 

私は、次のをお勧めします:

  • 使用バイト配列の代わりに文字列すべての暗号化/復号化機能で使用できます。次に、これらのバイト配列をテキストとして扱わずにファイルに直接書き込みます。
  • これはあなたが:)の代わりに独自の暗号を設計の暗号化のため
関連する問題