Cryptography Utility

There are endless ways of making your code secure and cryptography is a vast subject with numerous algorithms, options and levels of security you can achieve.

However, if you are just looking for something quick, and perhaps your main agenda here is to only convert your clear text into an alphanumeric string alone, here’s a utility for you, which also accounts for cipher expiry.

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Crypto
{
    class Program
    {
        public static void Main()
        {
            string text = "clear text to be encrypted (#&(^(*&&$@^*&^$%^*%&"
                + DateTime.UtcNow.AddSeconds(60).ToString("MMddyyyyHHmmss");
            Console.WriteLine("256:" + Decrypt256(Encrypt256(text)));
            Console.WriteLine(Encrypt256(text));
        }

        private const string AesIV256 = @"!QAZ2WSX#EDC4RFV";
        private const string AesKey256 = @"q3t6w9z$C&F)J@McQfTjWnZr4u7x!A%D";

        public static string Encrypt256(string text)
        {
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider
            {
                BlockSize = 128,
                KeySize = 256,
                IV = Encoding.UTF8.GetBytes(AesIV256),
                Key = Encoding.UTF8.GetBytes(AesKey256),
                Mode = CipherMode.CBC,
                Padding = PaddingMode.PKCS7
            };

            byte[] src = Encoding.Unicode.GetBytes(text);

            using (ICryptoTransform encrypt = aes.CreateEncryptor())
            {
                byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
                return Convert.ToBase64String(dest);
            }
        }

        public static string Decrypt256(string text)
        {
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider
            {
                BlockSize = 128,
                KeySize = 256,
                IV = Encoding.UTF8.GetBytes(AesIV256),
                Key = Encoding.UTF8.GetBytes(AesKey256),
                Mode = CipherMode.CBC,
                Padding = PaddingMode.PKCS7
            };

            byte[] src = Convert.FromBase64String(text);

            using (ICryptoTransform decrypt = aes.CreateDecryptor())
            {
                byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
                string decrypted = Encoding.Unicode.GetString(dest);

                NameValueCollection qs = HttpUtility.ParseQueryString(decrypted);
                DateTime expiryDateTime;
                DateTime.TryParseExact(qs["timestamp"], "MMddyyyyHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out expiryDateTime);
                Console.WriteLine(expiryDateTime > DateTime.UtcNow ? "Cipher not expired!" : "Cipher Expired!");

                return decrypted;
            }
        }
    }
}

This snippet uses Advanced Encryption Standard (AES).
In present day cryptography, AES is widely adopted and supported in both hardware and software. Till date, no practical cryptanalytic attacks against AES has been discovered. Additionally, AES has built-in flexibility of key length, which allows a degree of ‘future-proofing’ against progress in the ability to perform exhaustive key searches.
(Source: https://www.tutorialspoint.com/cryptography/advanced_encryption_standard.htm)

Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s