• 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

SgtEyre

This may seem like a very elementary question however, I am just learning the C# language. I have to write a program that that encrypts a string and then decrypts the string back to the original string. That is not a problem I have done this before. My problem is that I have to keep the the encryption between the ASCII code of 32 - 126 and I do not know how to keep it from being out of range.

Example; the ~ character is # 126 in ASCII. If I add the a fixed number to encrypt by such as 30, it will then take the value of the ~ character and make it 156, which is not within the ASCII range of 32 - 126. How do I keep it from doing this? And keep it within the range of 32 -126 even though I am adding 30 to it?

If anyone could help me I would greatly appreciate it. I am just not sure on how to code the solution to the problem. I am sure that this question will seem rather elementary to most of you however, your help would execellent. Thank you all for your time.

William Grim

Is that homework?

Also, that is easily reversable encryption.  I don't think you should rely on it at all.
William Grim
IT Associate, Morgan Stanley

R. Andrew Lamonica

You didn’t really specify what C# encryption package you are using, but here is a DES example that does what you need.  Most of the example is from the MSDN documentation, but I added the part that encodes the text in Base64 (text that only contains these characters â€Ã...“=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/â€Ã, = A subset of the ASCII set ).  

Keep in mind, that the US has strange laws concerning the export of encryption technology, so (I suspect that) versions of the .NET framework differ by country.

Click here for the example.

P.S. This example uses the .NET framework 2.0 and Visual Studio 2005.  If you are taking a CS class at SIUE you can get these pieces of software at the start of next semester.

EvilAndrew

I love C#, It makes stuff like this take about 10 Minutes.

Also, I'm with wgrim.  Is this for a class or thesis project?  If so, what class/project?  I would like to know because it sounds like an interesting one.
......

Jerry


Sounds like your looking for something simple - so here's a hint: ROT-13 (mod operator).

"Make a Little Bird House in Your Soul" - TMBG...

SgtEyre

Basically I am trying to figure out how and where to create the range for this project. I want to keep the range inbetween 32 and 126 (the standard range for ASCII) I am just unsure of where to set the range so that the character ~ (which is 126 on the ASCII table) will not be 176 when I add 50 to the value during the encrypt method. I will put my code below so that you can see what I have. If anyone could suggest where and how to put the range in there to keep the value of the encrypted code between 32 and 126 it would be greatly appreciated. thanx.

 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 + 55);
                aLetter = (char)aLetterNumber;
                displayMessage(aLetter);
            }

            return userData;

Jerry

If you are not familiar with the mod operator, then you can do something even simpler.

I'm not going to write your code for you since I don't know that this is not for an assignment, but I will give you a hint:

if aLetterNumber > 126 ...

 
"Make a Little Bird House in Your Soul" - TMBG...

SgtEyre

This is not for an assignment.... Well let me rephrase that, This was an assignment I had about a month ago, and the only assignment I was not able to figure out in it's entirety. And to be honest it is been bugging me that I was not able to do this assignment... I tried to figure it out during some free time I have been able to have lately. However, I am still somewhat perplexed as to what to do with the code. I do not know why this is getting me, but it is and I am about to pull my hair out!!!! I am assuming that you are suggestion I use the if...else statement? and if so what do I subtract values by??

William Grim

You really need to learn modulus to do this.  Read about "modulo expressions" anywhere on the Internet and you'll be set for this project.  I'd also write some test code to see how it works.
William Grim
IT Associate, Morgan Stanley

R. Andrew Lamonica

I apologize for my earlier post.  I assumed you were using good encryption instead of simple character substitution (like a decoder ring from a Caption Crunch Box).  

Jerry and grimw’s suggestions will work but only if you add an assumption to the assignment. The needed assumption is that the unencrypted text (plaintext) only includes ASCII characters.  This might be reasonable if you were just encrypting text messages but it will not work for files or messages with formatting (paragraph breaks, tabs, etc.).  

To handle binary files of formatted text you need to re-encode (spread out) the data to use only a small range of values in each byte.  This can be done with the â€Ã...“Convert.ToBase64String()â€Ã, function I used in the example I posted.  However, since almost no one looked at the example (I checked the logs) I assume posting it was a waste of time so I will not bother to post another that uses character substitution.  

Jerry

Quotegrimw wrote:
You really need to learn modulus to do this.  Read about "modulo expressions" anywhere on the Internet and you'll be set for this project.  I'd also write some test code to see how it works.

Follow the link to the Wikipedia entry on Caesar cipher that Andrew include in his post. It has the formula for using the mod operator.

And of course Andrew is right - your code will need to skip anything that is not in the ascii range of letters. It should just print them out.





"Make a Little Bird House in Your Soul" - TMBG...

SgtEyre

Alright, so now I am confused.... I should create a seperate method heading>???? That is fine, I can do that however I am not familiar with the Convert.ToBase64String() function, (as I said I am literally just learning the C# language). And I am unsure what to put in the body of the suggested method heading. I apologize for knowing little and asking many questions however, I have a strong desire to know what I missed and why I was not able to complete the assignment,and how to do it, so that next time... if there is a next time, I can do it right and understand it! Thanks for all of your help!! Keep the suggestions coming.. You just may have to put it in layman terms so that I understand, as I am not quite adept in all of the terminology... and MAYBE at some point I will finally get it and understand what I was missing. Once again thanks for all the help so far, please keep the suggestions coming I need all the help I can get because this past assignment still has me stumped.

blacklee

QuoteSgtEyre wrote:
Alright, so now I am confused....  
You are not alone.  :-)
Maybe you should post the exact problem definition. Where the input is coming from?
How much previous CS knowledge are you expected to have for this course? Did the course have any prerequisites?

SgtEyre

Use only standard ASCII characters 32 - 126
Let's say your algorithm uses an encryption key "joe student key", and adds each character in the key to each character in the plaintext message "Encrypt and Decrypt any Text".  Look up 'j' and 'E' in the ASCII character set chart to get their numeric values.  'j' is 106, and 'E' is 69.  If you add those two together, you get a new character (175) which does not fall on the ASCII chart and is certainly not between 32 and 126.  So let's suppose your algorithm subtracts 126 ('~') from the result giving you 49 ('1').  This would work fine for this example, but will it work for all cases

This was the assignment requirements. If you would like me to paste the code I had for the assignment. I will...if that helps. As I said before this was the only assignment that I was not able to complete. Every thing else I received an A on... I do not know why I am having such a hard time with this.

blacklee

QuoteSo let's suppose your algorithm subtracts 126 ('~') from the result giving you 49 ('1'). This would work fine for this example, but will it work for all cases

I think subtracting 126 would work fine if the specified range was between 1 and 126, not between 32 and 126. For example, if the encrypted ASCII value is 127, and you subtract 126; you'll get 1 (<32).

Start with something easy. Let's say your ASCII range is between 1 and 10; and you always add number 3 for encryption.
1 -> 4 (1+3)
2 -> 5 (2+3)
3 -> 6 (3+3)
...
7 -> 10 (7+3)
8 -> 1 (8+3-10)
9 -> 2 (9+3-10)
10 -> 3 (10+3-10)

The formula here is
if encrypted value > 10, encrypted value = encrypted value -10

Now assume the range is between 2 and 10.
2->5
...
7 ->10 (7+3)
8 -> 2 (8+3-???)
9 -> 3
10 -> 4

What is the formula here and why?

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