PII (Personally Identifiable Information) is used to identify individual identity markers such as SSN, DOB, Bank Account Number, etc. When you are working with a financial application, you face a scenario where you have to store the data in an encrypted format so that no one can see the actual data.
While showing this data on the UI screen, it needs to be decrypted and masked as well. It means we need to mask the PII data before sending it to the UI. As you have seen before, your account number, credit card number, mobile number, and SSN, all are obfuscated (ex-Mobile SSN- XXXXX2398).
To secure the PII data in the database, the following technique can be used.
- .NET Assembly
- CLE (Column-level Encryption)
- TDE (Transparent Data Encryption)
In this article, we will see how we can use the .NET assembly in SQL Server to secure the information.
Rijndael algorithm is used in the below example. To encrypt and decrypt, we use cryptography concepts, and there are many algorithms that are used to encrypt and decrypt the data.
The only thing is that we need to secure the key. The key can be read from a flat file, app. config, etc.
I have hard-coded the key in the library file itself but you can use it as per your requirement. I have also used the console application to demonstrate whether the Encryption/Decryption is working or not. Once the library project is created, the assembly can be used in SQL Server.
Column Level Encryption with real time scenarios will be explained in the next article.
Step 1. Create your library project.

Step 2. Write the below code. So, your class will have two public methods: Encrypt and Decrypt.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EncryptDecrypt.Library
{
/// <summary>
///
/// </summary>
public sealed class AES_EncryptDecrypt
{
Static readonly string keyData = "su@@@###su&BLSKF";
#region Encrypt
/// <summary>
///
/// </summary>
/// <param name="inputData"></param>
/// <param name="pass"></param>
/// <returns></returns>
public static string Encrypt(string inputData)
{
try
{
return Convert.ToBase64String(EncryptStringToBytes(inputData, Encoding.Default.GetBytes(keyData)));
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
///
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] EncryptStringToBytes(string plainText, byte[] key)
{
return EncryptStringToBytes(plainText, key, null);
}
/// <summary>
///
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <returns></returns>
public static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] IV)
{
if ((plainText == null) || (plainText.Length <= 0))
{
throw (new ArgumentNullException("PlainText"));
}
if ((key == null) || (key.Length <= 0))
{
throw (new ArgumentNullException("PlainText"));
}
RijndaelManaged rijManaged = new RijndaelManaged();
rijManaged.Key = key;
if (!(IV == null))
{
if (IV.Length > 0)
{
rijManaged.IV = IV;
}
else
{
rijManaged.Mode = CipherMode.ECB;
}
}
else
{
rijManaged.Mode = CipherMode.ECB;
}
byte[] encryptedData = null;
ICryptoTransform iCryptoTransform = rijManaged.CreateEncryptor();
encryptedData = iCryptoTransform.TransformFinalBlock(Encoding.Default.GetBytes(plainText), 0, plainText.Length);
return encryptedData;
}
#endregion
#region Decrypt
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="pass"></param>
/// <returns></returns>
public static string Decrypt(string inputData)
{
try
{
return DecryptStringFromBytes(Convert.FromBase64String(inputData), Encoding.Default.GetBytes(keyData));
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
///
/// </summary>
/// <param name="cipherText"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DecryptStringFromBytes(byte[] cipherText, byte[] key)
{
return DecryptStringFromBytes(cipherText, key, null);
}
/// <summary>
///
/// </summary>
/// <param name="cipherText"></param>
/// <param name="key"></param>
/// <param name="IV"></param>
/// <returns></returns>
public static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] IV)
{
if ((cipherText == null) || (cipherText.Length <= 0))
{
throw (new ArgumentNullException("cipherText"));
}
if ((key == null) || (key.Length <= 0))
{
throw (new ArgumentNullException("key"));
}
RijndaelManaged rijManaged = new RijndaelManaged();
rijManaged.Key = key;
rijManaged.Mode = CipherMode.CBC;
if (!(IV == null))
{
if (IV.Length > 0)
{
rijManaged.IV = IV;
}
else
{
rijManaged.Mode = CipherMode.ECB;
}
}
else
{
rijManaged.Mode = CipherMode.ECB;
}
string PlainData = null;
ICryptoTransform iCryptoTransform = rijManaged.CreateDecryptor(rijManaged.Key, rijManaged.IV);
MemoryStream msDecrypt = new MemoryStream(cipherText);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, iCryptoTransform, CryptoStreamMode.Read);
StreamReader srDecrypt = new StreamReader(csDecrypt);
PlainData = srDecrypt.ReadToEnd();
return PlainData;
}
#endregion
}
}






Daniel AravinthPosted Jul 31, 2020, 10:14 AM
How to store the key data as secure instead of using in the class file?
Sarang KanfadePosted Dec 14, 2017, 1:11 AM
Is this work same in MYSQL also and for a special character?