Sunday, 26 August 2018

Data Encryption or Dencryption Console App using C#

Data Encryption or Dencryption Console App using C#

App.Config


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SecurityKey" value="0123456789qwertyuiopasdfghjklmnbvcxz"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

Main Program.CS

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EncryptionAndDencryptionConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "hello";
            string b = EncryptionAndDencryption.Encrypt(a);
            string c = EncryptionAndDencryption.Decrypt(b);
            Console.WriteLine("Data->"+a);
            Console.WriteLine("After encryption->" + b); 
            Console.WriteLine("After dencryption->" + c);
            Console.ReadLine();
        }
    }
}

Class EncryptionAndDencryption


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EncryptionAndDencryptionConsoleApp
{
   public static class EncryptionAndDencryption
    {

//Method for Encrypt the String which is passed as parameter:

        public static string Encrypt(string toEncrypt)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            string key = (string)settingsReader.GetValue("SecurityKey",typeof(String));
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
             TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
             tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0,toEncryptArray.Length);
            tdes.Clear();
                   return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

//Method for Dencrypt the String which is passed as parameter.

        public static string Decrypt(string cipherString)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(cipherString);
            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            string key = (string)settingsReader.GetValue("SecurityKey",typeof(String));
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }
}