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.

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...

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

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:

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]

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]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.