?
Solved

Encryption Program using Caesar shift C#

Posted on 2006-11-06
3
Medium Priority
?
2,165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Introduction to Processes

801 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