• Welcome to Computer Association of SIUE - Forums.
 

Help with project in C#

Started by SgtEyre, 2006-05-05T12:02:24-05:00 (Friday)

Previous topic - Next topic

Geoff Schreiber

If you can only use 32-126, you just need to shift your range a little bit...  

Characters 32-126 become Characters 1-95 through subtraction, then using modulo 95 you can get your result to stay within the correct range after ciphering.
~~~~~~~~~~~~~~~~~~~
Geoff Schreiber
Project Engineer
FASTechnology Group

SgtEyre

Blacklee,

Ok, here is the code I have so far as a starting point. (I also have a menu class, however that is not where this problem is). The code I have right now would take a string, encrypt it by adding a value of one, and decrypt it by subtracting a value of one. How would I code a range in that would work for the assignment specs I listed previously. Can you please show me so I can get this behind me....It has been bugging me since I got an F on it and it is the only F I have every gotten in my life. Thanks for your help.

using System;

using System.Text;

namespace Crypt
{
    public class Crypto
    {
        public char convertData(string ch)
        {
            char input = char.Parse(ch);
            return input;
        }


        public void displayMessage(char aletter)
        {
            Console.WriteLine("The ASCII value of your character: " + aletter + " is " + (int)aletter);
        }



        public string encrypt(string userData)
        {
            int letterLength = userData.Length;
            for (int count = letterLength; count > 0; count--)
            {
                string number = userData.Substring(letterLength - count, 1);
                char aLetter = char.Parse(number);
                int aLetterNumber = ((int)aLetter + 1);
                aLetter = (char)aLetterNumber;
                displayMessage(aLetter);
                   
            }

                return userData;

        }

        public string decrypt(string userDataTwo)
        {

            int letterLength = userDataTwo.Length;
            for (int count = letterLength; count > 0; count--)
            {
                string number = userDataTwo.Substring(letterLength - count, 1);
                char aLetter = char.Parse(number);
                int aLetterNumber = ((int)aLetter - 1);
                aLetter = (char)aLetterNumber;
                displayMessage(aLetter);
            }

            return userDataTwo;


        }


        public static void Main(String[] args)
        {
            Crypto convert = new Crypto();
            Menu menu = new Menu();
            char ch;
            do
            {
                ch = (menu.chooseFromMenu());
                menu.processCommand(ch, convert);
            } while (ch != 'Q' && ch != 'q');
        }

    }
}


blacklee

SgtEyre,

Don't worry about F, this is a hard assignment. Sorry, I can't write the code for you. Even if I wanted to, I don't know C#. I'll be happy to help you with the algorithm if I can.

I'm assuming you know how to change your code to use encryption key "joe student key" in place of 1; and to display output as a string. Let me know if you don't.

Let's go back to the example I've posted in my last message. You probably already figured out, the formula is: if encrypted value > 10, encrypted value = encrypted value -10 + 1, because the range has shifted by one.

In your program the range is shifted by 31, so you need to insert after int aLetterNumber = ((int)aLetter + 1):
if aLetterNumber > 126
aLetterNumber = aLetterNumber - 126+31 = aLetterNumber - 95.
Similarly, in decrypt:
int aLetterNumber = ((int)aLetter - 1);
if aLetterNumber < 32
aLetterNumber = aLetterNumber + 95

That would be all, if the encryption key were a constant number.  Because encryption key can be in the range from 32 to 126, the encrypted value can be in the range 64 to 252, which means that some of the values can be "wrapped" more than once.

Go back to the last example from my previous message again. Let's assume that we used encryption key {10,9,8,7,6,5,4,3,2} in place of constant 3. If input is (10,7,2), encryption is (20,16,10). Possible encryption range is from 4 to 20. How to change it to fall within 2-10 range? The table below illustrates how it can be done:



Note that if you subtract 9 from 20, the result is still out of range. So now the formula changes from "if encrypted value > 10, encrypted value = encrypted value - 9" to "while encrypted value > 10, encrypted value = encrypted value - 9"
Similarly, change the if statement to "while aLetterNumber > 126
aLetterNumber = aLetterNumber - 95"

You can use modulo (mod) operation in place of subtracting. Then you don't need a while loop nor an if statement. Note that (2 mod 9) = (11 mod 9) = (20 mod 9) = 2, because mod finds a remainder from division. If you are not familiar with modulo, you don't really have to use it for this program if you don't want to.

I hope this helps you with encrypting. Decrypting should be very similar. Let me know if you have any other questions.

SgtEyre

Thanks for your help.. It all clicked for me after that! I really appreciate it, if only I would have received this help sooner so I could have completed the assignmet. Oh well! Lesson learned! Once again thanks for your help and if I ever need anymore help with assignments I know where to go now. Thanks blacklee and thank you all for your input.

Warmest Regards

SgtEyre