C # - Decrypt a file with a Key XOR

1

I'm having a problem that is burning my head! What I need is to decrypt a .bmd file in c #

I already have a program in C ++ that does this procedure, but I can not make it work in c ++. It shows me all signs of questions.

This is the code in C ++:

BYTE xorKey[4] = { 0xFF, 0xFA, 0x5, 0x88 };

    void Load(std::string file)
    {
        LPSTR filedec = "/file.bmd";

        FILE* hFile = fopen(file.c_str(), "rb");
        fseek(hFile, 0, SEEK_END);
        int size = ftell(hFile);
        fseek(hFile, 0, SEEK_SET);

        BYTE* buf = new BYTE[size];
        fread(buf, 1, size, hFile);

        fclose(hFile);


        for (int i = 0; i < size; i++)
        {
            buf[i] ^= xorKey[i % 4];
        }

        std::ofstream decFile;
        decFile.open(filedec);
        decFile << buf;
        decFile.close();

        DeleteFileA(filedec);
    }

And I in C # try to decrypt it in the following way:

  private string GetText(byte[] Text)
    {
        byte[] Key = { 0xFF, 0xFA, 0x5, 0x88 };
        // ----
        for (int i = 0; i < Text.Length; i++)
        {
            Text[i] ^= Key[i % 4];
        }
        // --


        return Encoding.ASCII.GetString(Text);

    }



      private void button1_Click(object sender, EventArgs e)
                {
                    string fn = "";

                    openFileDialog1.Filter = "ServerInfo.bmd files (*.bmd)|*.bmd";

                    openFileDialog1.Multiselect = false;


                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {

                        fn = openFileDialog1.FileName;

                        richTextBox1.Text = GetText(Encoding.ASCII.GetBytes(readText(fn)));

                    }


                }





     private string readText(string fn)

        {

            FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read);

            StreamReader fr = new StreamReader(fs);

            string content;

            content = fr.ReadToEnd();

            fs.Close();//close the file

            return content;
        }
    
asked by Ignacio Copparoni 01.01.2019 в 22:53
source

0 answers