Click here to Skip to main content
15,353,970 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm trying to create a cipher in C#, I need to take in the encrypted text from a text document and have the decryptions show up in the console and have the correct one export to a text document.

This is what I have so far:

using System;
using System.IO;

public class cipher
    public static void Main(string[] args)

        string output = "";
        int shift;
        bool userright = false;
        string cipher = File.ReadAllText("decryptme.txt");

        char[] decr = cipher.ToCharArray();

        do {

            Console.WriteLine("How many times would you like to shift? (Between 0 and 26)");
            shift = Convert.ToInt32(Console.ReadLine());
            if (shift > 26) {
                Console.WriteLine("Over the limit");
                userright = false;
            if (shift < 0) {
                Console.WriteLine("Under the limit");
                userright = false;
            if (shift <= 26 && shift >= 0)
                userright = true;

        while (userright == false);

        for (int i = 0; i < decr.Length; i++)
            char character = decr[i];

            character = (char)(character + shift);

            if (character == '\'' || character == ' ')

            if (character > 'Z')
                character = (char)(character - 26);
            else if (character < 'A')
                character = (char)(character + 26);

              output = output + character;

            Console.WriteLine("\nShift {0} \n {1}", i + 1, output);

        StreamWriter file = new StreamWriter("decryptedtext.txt");


I'm stuck right now, I have it feed in the encrypted text but it doesn't decipher it. It just prints in the console the encrypted text. Any help would be appreciated.

Updated 21-Apr-22 1:09am
PIEBALDconsult 15-Dec-15 8:55am
0) cipher.ToCharArray(); --- Is needless, a string is already indexable.
1) Convert.ToInt32(Console.ReadLine()); --- Don't use Convert; use Int32.TryParse .
2) All three if (shift statements can be replaced with one userright = (shift <= 26 && shift >= 0) statement.
3) For output, use a StringBuilder rather than a string.

BillWoodruff 15-Dec-15 11:52am
In the last few months there have been several questions posted about the Casear Cipher, and there's lots good solutions, and code.

Try this link.
Start by feeding it "known" text: assume a shift of one and the text "HELLO"
Create a text file using NotePad that contains just "IFMMP" and save it.
Put a breakpoint on the line:
char character = decr[i];

And run your app, feeding it the text file you just created.
Then step through your program one line at a time, and look at exactly what is going on.
The first line should fetch the encrypted character, the next should decrypt it - so you shown be able to see the decrypted character in the debugger.
It should be immediately obvious what the problem is! :laugh:

Hint: you won't need to go far to spot the problem...
This is a bit simpler. Uses mod to deal with numbers outside bounds:

private abstract class CharClass
    //default shift
    private static int _shift = 13;
    //chars that can be shifted
    private const string Alphabet = "abcdefghijklmnopqrstuvwxyz";

    //update the shift number
    public static int Shift
        private get { return _shift; }
        set { _shift = value; }

    //perform the shift per char
    public static char ShiftChar(char c)
        var index = Alphabet.IndexOf(c);
        if (index == -1)
            return c;
        index = ((index + Shift) + Alphabet.Length) % Alphabet.Length;
        return Alphabet[index];

    //perform the shift on a string
    public static string ShiftString(string s)
        return new string(s.Select(ShiftChar).ToArray());


usage: (well, my test method)

public void Test1()
    CharClass.Shift = 5;

    const string unEncrypted = "the quick brown fox jumps over the lazy dog";

    var encrypted = CharClass.ShiftString(unEncrypted);

    CharClass.Shift = -5;

    var unEncryptedAgain = CharClass.ShiftString(encrypted);


ONLY the chars in Alphabet will be shifted. uppercase chars will not in this case, neither will whitespace or special chars.

I could update this to handle upper case too, but I'm sure you can manage it ^_^

Hope that helps
char[] enc = { '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' };
char[] dec = { 'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a' };
Console.WriteLine("Enter Massge");
string x = Console.ReadLine();
Console.WriteLine("Inter Key Number");
int qq = Convert.ToInt32(Console.ReadLine());

char[] main = x.ToCharArray();
for (int i = 0; i < main.Length; i++)
for (int j = 0; j < enc.Length; j++)
if (main[i] == enc[j])
Dave Kreskowiak 21-Apr-22 7:46am
Doing someone's homework for them is never a good idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900