• Post Reply Bookmark Topic Watch Topic
  • New Topic

Decrypting a file with an array  RSS feed

 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment is to encrypt and then decrypt a file in Java. I used an array to add 100 to each character per the assignment but I need some help figuring out a way to subtract the 100 from each character so the file is decrypted again. I tried to create another array to decrement the 100 from each character but cant seem to figure it out/get it working. Here is my assignment and my code so far:

"Although there are complex encryption techniques, you should come up with a simple one of your own. For example, you can read the first file one character at a time, and add 100 to the character code of each character before it is written to the second file.

Then write a program to decrypt the second file. The program should read the content of the second file, restore the data to its original state, and write to a third file."

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo wrote:
Then write a program to decrypt the second file. The program should read the content of the second file, restore the data to its original state, and write to a third file."


Well, you never opened the "third file". You just opened the encrypted file (which I supposed is the second file here) for reading, and tried to write to it, which of course, isn't allowed (as the file is opened for reading). And of course, this would have been caught by the compiler, as there are no writeXXX() methods.

BTW, welcome to the ranch.

Henry
 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I need a method to write the new decrypted file? Sorry, first time working with binary files and encryption/decryption, etc...
 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm going to post my revised code and all the errors I got from compiling. They're all in my Decryption class. And thanks for the welcome. Appreciate the help.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo wrote:So, I need a method to write the new decrypted file? Sorry, first time working with binary files and encryption/decryption, etc...


BTW, you also never opened the "first" file when encrypting. All you did was process and wrote out an array into your "second" file... which arguably, if there was no first file, can you really call the encrypted files as the "second" file? ...

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo wrote:



Java statements, which the "for" loop is one of them, can only be used in initializers, constructors, or methods. You can't just declare "for" loops anywhere in a class.

Henry
 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay how does this look? Obviously, the file names aren't final but any improvements/problems to the code in general?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo wrote:Okay how does this look? Obviously, the file names aren't final but any improvements/problems to the code in general?


Well, obviously, the code still has quite a bit of compiler errors. Is there a specific error that you are having issues figuring out?

Henry
 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here are the compiler errors I'm getting now. Less then I was getting before but still a lot.

 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, I'm at a bit of a loss at figuring this out right now. This is my last assignment before my final so I just want to do a fairly good job
 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...revised my code once again. This time I'm only getting 2 compiler errors.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo wrote:Well, here are the compiler errors I'm getting now. Less then I was getting before but still a lot.


Less compiler errors doesn't actually mean a better situation. It is possible for an error to "confuse" the compiler that it is unable to find any more errors. It is also possible for a confused compiler to think there is an error, where there is none. Just fix the first few errors and recompile.

Philip Alonzo wrote:


Java methods can't be declared directly within another method.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo wrote:


The decryptFile() method of the FileDecryption class, is not in scope from the main() method of the FileEncryptionAndDecryption class.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philip Alonzo,

It seems you wrote a bit too much code without testing it by small parts (because there are no small parts). I'd suggest you to decompose your program into smaller methods, so it might would help you better understand whats happening. Each method suppose to do what is meant to do according to a method name (only). If you are manipulating with files, try to keep separate this task from encryptFile and DecryptFile.

 
Philip Alonzo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help so far guys. Appreciate it. So I've worked on my code further and it now compiles with no errors. However, I get an error when it runs.

My code and error:

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it is not an error.
This exception is quite descriptive, what do you think, what happens here?
Look carefully
It less likely would happen if you would decompose your program into smaller methods each for a particular task. At least easier would be to notice where things went wrong ;)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!