Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:30:34+00:00 2026-06-03T05:30:34+00:00

I’m reading from a MS Office Word 2010 file (.docx) and then messing with

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:30:36+00:00Added an answer on June 3, 2026 at 5:30 am

    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 🙂
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.