Writing Simple Encrypt and Decrypt Utility Methods in C#
by Jeffrey Sward
 
Requirements
 
  • Encryption for visual inspection only (human pencil-and-paper)
  • Does not need to be secure from high volume automatic attack
  • Simple method calls: pass plain text string, return cipher text string, and pass cipher text string, return plain text string
  • Wrap in utility form for reuse
 
Issues
 
  • System.Security.Cryptography is very versatile, but requires excessive parameterization
  • Cryptographic functions operate on byte array of integers rather than string of ASCII characters
  • Conversion from string to byte array necessary
  • Conversion from ASCII to binary byte necessary
  • Cryptographic functions operate on memory stream objects rather than simple strings
  • Two arrays of integers are needed in addition to text: key and initialization vector
 
Solution
 
  • Select AES method because of least amount of complexity from options in System.Security.Cryptography
  • Hard code constant arrays of byte integers fro key and initialization vector. This is sufficient for protection from human pencil-and-paper encryption
  • Simple string-to-string methods for encryption and decryption
  • All conversions handled within methods, allowing for passing and receiving plain text strings and cipher strings only.
 
Code Sample Encrypt
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography;
using System.IO;

public static String Encrypt(String plainTextString)
    {
      String cipherTextString = String.Empty;
      try
      {
        WebUtilities.GenericLogHandler.Info
          (
               WebUtilities.GlobalConstants.Entry
             + typeof(GenericCryptographicHandler).FullName.ToString()
             + WebUtilities.GlobalConstants.Dot
             + MethodBase.GetCurrentMethod().Name
          );
        WebUtilities.GenericLogHandler.Debug
          (
              WebUtilities.GlobalConstants.PlainText
            + plainTextString
          );
        byte[] plainTextByte = Encoding.UTF8.GetBytes(plainTextString);
        byte[] cipherTextByte = null;
        using (Aes cryptographicAlgorithm = Aes.Create())
        {
          using (ICryptoTransform encryptor
            = cryptographicAlgorithm.CreateEncryptor
              (GlobalConstants.DefaultCryptographicKey
             , GlobalConstants.DefaultCryptographicInitializationVector))
          {
            MemoryStream encryptMemoryStream = new MemoryStream();
            using (Stream encryptCryptoStream
              = new CryptoStream(encryptMemoryStream, encryptor, CryptoStreamMode.Write))
            {
              encryptCryptoStream.Write(plainTextByte, 0, plainTextByte.Length);
            }
            cipherTextByte = encryptMemoryStream.ToArray();
          }
        }
        cipherTextString = Convert.ToBase64String(cipherTextByte);
        if (cipherTextString == null)
        {
          cipherTextString = GlobalConstants.Null;
        }
        WebUtilities.GenericLogHandler.Debug
          (
               WebUtilities.GlobalConstants.CipherText
             + cipherTextString
           );
        WebUtilities.GenericLogHandler.Info
          (
               WebUtilities.GlobalConstants.Exit
             + typeof(GenericCryptographicHandler).FullName.ToString()
             + WebUtilities.GlobalConstants.Dot
             + MethodBase.GetCurrentMethod().Name
          );
      }
      catch (Exception ex)
      {
        WebUtilities.GenericErrorHandler.HandleError(ex);
      }
     return cipherTextString;
    }

 
Code Sample Decrypt
 

    public static String Decrypt(String cipherTextString)
    {
      String plainTextString = String.Empty;
      try
      {
        WebUtilities.GenericLogHandler.Info
          (
               WebUtilities.GlobalConstants.Entry
             + typeof(GenericCryptographicHandler).FullName.ToString()
             + WebUtilities.GlobalConstants.Dot
             + MethodBase.GetCurrentMethod().Name
          );
        WebUtilities.GenericLogHandler.Debug
          (
              WebUtilities.GlobalConstants.CipherText
            + cipherTextString
          );
        byte[] cipherTextByte = Convert.FromBase64String(cipherTextString);
        byte[] plainTextByte = null;
        using (Aes cryptographicAlgorithm = Aes.Create())
        {
          using (ICryptoTransform decryptor
            = cryptographicAlgorithm.CreateDecryptor
              (GlobalConstants.DefaultCryptographicKey
             , GlobalConstants.DefaultCryptographicInitializationVector))
          {
            MemoryStream decryptMemoryStream = new MemoryStream();
            using (Stream decryptCryptoStream
              = new CryptoStream(decryptMemoryStream, decryptor, CryptoStreamMode.Write))
            {
              decryptCryptoStream.Write(cipherTextByte, 0, cipherTextByte.Length);
            }
            plainTextByte = decryptMemoryStream.ToArray();
          }
        }
        plainTextString = Encoding.UTF8.GetString(plainTextByte);
        if (plainTextString == null)
        {
          plainTextString = GlobalConstants.Null;
        }
        WebUtilities.GenericLogHandler.Debug
          (
               WebUtilities.GlobalConstants.CipherText
             + cipherTextString
           );
        WebUtilities.GenericLogHandler.Info
          (
               WebUtilities.GlobalConstants.Exit
             + typeof(GenericCryptographicHandler).FullName.ToString()
             + WebUtilities.GlobalConstants.Dot
             + MethodBase.GetCurrentMethod().Name
          );
      }
      catch (Exception ex)
      {
        WebUtilities.GenericErrorHandler.HandleError(ex);
      }
      return plainTextString;
    }

 
Code Sample Cryptographic Constants
 

// use random numbers between 0 and 255 rather than the values shown

public static readonly byte[] DefaultCryptographicKey = {1 ,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

public static readonly byte[] DefaultCryptographicInitializationVector = {101 ,102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116};

 
 

All written content of this web site is solely the editorial opinion of Jeffrey Sward. All images, graphics, and written content of this web site, including the html files, are creative products covered by copyright law. All content copyright Jeffrey Sward 1975-2019. All rights reserved. No portion of this web site or its constituent elements may be reproduced in any form, by any means, without prior written permission. So there.