Translate

Wednesday 23 January 2013

Caesar Cipher Algorithm Implemented in C#


Caesar Cipher:
As per wiki...
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security.


See: wiki-Caesar_cipher for details


I have implemented it in C#.. The Code follows..



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Caesar
    {
        static void Main(string[] args)
        {
          
           
            Console.WriteLine("Enter Key:");
            int k = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Press C for ciphering and D for deciphering:");
            string choice =Convert.ToString(Console.ReadLine()).ToUpper();

            switch (choice)
            {
                case "C": Console.Write("Enter Plain Text:");
                          string pt = Console.ReadLine();
                          caesar_cipher(k, pt);
                           break;

                case "D": Console.Write("Enter Ciphertext :");
                           string ct = Console.ReadLine();
                          caesar_decipher(k, ct);
                          break;

                default: Console.WriteLine("Wrong Choice...!!");
                           break;
            }
           
        }
        static void caesar_cipher(int key, string pt)
        {
            int size = pt.Length;
            char[] value = new char[size];
            char[] cipher = new char[size];
            for (int r = 0; r < size; r++)
            {
                value[r] = Convert.ToChar(pt.Substring(r, 1));
            }

            for (int re = 0; re < size; re++)
            {
                int count = 0;
                int a = Convert.ToInt32(value[re]);
                for (int y = 1; y <= key; y++)
                {
                    if (count == 0)
                    {
                        if (a == 90)
                        { a = 64; }
                        else if (a == 122)
                        { a = 96; }
                        cipher[re] = Convert.ToChar(a + y);
                        count++;
                    }
                    else
                    {
                        int b = Convert.ToInt32(cipher[re]);
                        if (b == 90)
                        { b = 64; }
                        else if (b == 122)
                        { b = 96; }
                        cipher[re] = Convert.ToChar(b + 1);
                   
                    }
                }
            }
            string ciphertext = "";

            for (int p = 0; p < size; p++)
            {
                ciphertext = ciphertext + cipher[p].ToString();
            }
            Console.WriteLine("Cipher Text=");
            Console.WriteLine(ciphertext.ToUpper());
        }


        static void caesar_decipher(int key, string ct)
        {
            int size = ct.Length;
            char[] value = new char[size];
            char[] cipher = new char[size];
            for (int r = 0; r < size; r++)
            {
                cipher[r] = Convert.ToChar(ct.Substring(r, 1));
            }

            for (int re = 0; re < size; re++)
            {
                int count = 0;
                int a = Convert.ToInt32(cipher[re]);
                for (int y = 1; y <= key; y++)
                {
                    if (count == 0)
                    {
                        if (a == 65)
                        { a = 91; }
                        else if (a == 97)
                        { a = 123; }
                        value[re] = Convert.ToChar(a - y);
                        count++;
                    }
                    else
                    {
                        int b = Convert.ToInt32(value[re]);
                        if (b == 65)
                        { b = 91; }
                        else if (b == 97)
                        { b = 123; }
                        value[re] = Convert.ToChar(b - 1);

                    }
                }
            }
            string plaintext = "";

            for (int p = 0; p < size; p++)
            {
                plaintext = plaintext + value[p].ToString();
            }
            Console.WriteLine("Plain Text=");
            Console.WriteLine(plaintext.ToLower());
        }
    }
}
-------------------------------------------------------------------- 

The Output:
Ciphering




Deciphering


No comments:

Post a Comment