Book Cipher

Pages: 12
Hi everyone. I am faced with a new challenge. I have to create two programs: one that encodes a file using a book cipher, and one the decodes it. I do not want a direct answer and would prefer guidance through this. Basically right now I am just planning out what to do and do not want to start off on a bad start. What I am doing is creating a method called encode which used the cipher file and file to be encoded as it's parameters. The method will scan the book cipher and use a switch for each letter of the alphabet and store what line number that character is on as well as which character it is from the beginning. So say 'a' is on line 10 and is the 126th character, 'a' encrypted would be 10126. If anyone has anything I should know or some other insight that would be great.

Thanks, and sorry for the long post
I have a bit of input. How would you know how to interpret 10126 as line 10, character 126? Why not line 101, character 26, or any of the other possible interpretations? You'll want to either set a fixed size for each number or use a delimiter of some kind.
@ Zhuge: Book ciphers use either a: use the entire book in a set pattern, for example word one is from chapter one word two from chapter two... or b: use a designated part of the book for codecing. The method used is established at the time the cipher is written, kind of like how you know what file to read from when you write a program.

@ OP: I would start simple with version one and practice with a ceasar shift otherwise do what you say, except a series of numbers would equal entire words.

1: Use the fstream to open a txt file containing your 'book'.
2: Compare the message you want to encode as an array of strings to the words in the book.txt file with a loop using two intagers to keep track of your position one for word the other for paragraph. When you find the word output the value of the two integers as your code for example the fourth word in the first paragraph gets encoded as 04001. Use blank lines to delimit paragraphs, when a new paragraph is reached reset the word counter.
3: Output both integers to a new file without a space between them keeping with the example 04001 for the fourth word in the first paragraph is how it appears in the output file. Use Division and Modulo by 1000 to decode these in my example.

That should be a good start with automating the encoding part. This sounds cool I might try it myself :).

EDIT: Make sure the output of your encripted message is uniform, in my example ALL words would have to be output as a 5 digit number WITH THE ZEROS!!! To do this output your two integers into a two dimentional array slots 0 and 1 are reserved for the first integer slots 2, 3 and 4 are reserved for the second integer slot 5 is of course your array terminating delimiter.
Last edited on
Decoding program flow example the fun part of this:

1: 04001 is read through ifstream into a variable first.
2: First is loaded into the integer ifw (integer_first_word) and isw (integer_second_word).
3: ifw = ifw / 1000. Divides ifw by 1000 giving you in this case 4.
4: isw = isw % 1000. Modulo isw by 1000 giving you the remainder in this case 1.
5: Outer loop loads and discards the number of paragraphs in book.txt again using a blank line to denote paragraphs for number of times equal to isw.
6: Inner loop loads words into string word and dicards them for number of times equal to ifw.
7: Value of word is output to either another txt file or an array that you will use to collect them. Don't forget to promote the pointer in your array.
Explain what you mean. It seems the OP is using two numbers to locate a character; the line number of the character and it's position on that line. No matter what you do, you will have to figure out some way to specify which number is which because simply concatenating the numbers yields a number that cannot easily be converted back with either a delimiter or some set standard. Such a standard could say, for example, the first three digits are the line number and the last four are the character number on that line. But either way, the number alone isn't enough.
Yes it can be converted back, it's important that ALL of the numbers come out in the same format, which makes this a weak cipher. What that format is depends on the skill level of the programmer.

Read my post @ 5:12 to see how to bring it back.
Ah, so we were thinking the same thing, just in different words, I see.
Yup, looks like fun doesn't it? I'm playing with this now.
Thanks for the posts guys. I might try working on this at a later time when I get the chance, studying for midterms right now.
Hi guys. I have ran into an issue. I had a million errors but got it down to one. I made a method after my main method called encode. When I try to run the program I get the error " error C3861: 'encode': identifier not found". In Java I am used to declaring methods after the main method. Is it different in c++? Do I have to make another source file or something?

EDIT: I should also add the when the program is run it has to be run like this "program.exe bookfile messagefile". I assumed I had to create a method to do this. Is this correct?

Thanks,
Brad
Last edited on
If you declare the functions after main(), then main() won't know they exist unless you put a prototype/signature above main(). Just like how you can't use a variable above where it is declared.

For command line arguments, set up main to look like so:
int main(int argc, char* argv[]) {
argc and argv are just names and can be whatever you like. argc will store the number of arguments you received including the executable name, and argv is an array of C-style strings containing the arguments, with argv[0] being the identifier you used to invoke the program.

So for example, the command you had before:
program.exe bookfile messagefile
Would result in the following:
argc = 3
argv[0] = "program.exe"
argv[1] = "bookfile"
argv[2] = "messagefile"
Thanks a lot I'll try that out.

EDIT: Actually wait... how do I tell the arguments what to do lol. Sorry if this is a super noob question.
Last edited on
What do you mean by "what to do?"
Sorry I mean how do I store what the user enters for the arguments as variables. I want bookfile/messagefile to be variables. I have this atm which I know is probably wrong:

1
2
3
4
5
6
7
8
9
	for (argc--, argv++;argc > 1;argc--, argv++)
	{
		argv[2] = cin.get();
	}

	for (argc--, argv++;argc > 0;argc--, argv++)
	{
		argv[1] = cin.get();
	}
No, you don't need to set any variables like that. They are automatically set by the OS when your program is run with arguments.
Yeah, if you re-read what Zhuge said, argv will *already* contain what the user input on the command line, you don't have to do any reading to get them.
Ok thanks a lot, I just thought that argv[1] for example would still contain "bookfile". Thanks for clarifying.
Another noob question: When I find a certain letter in the message file, how do I replace it with what something else?
You mean like search and replace, or something? In most cases, when editing a file you read it all into memory, make changes, then write it all out again.
Ok. I need to replace each letter with something similar to "100:5:2." I already have the values for each letter in a variable. What is the command to change, say, the letter a to what I have stored in my a variable. I am reading the file character by character. I was thinking that maybe it would be possible to type cast my ch variable to a string and print it if that is possible. If that is not possible, how do I read the entire file into memory so that I can replace the letters, and what would be the command to do so.

Thanks again for all the help.
Pages: 12