String Encryption/Decryption C# code example

String Encryption/Decryption C# code example

Encrypt/Decrypt string using C#

Modern technology and modern ways to hack your information both runs parallel. When it comes to secure the data we as a developer always make sure the code we are writing to our most trusted clients or stackholders must be hack-proof and easy to implement.
In this post, I would like to discuss how to securely transport your data over the internet. Encryption is the key to make your data secure, There are several ways to encrypt the string and here we will take some examples to learn encryption/decryption using most secure method using RijndaelManaged.

Code Example:

using System;
using System.IO;
using System.Security.Cryptography;

class Rijndael
{
	public static void Main()
	{
		try
		{

			string myStringToSecure = "Data For Encryption!!!!!";
			using (RijndaelManaged objRij = new RijndaelManaged())
			{

				objRij.GenerateKey();
				//If you wish to have your own key to secure data you can replace above statement and get your own key with below commented code
				//objRij.Key=Encoding.UTF8.GetBytes("yourkeystring or any password");
				objRij.GenerateIV();
				byte[] encrypted = EncryptString(myStringToSecure, objRij.Key, objRij.IV); //This code will encrypt your string
				string strDecrypt = DecryptStringFromBytes(encrypted, objRij.Key, objRij.IV);//This code will decrypt your string
				Console.WriteLine("Original:   {0}", myStringToSecure);
				Console.WriteLine("Post Decryption: {0}", strDecrypt);
			}

		}
		catch (Exception e)
		{
			Console.WriteLine("Error: {0}", e.Message);
		}
	}
	static byte[] EncryptString(string textTobeEncrypted, byte[] Key, byte[] IV)
	{
		if (textTobeEncrypted == null || textTobeEncrypted.Length <= 0)
			throw new ArgumentNullException("textTobeEncrypted");
		if (Key == null || Key.Length <= 0)
			throw new ArgumentNullException("Key");
		if (IV == null || IV.Length <= 0)
			throw new ArgumentNullException("Key");
		byte[] encrypted; 
		using (RijndaelManaged objAg = new RijndaelManaged())
		{
			objAg.Key = Key;
			objAg.IV = IV;
			ICryptoTransform encryptor = objAg.CreateEncryptor(objAg.Key, objAg.IV); 
			using (MemoryStream msEncrypt = new MemoryStream())
			{
				using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
				{
					using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
					{
						swEncrypt.Write(textTobeEncrypted);
					}
					encrypted = msEncrypt.ToArray();
				}
			}
		}
		return encrypted;
	}
	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");
		if (IV == null || IV.Length <= 0)
			throw new ArgumentNullException("Key"); 
		string textTobeEncrypted = null; 
		using (RijndaelManaged objAg = new RijndaelManaged())
		{
			objAg.Key = Key;
			objAg.IV = IV;
			ICryptoTransform decryptor = objAg.CreateDecryptor(objAg.Key, objAg.IV); 
			using (MemoryStream mDecrypt = new MemoryStream(cipherText))
			{
				using (CryptoStream cDecrypt = new CryptoStream(mDecrypt, decryptor, CryptoStreamMode.Read))
				{
					using (StreamReader srDecrypt = new StreamReader(cDecrypt))
					{
						textTobeEncrypted = srDecrypt.ReadToEnd();
					}
				}
			}

		}

		return textTobeEncrypted;
	 }
}

Calling methods:

EncryptString
DecryptStringFromBytes

Next>>Perform Custom VSTO Add-In Installation using InstallShield Limited Edition

Leave a Reply

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