943,973 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3994
  • C RSS
Oct 19th, 2007
0

Encrypting and Decrypting text from file using keyword

Expand Post »
Hi all this is the 2nd exercise I can't seem to figure out how to do.Exercise goes like this :

One method of encryption is this : We pair the letters of the alphabet to the numbers 0-25.
Assuming we have a text "T" and a keyword "K" of "V" letters.
We add the number of the letter we want to encrypt with the number of the first character of the keyword.If the result is beyond the limits of the alphabet (I assume like 35 for example),we substract 26.Repeat the process with the 2nd character of the text and the 2nd character of the keyword until the end of the text.If we run out of keyword letters we start over from the first letter of the keyword.

The program will be able to take 3 parameters

- enc or - dec : choose whether it'll be encryption or decryption
- cipher <word>: the keyword we'll use (10 chars max)
- <file_name> : our file's name (i.e. test.txt)

an example is :

test.txt (contents) : Attack at dawn.
command line : crypto -enc -cipher lemon test.txt
output : lxfopv mh oeib.

test.enc (contents) : lxfopv mh oeib.
command line : crypto -dec -cipher lemon test.enc
output : Attack at dawn.

I have absolutely NO idea how to do this. Any help would be greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alexbeav is offline Offline
2 posts
since Oct 2007
Oct 19th, 2007
0

Re: Encrypting and Decrypting text from file using keyword

Start by writing a program that accepts parameters on the command line and prints them out.
Once that's working, check the parameters to see what they are and output messages for each so you know you are analyzing the parameters correctly.
Then open the file, read it, and display it.
Basically, do a piece at a time...
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 posts
since May 2006
Oct 20th, 2007
0

Re: Encrypting and Decrypting text from file using keyword

I'll describe the algorithm in words. It's your job to write the code. You need a string constant that is 257 characters in length. It's value is a character string containing all of the ASCII characters followed by a terminating 0). Call it ASCIITable. As you read each character from "test.txt", add its ascii value to the ascii value of the current letter of your cypher word. Bump the index of the current letter in the cypher word and if you overflow, reset to zero. Use the value obtained by by adding the ascii code for the letter you read and the letter from the cypher as an offset into ASCIITable. The ascii character at that location becomes your output character. By the way, the bytes of ASCIITable do not have to be in any particular order, so long as each byte is unique.

Hoppy
Last edited by hopalongcassidy; Oct 20th, 2007 at 5:25 pm.
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Oct 20th, 2007
0

Re: Encrypting and Decrypting text from file using keyword

Quote ...
and if you overflow, reset to zero.
Not quite. That only works if you had gone one past the maximum allowed value. If you can add more than 1, you need to do this:
Quote ...
If the result is beyond the limits of the alphabet (I assume like 35 for example),we substract 26.
If you can add more than the maximum range -- in other words, if you could add 123 -- then you'd have to keep subtracting 26 until the value was within range. It's quite straightforward to do this -- just change the if() to a while(). (Or, more efficiently, use the modulus operator.)

[edit]
Quote ...
You need a string constant that is 257 characters in length. It's value is a character string containing all of the ASCII characters followed by a terminating 0).
That could cause some problems. Since you have all 256 extended ASCII characters, you also have '\0' in your string. So you can't treat a NULL as the terminating character for the string. You'd probably just hard-code in the fact that there are 256 characters in the buffer (a non-NULL-terminated string cannot properly be called a string). If you did this, you'd probably just get rid of that array[256] (a NULL), since it would be redundant. [/edit]
Last edited by dwks; Oct 20th, 2007 at 5:45 pm.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Oct 20th, 2007
0

Re: Encrypting and Decrypting text from file using keyword

You're right. My mistake.
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Oct 20th, 2007
0

Re: Encrypting and Decrypting text from file using keyword

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Question about my project
Next Thread in C Forum Timeline: problem in this code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC