Here I am storing the values in database using LINQ queries as encrypted form.

We can encrypt or decrypt values using other algorithms.

But here, I am using SALT to encrypt and decrypt the values.

Step 1:

  1. public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
  2. {
  3. byte[] encryptedBytes = null;
  4. // Set your salt here, change it to meet your flavor:
  5. byte[] saltBytes = passwordBytes;
  6. // Example:
  7. //saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  8. using (MemoryStream ms = new MemoryStream())
  9. {
  10. using (RijndaelManaged AES = new RijndaelManaged())
  11. {
  12. AES.KeySize = 256;
  13. AES.BlockSize = 128;
  14. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  15. AES.Key = key.GetBytes(AES.KeySize / 8);
  16. AES.IV = key.GetBytes(AES.BlockSize / 8);
  17. AES.Mode = CipherMode.CBC;
  18. using (CryptoStream cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
  19. {
  20. cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
  21. cs.Close();
  22. }
  23. encryptedBytes = ms.ToArray();
  24. }
  25. }
  26. return encryptedBytes;
  27. }
  28. public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  29. {
  30. try
  31. {
  32. byte[] decryptedBytes = null;
  33. // Set your salt here to meet your flavor:
  34. byte[] saltBytes = passwordBytes;
  35. // Example:
  36. //saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  37. using (MemoryStream ms = new MemoryStream())
  38. {
  39. using (RijndaelManaged AES = new RijndaelManaged())
  40. {
  41. AES.KeySize = 256;
  42. AES.BlockSize = 128;
  43. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  44. AES.Key = key.GetBytes(AES.KeySize / 8);
  45. AES.IV = key.GetBytes(AES.BlockSize / 8);
  46. //AES.Mode = CipherMode.CBC;
  47. using (CryptoStream cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
  48. {
  49. cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  50. //If(cs.Length = ""
  51. cs.Close();
  52. }
  53. decryptedBytes = ms.ToArray();
  54. }
  55. }
  56. return decryptedBytes;
  57. }
  58. catch (Exception Ex)
  59. {
  60. return null;
  61. }
  62. }
  63. public string Encrypt(string text, string pwd)
  64. {
  65. byte[] originalBytes = Encoding.UTF8.GetBytes(text);
  66. byte[] encryptedBytes = null;
  67. byte[] passwordBytes = Encoding.UTF8.GetBytes(pwd);
  68. // Hash the password with SHA256
  69. passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  70. // Getting the salt size
  71. int saltSize = GetSaltSize(passwordBytes);
  72. // Generating salt bytes
  73. byte[] saltBytes = GetRandomBytes(saltSize);
  74. // Appending salt bytes to original bytes
  75. byte[] bytesToBeEncrypted = new byte[saltBytes.Length + originalBytes.Length];
  76. for (int i = 0; i < saltBytes.Length; i++)
  77. {
  78. bytesToBeEncrypted[i] = saltBytes[i];
  79. }
  80. for (int i = 0; i < originalBytes.Length; i++)
  81. {
  82. bytesToBeEncrypted[i + saltBytes.Length] = originalBytes[i];
  83. }
  84. encryptedBytes = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
  85. return Convert.ToBase64String(encryptedBytes);
  86. }
  87. public string Decrypt(string decryptedText, string pwd)
  88. {
  89. byte[] bytesToBeDecrypted = Convert.FromBase64String(decryptedText);
  90. byte[] passwordBytes = Encoding.UTF8.GetBytes(pwd);
  91. // Hash the password with SHA256
  92. passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  93. byte[] decryptedBytes = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
  94. if (decryptedBytes != null)
  95. {
  96. // Getting the size of salt
  97. int saltSize = GetSaltSize(passwordBytes);
  98. // Removing salt bytes, retrieving original bytes
  99. byte[] originalBytes = new byte[decryptedBytes.Length - saltSize];
  100. for (int i = saltSize; i < decryptedBytes.Length; i++)
  101. {
  102. originalBytes[i - saltSize] = decryptedBytes[i];
  103. }
  104. return Encoding.UTF8.GetString(originalBytes);
  105. }
  106. else
  107. {
  108. return null;
  109. }
  110. }
  111. private int GetSaltSize(byte[] passwordBytes)
  112. {
  113. var key = new Rfc2898DeriveBytes(passwordBytes, passwordBytes, 1000);
  114. byte[] ba = key.GetBytes(2);
  115. StringBuilder sb = new StringBuilder();
  116. for (int i = 0; i < ba.Length; i++)
  117. {
  118. sb.Append(Convert.ToInt32(ba[i]).ToString());
  119. }
  120. int saltSize = 0;
  121. string s = sb.ToString();
  122. foreach (char c in s)
  123. {
  124. int intc = Convert.ToInt32(c.ToString());
  125. saltSize = saltSize + intc;
  126. }
  127. return saltSize;
  128. }
  129. public byte[] GetRandomBytes(int length)
  130. {
  131. byte[] ba = new byte[length];
  132. RNGCryptoServiceProvider.Create().GetBytes(ba);
  133. return ba;
  134. }
  135. public bool Add(Movy movie)
  136. {
  137. demo.Movies.InsertOnSubmit(movie);
  138. demo.SubmitChanges();
  139. return true;
  140. }
Step 2: Call that methods in .cs file.
  1. public partial class Add: System.Web.UI.Page
  2. {
  3. /// <summary>
  4. /// Linq To SQL : Connection object.
  5. /// </summary>
  6. DemoDataContext demo = new DemoDataContext();
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9. }
  10. /// <summary>
  11. /// Button submit click event : Storing value in database as encrypted form
  12. /// </summary>
  13. /// <param name="sender"></param>
  14. /// <param name="e"></param>
  15. protected void btnSubmit_Click(object sender, EventArgs e) {
  16. try {
  17. if (txtName.Text != "")
  18. {
  19. var m = new Movy
  20. {
  21. MovieName = new SecurityClass().Encrypt(txtName.Text, txtName.Text),
  22. };
  23. new SecurityClass().Add(m);
  24. lblMessgae.Text = "Submitted successfully.";
  25. txtName.Text = "";
  26. }
  27. }
  28. catch (Exception ex)
  29. {
  30. ex.Message.ToString();
  31. }
  32. }
  33. protected void btnSearch_Click(object sender, EventArgs e) {
  34. try {
  35. if (txtSearchByName.Text != "")
  36. {
  37. var movienameEncrypted = new SecurityClass().Encrypt(txtName.Text, txtName.Text);
  38. if (movienameEncrypted != null)
  39. {
  40. string movienameDecrypt = new SecurityClass().Decrypt(movienameEncrypted, txtSearchByName.Text);
  41. if (movienameDecrypt != null)
  42. {
  43. var Details = (from m in demo.Movies where movienameDecrypt == txtSearchByName.Text select new
  44. {
  45. UserName = m.Id
  46. })
  47. .FirstOrDefault();
  48. if (Details != null)
  49. {
  50. lblMessgae.Text = "Movie exist into the database.";
  51. }
  52. else
  53. {
  54. lblMessgae.Text = "Not exist.";
  55. }
  56. }
  57. else
  58. {
  59. lblMessgae.Text = "Not exist.";
  60. }
  61. }
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. ex.Message.ToString();
  67. }
  68. }
Screen:

output