Solved

Encryption Program using Caesar shift C#

Posted on 2006-11-06
3
2,135 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now