I am trying to encrypt the contents of one file to another file using a passphrase in Java. The file is read into a byte array, encrypted into another byte array, and then written to a new file. Unfortunately, when I try to change the encryption, the output file is decrypted as garbage.
I strongly suspect that the problem is with generating an identical key every time the same phrase is used. I wrote a testing method that dumps a key into a file whenever generated. The key is recorded both directly and in encoded form. The former is identical each time, but for some reason, the latter is always different.
Honestly, I am not very versed in encryption methods, especially in Java. I only need data that should be moderately secure, and encryption should not withstand attacks from anyone with significant time and skills. Thanks in advance to everyone who has advice on this.
Edit: Esailija was kind enough to indicate that I always set the cipher using ENCRYPT_MODE. I fixed the problem with a boolean argument, but now I get the following exception:
javax.crypto.IllegalBlockSizeException: input length must be a multiple of 8 when decrypting with the addition of encryption
It sounds like the phrase is not being used properly. I got the impression that "PBEWithMD5AndDES" would hash it into a 16-byte code, which, of course, is a multiple of 8. I wonder why the key is generated and used just fine for encryption mode, but then it complains when it tries to decrypt in those same conditions.
import java.various.stuff; public class FileEncryptor {
source share