top of page

C# Tutorial - Message Encryption App

  • Rhys Ramsay
  • Feb 7, 2017
  • 3 min read

Ready to feel like MI5 agent James Bond? In this quick and easy tutorial, I'll be showing you how to release your inner super spy by creating a fun Windows forms encryption application in C# which allows you to make secret, codified messages you can share with your friends.

You will require a copy of Visual Studio 2015 or later for this tutorial, you can download VS Community 2015 by clicking on this link:

Once you've fired up Visual Studio, you'll want to click on 'New Project' then navigate to Templates > VisualC# and select the Windows Forms Application template. Change the project name to MessageEncryption and hit OK.

This should take you to the Form1.cs design page, first of all we need to head to Form Properties > Text and change this value to display 'Message Encryption App'. Next, we need to scroll down to Form Size and set the values in here to 445 by 496.

The next step is to open up the Toolbox on the left hand side and select the following:

2 RichTextBoxes,

2 Buttons,

1 Label

and 1 NumericUpDown Box

Now set the following values:

RichTextBox1 'Name' = rtbMessage

Size = 422, 162

Location = 0, 59

RichTextBox2 'Name' = rtbEncryption

Size = 422, 162

Location = 0, 256

Button1 'Name' = btnEncrypt

Text = Encrypt Message

Size = 167, 23

Location = 0, 227

Button 2 'Name' = btnDecipher

Text = Decipher Message

Size = 167, 23

Location = 0, 424

Label 'Text' = Set Encryption Key:

NumericUpDown1 'Name' = nudEncryptionKey

Ok, now we have the layout of our form it's time to add some funcionality! Double click on the 'Encrypt Message' button we created for our form, this should take you to the code view of Form1.cs and generate a new function for our button.

Add the following code inside the function:

private void btnEncrypt_Click(object sender, EventArgs e)

{

rtbEncryption.Text = "";

foreach (char c in rtbMessage.Text)

{

char encrypted = (char)(c + nudEncryptionKey.Value);

rtbEncryption.Text += encrypted.ToString();

}

Basically we're telling our form that rtbEncryption should be blank by default and that there shouldn't be any text in this field until we hit the button to encrypt our message. The code which actually encrypts the message the user inputs into the rtbMessage field starts at foreach, this essentially creates an object called 'encrypted' as a char datatype, we then add the value of our NumericUpDown box or, our encryption key and increment the characters in the message by this value. For example, if the user inputs the letter 'H' and the encryption key is '1' then the application will output the letter 'I' in rtbEncryption. The final line states that once the nudEncryptionKey value has been added to the characters of our message, we then want the encrypted object characters to be output as a string in rtbEncryption.

Now when you run the app you should be able to type a message into the first RichtextBox, rtbMessage and hit 'Encrypt Message' to jumble it up. We also want our user to be able to share his encryption key with a buddy so that he can send messages which his friend can then paste into rtbEncryption and decipher. To do this, double click on btnDecipher in Form1.cs[Design].

Again, we will be taken to the code for Form1.cs. Now there should be another function for btnDecipher, we are taken the same code from before and adding it here only this time in reverse as follows:

private void btnEncrypt_Click(object sender, EventArgs e)

{

foreach (char c in rtbEncryption.Text)

{

char encrypted = (char)(c - nudEncryptionKey.Value);

rtbMessage.Text += encrypted.ToString();

}

So now, we are asking it to subtract the value of nudEncryptionKey from the characters in rtbEncryption and asking it instead to display the deciphered message in rtbMessage altough in order to be able to decipher a message, the encryption key must be set to the same value as when the message was originally created. This means it will be more difficult for anyone to intercept and decode your message as there are a greater range of values to choose from to encrypt the message.

That's all there is to it, I hope you've enjoyed this tutorial and found it useful. Watch this space as next time I'll be showing you guys how to make your very own Ticket Management System WinForms app, again in C#!

You can find the source code for this project here :

(You will have to change the namespace from Cipher to MessageEncryption depending on what you decided to name your project.)

Comments


RECENT POSTS

FEATURED POSTS

Check back soon
Once posts are published, you’ll see them here.

FOLLOW

ABOUT

My name's Rhys and I'm a Software Engineering student. I've worked in a wide variety of jobs but have always loved creating cool stuff with computers. I'm currently seeking opportunities within Software or Front-End Web Development, I'm also keen on maths, science, video games and playing bass guitar! 

LINKS

SUBSCRIBE 

Like what you see? Don't wanna miss another project? Subscribe today!

  • github-mark
  • YouTube Social  Icon
  • LinkedIn Social Icon
  • Google+ Social Icon

© 2017 by Rhys Ramsay. Created with Wix.com

bottom of page