Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This encryption method I write working if I encrypt / decrypt text document.
Then I tried to encrypt word document plain text (content), and sadly some character was unsupported.
So I decided to encrypt the whole file bytes.
I thought this method can encrypt any file because now I encrypt the whole bytes.
But after I encrypt and decrypt, the file corrupted except text document.
This is my code.

C#
byte[] b = null;

            b = System.IO.File.ReadAllBytes(this.txtFile.Text);

            string key = this.txtKey.Text;
            int keyIndex = 0;

            for (int i = 0; i < b.Length; i++) 
            {
                if (keyIndex >= key.Length)
                {
                    keyIndex = 0; //key will repeat all over again
                }

                string fileBIN = Convert.ToString(b[i], 2);
                string keyBIN = Convert.ToString(Convert.ToInt32(Convert.ToChar(key[keyIndex])), 2);
                string newBIN = string.Empty;

                keyIndex += 1;

                if (fileBIN.Length > keyBIN.Length)
                {
                    for (int x = 0; x < Math.Abs(fileBIN.Length - keyBIN.Length); x++) { keyBIN = "0" + keyBIN; x -= 1; } //add 0 to binary that was shorter
                }
                else
                {
                    for (int x = 0; x < Math.Abs(fileBIN.Length - keyBIN.Length); x++) { fileBIN = "0" + fileBIN; x -= 1; } //add 0 to binary that was shorter
                }

                if (fileBIN == keyBIN)
                {
                    newBIN = fileBIN;
                }
                else
                {
                    for (int x = 0; x < fileBIN.Length; x++)
                    {
                        if (fileBIN.Substring(x, 1).ToString() == keyBIN.Substring(x, 1).ToString()) { newBIN += "0"; }
                        else { newBIN += "1"; }
                    }
                }
                b[i] = Convert.ToByte(newBIN, 2);
            }

            System.IO.File.WriteAllBytes(this.txtFile.Text, b);
Posted
Updated 15-Dec-13 18:09pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Dec-13 0:28am    
Why using this weird method of encryption? What is the purpose of this particular encryption? How about using one of the algorithms implemented in .NET FCL?
—SA

The reason it doesn't work is because you're converting everything to strings. Encryption systems (anything but very rudimentary) always work on byte streams, not strings. This "home grown" thing you wrote is never going to work right. It just doesn't make sense.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Dec-13 0:39am    
We answered at the same time... a 5.
—SA
Midnight Ahri 16-Dec-13 0:42am    
Lol.
Actually this code is correct, just like what is in my lecturer powerpoint.
First step is to convert both text and key to ascii code / decimal, then convert the ascii code to binary.
Then combine both binary and convert it back to decimal, and the last step, convert back to text.
I know this encryption is stupid, what I do is also stupid.
But seriously, this is the last step, to encrypt word document.
Everything is already complete.
I can even encrypt/decrypt text document perfectly.
Just like what you said, I encrypt everything including the file binary header that represent the file type or whatever it is that was written in wikipedia.
Thats why the file corrupted.
I know that I can simply call dot net cryptography library.
But even how stupid this project is, my lecturer still able to know that I'm not doing it myself.
Sergey Alexandrovich Kryukov 16-Dec-13 1:45am    
Even if it works, it does not mean it is correct. Yes, I appreciate that you are doing it all by yourself, if so is required. Nevertheless, both answers pointed out your fundamental mistakes. Unicode text is not composed of bytes, but UTFs are.
—SA
Dave Kreskowiak 16-Dec-13 7:46am    
Yeah, this isn't going to work for anything other than a text file. A Word document is not a text file. It's most certainly binary.
Midnight Ahri 16-Dec-13 1:48am    
Thats what I need to solve my problem. I apologize of my poor written code. :(
"Some character in unsupported" — is already the absurd. Never ever try to encrypt characters. First, serialize all data into array of bytes, byte[]; it can be done using one of System.Text.Encoding classes:
http://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx[^].

And, of course, only use UTF encodings, better be UTF-8.

Then do encryption, and, when you need it, the reverse operations: decryption and getting strings from byte[].

See also: http://msdn.microsoft.com/en-us/library/System.Security.Cryptography%28v=vs.110%29.aspx[^].

Sorry, but digging in your code is just not interesting. Why wasting any time which already makes no sense?

—SA
 
Share this answer
 
Comments
Midnight Ahri 16-Dec-13 1:13am    
Thankyou very much for your answer, it works for any files now.
Nothing is wrong in my code, the reason why it's not interesting and makes no sense is because my code is messy.
Whatever, I'm going to get A+. =D
Sergey Alexandrovich Kryukov 16-Dec-13 1:43am    
Good luck with that. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900