Oztaco
Oztaco

Reputation: 3459

EncoderFallbackException while writing to file

I'm reading from a MS Office Word 2010 file (.docx) and then messing with it, then writing it into a new file. The only characters I'm adding in are the ones found on most keyboards (letters, numbers, punctuation...) and i'm also moving the existing characters around a little bit.

        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

This is my code, as well as some of the exceptions details, and i described aboved what the EncryptFile() and DecryptFile() does, add chars, move them around...does anyone have any idea why this is happening?

Upvotes: 0

Views: 2654

Answers (1)

Yuriy Guts
Yuriy Guts

Reputation: 2220

The reason for this exception is that the character-swapping functions end up producing a UTF-16 string with an invalid surrogate pair. That said, there is at least one character with code [DC00-DFFF] that is not preceded by a character with code [D800-DBFF]. This string cannot be written to a file because there is no way to represent invalid characters in the target encoding.

To demonstrate this issue on a simpler example, here is a piece of code that simulates the same situation:

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);
}

I would recommend the following:

  • Use byte arrays instead of strings in all encryption/decryption functions. Then write these byte arrays directly to files without treating them as text.
  • If this is a 'real-world' application and not a homework or pet project, use cryptography standards (AES, 3DES, ...) for encryption instead of designing own ciphers :)

Upvotes: 8

Related Questions