• Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggling to complete a cipher assignment  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My goal is to have all input and output stored as strings. I want to use the String newCipher to encrypt the String lineInput contents and store them to String encrypt. Then decrypt and write decryption message output.txt.
keyword.txt = "TROYONLINE"
input.txt = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"

I'm so lost right now. I've started over so many times that I forget what I've already tried and stuck in my loop of failure. What I would like to do is something like:
***Encryption***

read String lineInput and search for characters:

for every 'A' found, replace with string newCipher(pos 0)
for every 'B' found, replace with string newCipher(pos 1)
and so on until
for every 'Z' found, replace with string newCipher(pos 25)

Insert each replacement into String encrpyt

***Decryption***
read String encrypted and search for characters:

for every 'A' found, replace with string abc(pos 0)
for every 'B' found, replace with string abc(pos 1)
and so on until
for every 'Z' found, replace with string abc(pos 25)

Insert each replacement into String encrpyt


Suggestions are VERY welcome!!! Ps> I know StringBuilder is better than StringBuffer, but that isn't a concern right now. I really need some quick resolution and I've been working for several days with no luck. Thanks in advance!
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Unfortunately, with the code you have written, there will be no quick resolution for you.  Here are the problems I see:

1. A lot of code in main() -- so many things going on in the same method confuses you to no end. You need to break down your logic into smaller tasks. You'll need to do some functional decomposition and use a more divide and conquer approach to programming.  Jamming all your code into main shows a lack of organization of your thoughts/ideas of how you're going to solve the problem. If you're not organized, how can you expect to un-confuse yourself?

2. Poorly chosen names.  These add to your confusion even more. If you haven't organized your ideas into smaller chunks, then using names that don't reflect the purpose of the information they are attached to will only muddy up your cognitive waters even more. If you think that names are inconsequential, then you should think again. Why would a professional programmer, author, and speaker like Robert C. Martin, author of the book "Clean Code" dedicate an entire chapter in his book just to talk about using good names? It's not a trivial thing. In fact, choosing good names is one of the hardest things in programming and many programmers don't do it well. That's why there is so much more bad code than there is good code out there.

In the domain of cryptography, terms like "cipher" and "encryption" have very specific meanings. "Plain Text" is text that isn't enciphered/encrypted.  When you encrypt plain text, you get what is called the "Cipher text".  Encryption/Decryption is a two-way process. That is, you can take PlainText and apply an encryption algorithm to it to get CipherText. Then you can take the CipherText and apply the reverse process (decipher algorithm) to get back the PlainText.

So, when you say "I'm going to take a cipher and create a new cipher and then encrypt it," that really won't make much sense to anyone who is familiar with the proper terminology. And even though you think you know what you're talking about, the words you use and their actual meanings matter.  Go out and try the Stroop Effect Test to see for yourself how much words and their meanings matter when it comes to your cognitive ability.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start organizing your thoughts around your problem and your solution, write down the main subtasks. Your initial list might look something like this:

1. Get plain text to encode/encrypt
2. Process the text - do anything you need to do to clean it up or prepare it for encryption
3. Figure out the algorithm to use - implement it for one word
4. Set up a "pipeline" so that you can all words from the input and push them through your encryption algorithm.
5. Decide what to do with each word that is encrypted by your algorithm, maybe write it to a file or display it on the console.
... and so on.

Once you get a small breakdown of tasks, go through each subtask and see if you need to break it down even further. Break down tasks into smaller ones until you have tasks that are almost trivial to implement.

This is the kind of organization any complex piece of machinery has. A car has large assemblies like the engine, the power train, the fuel system, the electrical system. In an engine, you have the different subcomponents like a carburetor or fuel injection pump, the distributor, the engine block, the crankshaft, the pistons, etc. On a piston, you have the piston head, the piston rings, the connecting rod, the pin, the bearing, etc.  You see how each big assembly is broken down into smaller assemblies which are broken down into even smaller parts?  To manage the complexity of your program, you have to do pretty much the same thing.
 
steven franklin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu - thanks for the suggestions. I figured my main was pretty jumbled. Admittedly, this week's assignment was a BIG jump in skill level, and I probably am not ready for such a large jump. I would like to move at a much slower pace, as I have NO experience with programming, but the course isn't going to slow down for me and I certainly don't want my grade to suffer (or waste money). So, would you mind giving a suggestion on how I should break up my main method? I think I could put each file input section into their own methods, but then how would I call those methods from main? I appreciate all the tips I receive.
 
steven franklin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do these steps seem like a good plan?

1. Create string to hold the alphabet - initialize with 'A'-'Z'
2. Create string to hold cipher alphabet - initialize with 'Z'-'A'

3. Read text file to string to get keyword "TROYONLINE"
4. Read text file to string (including whitespaces) to get input text "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"

5. Append keyword to beginning of cipher alphabet

6. Remove all repeating letters of cipher string to get string newCipherAlphabet and write to file

7. Encrypt : read String lineInput and search for characters:
    for every 'A' found, replace with string newCipher(pos 0)
    for every 'B' found, replace with string newCipher(pos 1)
    and so on until
    for every 'Z' found, replace with string newCipher(pos 25)

8. Insert each replacement into String encrpyt

9. Decrypt: read String encrypt and search for characters:
    for every 'A' found, replace with string abc(pos 0)
    for every 'B' found, replace with string abc(pos 1)
    and so on until
    for every 'Z' found, replace with string abc(pos 25)

10. Insert each replacement into String output and write to file output.txt
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if that constitutes a good plan. It's at least a start.  As Dwight D. Eisenhower once said, "Plans are worthless but planning is everything."  Which means that plans are only as good as they get us to think about what we should do as a situation unfolds. If you have a plan and just mindlessly follow it without adjusting to the current set of circumstances, that's when the plan becomes worthless.

So, now you have a plan and you probably think it's workable. Like I said, I can't say whether or not it's a good plan because I haven't seen your actual requirements. I can't tell what algorithms you are supposed to use for encryption/decryption except that it involves a keyword, the alphabet backwards, and no duplicates. It looks like you might be doing just a straight-up Caesar or shift cipher.

One advantage of breaking down your program into small tasks is that you can also test your program sooner rather than wait until the end to try to run the program. If you add a little functionality at a time and test each little increment of functionality as you go, then you can get feedback from the software and learn from it. This is where the "planning is everything" thing comes in. If you test your program at each step, you can be sure that you're on the right track. If you make a mistake, it's easier to find the mistake because it has to be in the code that you just added since the last time you ran the program and your tests that didn't fail.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!