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

May 07 2012

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


So we can use our dll file in another project?

Indeed. Start up a new console project (I loaded up another instance of visual studio) and call it CaesarTest. Now the important bit is to reference your newly created library. To do this, complete the following steps:

  1. Right click on references in the solution explorer and choose “Add reference”
  2. Click on browse
  3. Locate your dll file (Should be located in CaesarLib\bin\Debug\) and click ok

So we can use it now?

Yup, using our library is as simple as importing the namespace. Have a look at this simple program I put together to test the library

using System;
using CaesarLib;

namespace CaesarTest
{
    class Program
    {
        static void Main()
        {
            var caesar = new CaesarCipher(5);

            var plaintext = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
            var ciphertext = caesar.Encrypt(plaintext);

            var decryptedPlaintext = caesar.Decrypt(ciphertext);

            Console.WriteLine(string.Format("Plaintext:  {0}", plaintext));
            Console.WriteLine(string.Format("Ciphertext: {0}", ciphertext));
            Console.WriteLine(string.Format("Decrypted:  {0}",decryptedPlaintext));
            Console.ReadKey(true);
        }
    }
}

This code produces the output:

Plaintext:  THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: OCZ LPDXF WMJRI AJS EPHKN JQZM OCZ GVUT YJB
Decrypted:  THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

Is this secure?

NO. The caesar cipher shown here is only used to demonstrate a very simple use case for creating a class library. It is easily broken and should NOT be used for any security purpose whatsoever.

Summary

So what did we learn about?

  1. We learnt what a class library is and why it is useful
  2. We learnt about the basic caesar cipher
  3. We learnt how to implement the caesar cipher as a class library
  4. And finally, we learnt how to bring an external library into a new project

Thank you for following this tutorial, I hope it helped you in some way :)
If anybody has any questions/feedback, please do not hesitate to contact me (You can use the contact buttons at the top of this page).

Download

The project files are available to download. Just use the button below.

Download Sourcecode565 downloads

Leave a Reply