Encrypting and Decrypting text from file using keyword

Reply

Join Date: Oct 2007
Posts: 2
Reputation: Alexbeav is an unknown quantity at this point 
Solved Threads: 0
Alexbeav Alexbeav is offline Offline
Newbie Poster

Encrypting and Decrypting text from file using keyword

 
0
  #1
Oct 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Encrypting and Decrypting text from file using keyword

 
0
  #2
Oct 19th, 2007
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...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: Encrypting and Decrypting text from file using keyword

 
0
  #3
Oct 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Encrypting and Decrypting text from file using keyword

 
0
  #4
Oct 20th, 2007
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]
Last edited by dwks; Oct 20th, 2007 at 5:45 pm.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: Encrypting and Decrypting text from file using keyword

 
0
  #5
Oct 20th, 2007
You're right. My mistake.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Encrypting and Decrypting text from file using keyword

 
0
  #6
Oct 20th, 2007
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC