How to encrypt/decrypt a file in Java?
Posted
by Petike
on Stack Overflow
See other posts from Stack Overflow
or by Petike
Published on 2010-03-14T13:23:10Z
Indexed on
2010/03/14
13:35 UTC
Read the original article
Hit count: 262
Hello,
I am writing a Java application which can "encrypt" and consequently "decrypt" whatever binary file.
I am just a beginner in the "cryptography" area so I would like to write a very simple application for the beginning.
For reading the original file, I would probably use the
java.io.FileInputStream
class to get the "array of bytes"
byte originalBytes[]
of the file.
Then I would probably use some very simple cipher, for example "shift up every byte by 1" and then I would get the "encrypted" bytes
byte encryptedBytes[]
and let's say that I would also set a "password" for it, for example "123456789".
Next, when somebody wants to "decrypt" that file, he has to enter the password ("123456789") first and after that the file could be decrypted (thus "shift down every byte by 1") and consequently saved to the output file via
java.io.FileOutputStream
I am just wondering how to "store" the password information to the encrypted file so that the decrypting application knows if the entered password and the "real" password equals?
Probably it would be silly to add the password (for example the ASCII ordinal numbers of the password letters) to the beginning of the file (before the encrypted data).
So my main question is how to store the password information to the encrypted file?
© Stack Overflow or respective owner