How to Encrypt a Text using C#

In this tutorial, we will create a program that encrypts a text using C#. We know that encryption is the translation of data into a secret code. Encryption is the most effective way to achieve data security. And for now we will create a program that can encrypt an inputted text. Now, let's start this Encryption tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Encrypt String. 2. Next, add one TextBox name TextBox1 for inputting a text, one label named lblOutput that will be used for displaying the output, and one Button named Button1 for encrypting the inputted text and will be displayed in the Label. You must design your interface like this: design 3. To achieve that, first of all we need to include the following namespace in the program code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10. using System.Security.Cryptography;
  11. using System.Text;
We have imported System.Security.Cryptography and System.Text. The System.Security.Cryptography namespace provides cryptographic services, including secure encoding and decoding of data, as well as many other operations, such as hashing, random number generation, and message authentication. And the System.Text namespace contains classes that represent ASCII and Unicode character encodings; abstract base classes for converting blocks of characters to and from blocks of bytes; and a helper class that manipulates and formats String objects without creating intermediate instances of String. 5. Now to encrypt a text, put this code below for Button1.
  1. public void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3. MD5CryptoServiceProvider encryptMD5 = new MD5CryptoServiceProvider();
  4. byte[] encryptedText = null;
  5. UTF8Encoding encoder = new UTF8Encoding();
  6. encryptedText = encryptMD5.ComputeHash(encoder.GetBytes(TextBox1.Text));
  7. lblOuput.Text = Convert.ToBase64String(encryptedText);
  8. lblOuput.Enabled = true;
  9. }
We initialized variable encryptMD5 As New MD5CryptoServiceProvider() to provide encryption or cryptographic services, variable encryptedText As Byte() to get the Byte of a certain text, and variable encoder As New UTF8Encoding() for using a Unicode character encodings. Next, the encryptedText gets the byte of the Hash of encryptMD5 and the UTF8Encoding of the TextBox1. Then the output will display at lblOutput that the encryptedText was converted into Base64String. Press F5 to run the program. Output: output Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10. using System.Security.Cryptography;
  11. using System.Text;
  12.  
  13. namespace Encrypt_String
  14. {
  15. public partial class Form1
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20.  
  21.  
  22. }
  23.  
  24. public void Button1_Click(System.Object sender, System.EventArgs e)
  25. {
  26. MD5CryptoServiceProvider encryptMD5 = new MD5CryptoServiceProvider();
  27. byte[] encryptedText = null;
  28. UTF8Encoding encoder = new UTF8Encoding();
  29. encryptedText = encryptMD5.ComputeHash(encoder.GetBytes(TextBox1.Text));
  30. lblOuput.Text = Convert.ToBase64String(encryptedText);
  31. lblOuput.Enabled = true;
  32. }
  33. }
  34.  
  35. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment