• 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?