Solved

Encryption Program using Caesar shift C#

Posted on 2006-11-06
3
2,144 Views
Last Modified: 2008-02-01
I am writing a program which reads in information from the file below: The program uses "Cryptography" and "Caesar Shift" to encode or decode the string.

e,COMPUTER SCIENCE,1
e,HELLO WORLD,11
e,THIS IS JUST A TEST,8
d,FDPNQVUFS!TDJFOD,1
d,KZZKXYIGVOZGR&R,6
d,TSHTXLT%GW,5

The first letter "e" or "d" tells the program if the string is going to be encoded or decoded.  The next string is the string that is going to be encoded or decoded.  The next integer tell the program how many steps the shift the word to be encoded or decoded.  So to decode a message, each letter is shifted N letters to the left and to encode a message, each letter is shifted N letters to the right depending on what the last number is.  So COMPUTER SCIENCE should output FDPNQVUFS!TDJFOD

I have writing everything to read in the file here is that i have so far:
So after the file is read in, somehow i need to beable to shift the string and change the letters.


using System;
using System.IO;
using System.Security.Cryptography;

namespace Lab07_Encryption
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  ReadFile();
            }

            public static void ReadFile()
            {

                  StreamReader fileReader;
                  string fileName = "input.txt";
             
                  fileReader = new StreamReader(fileName);
                  string sLine;
      
                  sLine = fileReader.ReadLine();

                  string [] split;
                  const char delm = ',';

                  while(sLine != null)
                  {
                        split = sLine.Split(delm);

                        string encryption = split[0];
                        string message = split[1];
                        int shift = int.Parse(split[2]);

                        string en = "e";

                        if(encryption == en)
                        {
                              encode();
                        }
                        else
                        {
                              decode();
                        }

                        sLine = fileReader.ReadLine();
                  }
            }

            public static void encode()
            {
      
            }

            public static void decode()
            {
            
            }
      }
}
0
Comment
Question by:jmkotman
  • 2
3 Comments
 
LVL 13

Expert Comment

by:AngryBinary
ID: 17885250
This looks like a homework assingment, so I'll just give you the general idea. You will want to convert the string to an array of characters, which can be cast as integers and operated upon as such:

for (int i = 0; i < inputString.length; i ++)
{
    int encodedChar = ((int)inputString[i] + charsToShift);
    outputString += (char)encodedChar;
}
0
 

Author Comment

by:jmkotman
ID: 17885663
Is there a built in class which knows all the character?
0
 
LVL 13

Accepted Solution

by:
AngryBinary earned 500 total points
ID: 17885852
The upshot is, there's no need to know the integer values of the characters ahead of time. A 'char' variable contains an integer, which represents (in .NET) it's Unicode value. Adding a number 'n' to that value will give you the Unicode value of another character n positions from the original character. As you cast from int to char and vice-versa, the conversion occurs automatically. In fact, underneath, they are both the same intrinsic type. To illustrate,

char myChar = 'a'; //------------------myChar contains the character code 65, or lowercase 'a'.
int myCharCode = (int)letter; //------myCharCode contains the integer 65

myCharCode += 2; //-----------------myCharCode now contains the integer 67
myChar = (char)myCharCode; //----myChar now contains the character code 67, or lowercase 'c'.

So, to answer your question, I suppose you can say that char 'knows' all the characters, although it's really just a matter of semantics.

--Randall
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 135
How can i compile this github project?? 2 85
Modify a small python script 19 109
draw a Christmas tree by using a nested loop? 26 62
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question