In this article, we discuss encryption and decryption using C#. We are using the MD5 algorithm. The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities.
MD5 algorithm can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database. We use the MD5 hash generator as the result is a 128-bit byte array which is a valid length for the TripleDES encoder. We use below to declare the Global Variable.
We write encryption and decryption using the C# method here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | using System; using System.Net; using System.Security.Cryptography; using System.Text; namespace EncryptionDecryption { public static class UtilityClass { public static string EncryptString(String toEncrypt, bool useHashing) { byte[] keyArray; byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); string key = "SELIM_AHMED_1920"; if (useHashing) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); } else keyArray = 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); } public static string DecryptString(String cipherString, bool useHashing) { byte[] keyArray; byte[] toEncryptArray = Convert.FromBase64String(cipherString); string key = "SELIM_AHMED_1920"; if (useHashing) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); } else keyArray = UTF8Encoding.UTF8.GetBytes(key); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte[] resultArray = null; resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); tdes.Clear(); return UTF8Encoding.UTF8.GetString(resultArray); } } } |
Here, we write the code for check encryption and decryption using C#…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; namespace EncryptionDecryption { class Program { static void Main(string[] args) { String encryptedString = String.Empty; Console.WriteLine("Please enter a string for encryption"); var sUserInput = Console.ReadLine(); encryptedString = UtilityClass.EncryptString(sUserInput, true);; Console.WriteLine($"Encrypted string = {encryptedString}"); String decryptedString = UtilityClass.DecryptString(encryptedString, true); Console.WriteLine($"Decrypted string = {decryptedString}"); Console.ReadKey(); } } } |
Encryption and Decryption using C# Encryption and Decryption using C#
The article was published on July 3, 2014 @ 1:22 PM
Leave a Comment