This article will teach you about Advanced Encryption Standard (AES) in C #. I think I will start a small series using some cryptography with AES in C #. Cryptography and Encryption are used by most developers who work with enterprise applications, especially if you work in the financial services industry. You can try to decrypt C# examples and aes encrypt C# examples at the end of the article step by step.
Cryptography is an interesting subject and the design of this algorithm is very interesting; I do not recommend using an algorithm that you have designed yourself. The algorithm in practice has now been analyzed many times by experts in both private and government industries around the world who are trying to find faults and weaknesses, so you are far better off using this recommended system.
The main algorithms fall into two categories: Symmetric encryption and Asymmetric encryption. Symmetric encryption contains algorithms that are based solely on an encryption key. For example, if you encrypt some plaintext with Key1 you get a cipher text out the other end. If you then decrypt the cipher text with the same key (Key1) you will get back to the original plaintext.
Asymmetric encryption works by having 2 keys, a public and private key. These keys are mathematically derived from each other. The public key can be used by anyone and the private key has to be kept secret. I will talk about asymmetric encryption and more specifically RSA in another post.
For this article I am going to look at the AES symmetric algorithm. AES stands for the Advanced Encryption Standard. This was a competition winner when the National Institute of Standards and Technology ran a contest to replace the already broken DES algorithm.
What I will show in this article is a good practical implementation of AES in .NET. We will start with the following interface. The interface contains 2 methods, Encrypt and Decrypt. They methods take cipher text/plaintext and an encryption key. For example: AES_DECRYPT(fieldname, "yourkey")
In this tutorial we will practice examples of Decryption and Encryption using inserting and selection methods in Mysql. We will insert data from .Net to Mysql with Encryption and select data from Mysql with decryption.
Let’s following the steps for learning about Encryption and Decryption with AES:
  1. Create database in mysql with the name “test” and create a table with the name “title”. See below for an example.

Advanced Encryption Standard (AES) In C#
  1. Create a new application project. In Visual Studio, on the menu click File> New > Project. For more details, see the following menu on the display.
Advanced Encryption Standard (AES) In C#

  1. Then the New Project will appear, as below.

Advanced Encryption Standard (AES) In C#

  1. Write down the name of the project that will be created on a field name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.
Advanced Encryption Standard (AES) In C#

  1. Create a new windows form like the below.

Advanced Encryption Standard (AES) In C#

Create a new class for connecting the database and write the following program listing :
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MySql.Data.MySqlClient;
  6. using System.Windows.Forms;
  7. using System.Data;
  8. namespace Ecnryption_Decryption
  9. {
  10. class connection
  11. {
  12. MySql.Data.MySqlClient.MySqlConnection conn;
  13. string myConnectionString;
  14. static string host = "localhost";
  15. static string database = "test";
  16. static string userDB = "root";
  17. static string password = "password";
  18. public static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
  19. public bool Open()
  20. {
  21. try
  22. {
  23. strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
  24. conn = new MySqlConnection(strProvider);
  25. conn.Open();
  26. return true;
  27. }
  28. catch (Exception er)
  29. {
  30. MessageBox.Show("Connection Error ! " + er.Message, "Information");
  31. }
  32. return false;
  33. }
  34. public void Close()
  35. {
  36. conn.Close();
  37. conn.Dispose();
  38. }
  39. public DataSet ExecuteDataSet(string sql)
  40. {
  41. try
  42. {
  43. DataSet ds = new DataSet();
  44. MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
  45. da.Fill(ds, "result");
  46. return ds;
  47. }
  48. catch (Exception ex)
  49. {
  50. MessageBox.Show(ex.Message);
  51. }
  52. return null;
  53. }
  54. public MySqlDataReader ExecuteReader(string sql)
  55. {
  56. try
  57. {
  58. MySqlDataReader reader;
  59. MySqlCommand cmd = new MySqlCommand(sql, conn);
  60. reader = cmd.ExecuteReader();
  61. return reader;
  62. }
  63. catch (Exception ex)
  64. {
  65. MessageBox.Show(ex.Message);
  66. }
  67. return null;
  68. }
  69. public int ExecuteNonQuery(string sql)
  70. {
  71. try
  72. {
  73. int affected;
  74. MySqlTransaction mytransaction = conn.BeginTransaction();
  75. MySqlCommand cmd = conn.CreateCommand();
  76. cmd.CommandText = sql;
  77. affected = cmd.ExecuteNonQuery();
  78. mytransaction.Commit();
  79. return affected;
  80. }
  81. catch (Exception ex)
  82. {
  83. MessageBox.Show(ex.Message);
  84. }
  85. return -1;
  86. }
  87. }
  88. }
In the next step, go back to the windows form and view code to write the following program listing:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10. using System.Data;
  11. using System.Collections;
  12. namespace Ecnryption_Decryption
  13. {
  14. public partial class Form1 : Form
  15. {
  16. connection con = new connection();
  17. //arraylist to getter and setter data
  18. private static ArrayList ListID = new ArrayList();
  19. private static ArrayList ListName = new ArrayList();
  20. private static ArrayList Listtitle = new ArrayList();
  21. private static ArrayList ListAddress = new ArrayList();
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. GetData();
  29. updateDatagrid();
  30. }
  31. private void GetData()
  32. {
  33. try
  34. {
  35. con.Open();
  36. string query = "select id as id,AES_DECRYPT(name,'camellabs.com') as name,AES_DECRYPT(title,'camellabs.com') as title,AES_DECRYPT(address,'camellabs.com') as address from title";
  37. //MySqlDataReader row;
  38. MySqlDataReader row;
  39. row = con.ExecuteReader(query);
  40. if (row.HasRows)
  41. {
  42. while (row.Read())
  43. {
  44. ListID.Add(row["id"].ToString());
  45. ListName.Add(row["name"].ToString());
  46. Listtitle.Add(row["title"].ToString());
  47. ListAddress.Add(row["address"].ToString());
  48. }
  49. }
  50. else
  51. {
  52. MessageBox.Show("Data not found");
  53. }
  54. con.Close();
  55. }
  56. catch (Exception err)
  57. {
  58. MessageBox.Show(err.ToString());
  59. }
  60. }
  61. private void button1_Click(object sender, EventArgs e)
  62. {
  63. try
  64. {
  65. con.Open();
  66. string query = "insert into title (name,title,address) values(AES_ENCRYPT('" + textBox1.Text + "','camellabs.com'),AES_ENCRYPT('" + textBox2.Text + "','camellabs.com'),AES_ENCRYPT('" + textBox3.Text + "','camellabs.com'))";
  67. int hasil = con.ExecuteNonQuery(query);
  68. if (hasil > 0)
  69. {
  70. MessageBox.Show("Data has been save Success");
  71. clearList();
  72. GetData();
  73. updateDatagrid();
  74. }
  75. else
  76. {
  77. MessageBox.Show("Failed to saving data");
  78. }
  79. con.Close();
  80. }
  81. catch (Exception err)
  82. {
  83. MessageBox.Show(err.ToString());
  84. }
  85. }
  86. private void updateDatagrid()
  87. {
  88. dataGridView1.Rows.Clear();
  89. for (int i = 0; i < ListID.Count; i++)
  90. {
  91. DataGridViewRow newRow = new DataGridViewRow();
  92. newRow.CreateCells(dataGridView1);
  93. newRow.Cells[0].Value = ListID[i];
  94. newRow.Cells[1].Value = ListName[i];
  95. newRow.Cells[2].Value = Listtitle[i];
  96. newRow.Cells[3].Value = ListAddress[i];
  97. dataGridView1.Rows.Add(newRow);
  98. }
  99. }
  100. private void clearList()
  101. {
  102. ListID.Clear();
  103. ListName.Clear();
  104. Listtitle.Clear();
  105. ListAddress.Clear();
  106. }
  107. }
  108. }
After you write down the program listings, press the F5 key to run the program and if you're successful connect your database. The result is:

Advanced Encryption Standard (AES) In C#

Advanced Encryption Standard (AES) In C#
You can see Advanced Encryption Standard (AES) in C# from Github project in Here.
Thank you for reading this article about Advanced Encryption Standard (AES) in C#, I hope this article is useful for you. Visit My Github about .Net Csharp here.