How to encrypt decrypt for storage of sensitive data asp net web api project

Introduction

When working on an ASP.NET web API , it is crucial to ensure the of sensitive data. One common requirement is to encrypt and decrypt data before storing it in a database or any other medium. In this article, we will explore various techniques and best practices for encrypting and decrypting sensitive data in an ASP.NET web API project.

Using Symmetric Encryption

Symmetric encryption is a technique where the same key is used for both encryption and . This approach is suitable when the data needs to be encrypted and decrypted the same application or system.

To encrypt sensitive data using symmetric encryption in ASP.NET, you can use the System.Security.Cryptography namespace. Here's an example:


using System;
using System.Security.Cryptography;
using System.Text;

 static string Encrypt(string plainText, string key)
{
    byte[] iv = new byte[16];
    byte[] ;

    using (Aes aes = Aes.())
    {
        aes.Key = Encoding.UTF8.GetBytes(key);
        aes.IV = iv;

        ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
                {
                    streamWriter.Write(plainText);
                }

                array = memoryStream.ToArray();
            }
        }
    }

    return Convert.ToBase64String(array);
}

To decrypt the encrypted data, you can use the following code:


public static string Decrypt(string cipherText, string key)
{
    byte[] iv = new byte[16];
    byte[] buffer = Convert.FromBase64String(cipherText);

    using (Aes aes = Aes.Create())
    {
        aes.Key = Encoding.UTF8.GetBytes(key);
        aes.IV = iv;

        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

        using (MemoryStream memoryStream = new MemoryStream(buffer))
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader streamReader = new StreamReader(cryptoStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }
    }
}

Using Asymmetric Encryption

Asymmetric encryption, also known as public-key encryption, uses two different keys for encryption and decryption. This approach is suitable when the data needs to be encrypted in one application or system and decrypted in another.

In ASP.NET, you can use the System.Security.Cryptography namespace to perform asymmetric encryption. Here's an example:


using System;
using System.Security.Cryptography;
using System.Text;

public static string Encrypt(string plainText, string publicKey)
{
    byte[] data = Encoding.UTF8.GetBytes(plainText);

    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
    {
        rsa.FromXmlString(publicKey);

        byte[] encryptedData = rsa.Encrypt(data, true);

        return Convert.ToBase64String(encryptedData);
    }
}

To decrypt the encrypted data, you can use the following code:


public static string Decrypt(string cipherText, string privateKey)
{
    byte[] data = Convert.FromBase64String(cipherText);

    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
    {
        rsa.FromXmlString(privateKey);

        byte[] decryptedData = rsa.Decrypt(data, true);

        return Encoding.UTF8.GetString(decryptedData);
    }
}

Conclusion

Encrypting and decrypting sensitive data is a crucial aspect of securing an ASP.NET web API project. By using symmetric or asymmetric encryption techniques, you can ensure that the data remains during storage. Remember to choose the appropriate encryption method on your specific requirements and consider other security measures such as key management and secure storage of encryption keys.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents