You need two steps - serialization/deserialization, which converts an object to a representation which can be stored on disk; and encryption/decryption, which enciphers the on-disk representation.
In Java you can use an ObjectOutputStream
to perform the serialization, and a CipherOutputStream
to perform the encryption. First obtain a FileOutputStream
, then pass that to the constructor of a CipherOutputStream
, then pass that to the constructor of an ObjectOutputStream
. Then you can just hand your Serializable object(s) to the ObjectOutputStream
, and they'll end up serialized, encrypted and written to a file. (You will of course need to perform additional setup on at least the CipherOutputStream
object, but that's the basic idea).
However, there is a rather large caveat to all of this. The encryption you're doing is no more than obfuscation - if you give someone the encrypted data and a program that can decrypt it, that person has all the information they need to decrypt the data themselves. There's no way to get around this - if your program can decrypt it, then your program can be pulled apart and its secrets found.