Jermaneeeee
Jermaneeeee

Reputation: 29

Ceasar Shift C#

I have got the code that will decrypt the encrypted text from a file, but the problem I am having is that the spaces and new lines are not being shown and instead are replaced with letters. I have written if character = ' ' to continue just to clean it up.

using System;
using System.IO;
namespace ceasarAssignment
{
    public class caesarShift
    {
        public static void Main()
        {
            string file = @"text.txt", // Name of the file that is being                         read from
            encrypted = File.ReadAllText(file), // Reading the file
            decrypted = " ";
            char character = '0';
            int shift = 0;

            encrypted = encrypted.ToUpper(); // Puts the string into uppercase

            char[] alph = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            // The array above is the alphabet (26 characters)
            Console.WriteLine("Encrypted text: \n{0}", encrypted);
            //Shows the encrypted text before it is decrypted

            for (int i = 0; i < alph.Length; i++) //adds a counter so that this                 for loop will repeat until it has happened 26 times     
            {
                decrypted = "";
                foreach (char c in encrypted)
                {
                    character = c;// each letter in the file is now defined as a         'character'

                    if (character == ' ')//if the character is a space it will just continue 
                        continue;

                    shift = Array.IndexOf(alph, character) - i; //searchs the array for the character then minuses the counter to add the correct shift 
                    if (shift <= 0)
                        shift = shift + 26;// if the character is at the beginning of the array go to the end

                    if (shift >= 26)
                        shift = shift - 26;// if the character is at the end of the array go to the beginning


                    decrypted += alph[shift];
                }
                Console.WriteLine("\n Shift {0} \n {1}", i + 1, decrypted); //Shows                           the decrypted code for each 26 shifts
            }

        }
    }
}

Upvotes: 0

Views: 124

Answers (4)

Abdelmneim Hussein
Abdelmneim Hussein

Reputation: 51

I think this is what you want:

private static void Main(string[] args)
    {
        string file = @"text.txt",
               encrypted = File.ReadAllText(file),
               decrypted =string.Empty;

        char character = '0';
        int shift = 0;
        encrypted = encrypted.ToUpper(); 

        char[] alph = new char[26]
                          {
                              'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                              'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
                          };

        Console.WriteLine("Encrypted text: \n{0}", encrypted);

        for (int i = 0; i < alph.Length; i++)
        {
            decrypted = "";
            foreach (char c in encrypted)
            {
                character = c;

                if (character == ' '||character==10)
                {
                    decrypted += character;
                    continue;
                } 


                shift = Array.IndexOf(alph, character) - i;
                if (shift <= 0)
                    shift = shift + 26;

                if (shift >= 26)
                    shift = shift - 26;


                decrypted += alph[shift];
            }
            Console.WriteLine("\n Shift {0} \n {1}", i + 1, decrypted);

        }
    }

Upvotes: 0

Pomadomaphin
Pomadomaphin

Reputation: 116

you can check if the character you are looking at is in your alphabet

if (!alph.Contains(c)) 
{
    decrypted += c;
    continue;
}

Upvotes: 1

Alex
Alex

Reputation: 21766

You should only decrypt a character if char.IsLetter(c)) is true, otherwise you should just pass it through unmodified.

if (!char.IsLetter(c)) {
   decrypted += c;
   continue;
 }

Upvotes: 1

Pablo Canseco
Pablo Canseco

Reputation: 562

if (character == ' ')//if the character is a space it will just continue 
        continue;

should instead append a space to your decrypted string and then continue.

try

if (character == ' ') {  
  decrypted += character;
  continue;
}

Upvotes: 0

Related Questions