1

I'm making a simple custom encrypting in C#.NET, the encryption passes succesfully, but the decrypting goes wrong. The algorithm is very intuitive, but I don't know why it's decrypted wrong. Here is my code:

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Encrypting 
            byte[] initial_text_bytes = Encoding.UTF8.GetBytes(initial_text_tb.Text);
            byte[] secret_word_bytes = Encoding.UTF8.GetBytes(secret_word_tb.Text);
            byte[] encrypted_bytes = new byte[initial_text_bytes.Length];




            int secret_word_index = 0;
            for (int i=0; i < initial_text_bytes.Length; i++)
            {
                if (secret_word_index == secret_word_bytes.Length)
                {
                    secret_word_index = 0;
                }
                encrypted_bytes[i] = (byte)(initial_text_bytes[i] + initial_text_bytes[secret_word_index]);
                secret_word_index++;
            }



          //  String s = Encoding.UTF8.GetString(encrypted_bytes);
                //new String(Encoding.UTF8.GetChars(encrypted_bytes));

            text_criptat_tb.Text = Convert.ToBase64String(encrypted_bytes);


        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //Decrypting
            byte[] initial_text_bytes = Encoding.UTF8.GetBytes(text_criptat_tb.Text);
            byte[] secret_word_bytes = Encoding.UTF8.GetBytes(secret_word_tb.Text);
            byte[] encrypted_bytes = new byte[initial_text_bytes.Length];

            int secret_word_index = 0;
            for (int i = 0; i < initial_text_bytes.Length; i++)
            {
                if (secret_word_index == secret_word_bytes.Length)
                {
                    secret_word_index = 0;
                }
                encrypted_bytes[i] = (byte)(initial_text_bytes[i] - initial_text_bytes[secret_word_index]);
                secret_word_index++;
            }
           // String s = new String(Encoding.UTF8.GetChars(encrypted_bytes));

            initial_text_tb.Text = Convert.ToBase64String(encrypted_bytes);


        }

And this is what I get when I encrypt: After encrypting And this is when I decrypt: After decrypting Thanks

3
  • 6
    "I'm making a simple custom encrypting " - Don't write your own encryption. It will be flawed... Commented Feb 21, 2012 at 23:58
  • You don't seem to be converting back from base64 anywhere... Commented Feb 22, 2012 at 0:01
  • Mitch, it's for a lab for uni Commented Feb 22, 2012 at 0:39

1 Answer 1

5

I see four problems with the code.

1. You are adding bytes from the initial text instead of the secret word.

Here:

encrypted_bytes[i] = (byte)(initial_text_bytes[i] + initial_text_bytes[secret_word_index]);

Use instead:

encrypted_bytes[i] = (byte)(initial_text_bytes[i] + secret_word_bytes[secret_word_index]);

2. You are using Encoding.UTF8.GetBytes to try to decode the base-64 string.

Here:

byte[] initial_text_bytes = Encoding.UTF8.GetBytes(text_criptat_tb.Text);

Use instead:

byte[] initial_text_bytes = Convert.FromBase64String(text_criptat_tb.Text);

3. You are subtracting bytes from the inital text intead of the secret word.

Here:

encrypted_bytes[i] = (byte)(initial_text_bytes[i] - initial_text_bytes[secret_word_index]);

Use instead:

encrypted_bytes[i] = (byte)(initial_text_bytes[i] - secret_word_bytes[secret_word_index]);

4. You are using Convert.ToBase64String to try to decode the UTF-8 data.

Here:

initial_text_tb.Text = Convert.ToBase64String(encrypted_bytes);

Use instead:

initial_text_tb.Text = Encoding.UTF8.GetString(encrypted_bytes);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.