Simple C# Text Encoding

Hi folks,

In this post I am going to teach you how to develop a simple c# text encoding (Note: we are going to improve its skills with a different method later). Our encoding method is very simple. All chars in Ascii table has an Ascii value. For example; letter ‘A’ has the value 65 in Ascii table. We are going to use these table values to encode our text. If you are ready, here we go!

  • First of all, we are going to compose a Windows Form Application.. Just do the followings..
  • Open Visual Studio if still do not 🙂 Then go.. File > New > Project
  • Select Windows Forms Applications beneath Visual C# > Windows category
  • Give a name to your project (I named it as CryptographyApp because as I mention at the beginning we are going to write a different methode to encode our strings later)
  • Have your form appear like in the picture below.

I added one RichTextBox and three Button controls to my Form and altered their properties as:

Form: Text – Encryption Application

RichTextBox: Name – rtbEncryption

First Button: Name – btnEncode, Text – ENCODE

Second Button: Name – btnDecode, Text – DECODE

Third Button: Name – btnClear, Text -CLEAR

Now we are ready to code!

  • Double click on btnEncode to go for its Click event.

Line 20: Defining an empty string variable. With help of it our RichTextBox is going to show encoded text.

Line 22: For loop starts from 0 and goes through our text to be encoded.

Line 24: This is the most important part of our code. Actually this line of code does all the job. Our empty cryptText variable is equivalent to its last value plus encrypted char. This means for loop searchs for all characters in our RichTextBox and with (char)(rtbEncryption.Text[i] + 1) code block it gets the ascii value of its following char(because we added 1 to its ascii value, if you want to do a different encryption you can change this value as you wish, but consider there are only 255 ascii values 🙂 )

(rtbEncryption.Text[i] + 1) has an integer value which we can see in Ascii table, with casting it to char we get the character that matches with the integer value in Ascii table.

In short; cryptText is always adding itself the next char value according to Ascii table (A-B, D-F, etc..).

Line 27: We assign the encoded string to our RichTextBox

Now we are done with Encoding part. Let go code the Decoding part. Here it goes!

  • Copy what you wrote in btnEncode_Click event, double click on btnDecode and reach its Click event then paste your copied codes right here.
  • Change ‘+’ to ‘-‘ in (char)(rtbEncryption.Text[i] + 1) block and that is it! Waiting for more code? Nope, that was all we had to do. Because we got the next char value in encoding and all we want to get is the previous value of the current char.

Before completing our application, go to the click event of btnClear and write as in the picture:

Line 44: Clear text

Line 45: Focus on RichTextBox control

Yee haw! Now our application is done! Here is a screen output.

[Note: We are going to develop this project. It is going to be a little complicated. So get ready for it ! 🙂 ]

Leave a Reply

Your email address will not be published. Required fields are marked *