A guide to creating class libraries in c#: The Caesar Cipher part three

May 07 2012

A guide to creating class libraries in c#: The Caesar Cipher part three


Finally it is code time

The idea with a class library is that you define a bunch of public types, which are then available for use from any program that references the dll. We will call our class the “CaesarCipher” and it will have two simple methods, Encrypt(string plaintext) and Decrypt(string ciphertext).

Have a look at the codelisting:

using System.Text;

namespace CaesarLib
{
    public class CaesarCipher
    {
        private readonly int _shiftValue;
        private readonly string _alphabet;
        private readonly string _shiftedAlphabet; 

        public CaesarCipher(int shiftValue)
        {
            _shiftValue = (shiftValue % 26) ;

            _alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            var alphabetStart = _alphabet.Substring(26 - _shiftValue);
            var alphabetEnd =  _alphabet.Substring(0, 26 - _shiftValue);


            _shiftedAlphabet = string.Format("{0}{1}",alphabetStart,alphabetEnd);


        }

        public string Encrypt(string plaintext)
        {
            var stringBuilder = new StringBuilder();  
            foreach(var character in plaintext.ToUpper())
            {
                var index = _alphabet.IndexOf(character);

                if (index != -1)
                {
                    stringBuilder.Append(_shiftedAlphabet[index]);
                }
                else
                {
                    stringBuilder.Append(character);
                }

            }

            return stringBuilder.ToString();
        }

        public string Decrypt(string ciphertext)
        {
            var stringBuilder = new StringBuilder();
            foreach (var character in ciphertext.ToUpper())
            {
                var index = _shiftedAlphabet.IndexOf(character);

                if (index != -1)
                {
                    stringBuilder.Append(_alphabet[index]);
                }
                else
                {
                    stringBuilder.Append(character);
                }

            }

            return stringBuilder.ToString();
        }
    }
}

Lets break it down…first the constructor

        public CaesarCipher(int shiftValue)
        {
            _shiftValue = (shiftValue % 26) ;

            _alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            var alphabetStart = _alphabet.Substring(26 - _shiftValue);
            var alphabetEnd =  _alphabet.Substring(0, 26 - _shiftValue);


            _shiftedAlphabet = string.Format("{0}{1}",alphabetStart,alphabetEnd);


        }

Its pretty simple going. We initialise the _shiftValue variable, but we modulus by 26 to give us a number in the range of 0..25 so that we can easily apply it to the alphabet (If you didn’t know, the English alphabet has 26 letters :P )

Then we use String.Substring() to get the first part of our alphabet by grabbing the last bunch of characters (i.e. for a shift of 4, we would get WXYZ). Then we use Substring again to chop off these last few letters (with a shift of 4, we would end up with ABCDEFGHIJKLMNOPQRSTUV).

The encrypt and decrypt methods are very similar. One thing to note is that we are converting all letters to upper case (so they are compatible with our alphabets) and ignoring all characters that are not letters. (indexOf returns -1 if the character is not found). The only difference between the two methods is that we swap the arrays around in the indexOf and Append calls.

        public string Encrypt(string plaintext)
        {
            var stringBuilder = new StringBuilder();  
            foreach(var character in plaintext.ToUpper())
            {
                var index = _alphabet.IndexOf(character);

                if (index != -1)
                {
                    stringBuilder.Append(_shiftedAlphabet[index]);
                }
                else
                {
                    stringBuilder.Append(character);
                }

            }

            return stringBuilder.ToString();
        }

Just “swap the alphabets” to convert it to a decryption.

public string Decrypt(string ciphertext)
        {
            var stringBuilder = new StringBuilder();
            foreach (var character in ciphertext.ToUpper())
            {
                var index = _shiftedAlphabet.IndexOf(character);

                if (index != -1)
                {
                    stringBuilder.Append(_alphabet[index]);
                }
                else
                {
                    stringBuilder.Append(character);
                }

            }

            return stringBuilder.ToString();
        }

So thats it?

Yup. Build it and go to the output folder and you should find your DLL! As for how to use it? Well… that can only come in part 4


Leave a Reply