How to encrypt and decrypt an object in and from a file in Java
Tech-Today

How to encrypt and decrypt an object in and from a file in Java


There are times when we need to write something on a file, but doesn't want it to be readable as a plain text. In this case we can use any type of encryption mechanism, but what if we want to decrypt the encrypted file back and read its contents. For example a configuration file, etc.

Let's show some codes as usual:

public class CipherUtils {

public static final String CIPHER_MODE = "AES/CBC/PKCS5Padding";

private CipherUtils() {

}

public static void encode(Serializable object, String password, String path)
throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException {
Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, fromStringToAESkey(password), new IvParameterSpec(new byte[16]));

// read object from file
SealedObject sealedObject = new SealedObject(object, cipher);
FileOutputStream fos = new FileOutputStream(path);
CipherOutputStream cipherOutputStream = new CipherOutputStream(new BufferedOutputStream(fos), cipher);

ObjectOutputStream outputStream = new ObjectOutputStream(cipherOutputStream);
outputStream.writeObject(sealedObject);
outputStream.close();
fos.close();
}

public static Serializable decode(String password, String path)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException,
ClassNotFoundException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
Cipher cipher = Cipher.getInstance(CIPHER_MODE);

// write object to file
cipher.init(Cipher.DECRYPT_MODE, fromStringToAESkey(password), new IvParameterSpec(new byte[16]));
CipherInputStream cipherInputStream = new CipherInputStream(new BufferedInputStream(new FileInputStream(path)),
cipher);

ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
SealedObject sealedObject = (SealedObject) inputStream.readObject();
Serializable serializeableObject = (Serializable) sealedObject.getObject(cipher);
inputStream.close();

return serializeableObject;
}

public static SecretKey fromStringToAESkey(String s) throws UnsupportedEncodingException {
// 128bit key need 16 byte
byte[] rawKey = new byte[16];
// if you don't specify the encoding you might get weird results
byte[] keyBytes = s.getBytes("UTF-8");
System.arraycopy(keyBytes, 0, rawKey, 0, keyBytes.length);

return new SecretKeySpec(rawKey, "AES");
}
}

This utility class can encrypt and decrypt any serialize-able object we have.

And here's how we use it:


public class CipherUtilsTest {

private Person person;

@Before
public void init() {
person = new Person();
person.setFirstname("Shirayuki");
person.setLastname("Hime");
person.setAge(18);
}

@Test
public void testEncodeDecode() {
try {
CipherUtils.encode(person, "shirayuki", "c://temp//cipher");
Person decodedPerson = (Person) CipherUtils.decode("shirayuki", "c://temp//cipher");
assertEquals(person.getAge(), decodedPerson.getAge());
assertEquals(person.getFirstname(), decodedPerson.getFirstname());
assertEquals(person.getLastname(), decodedPerson.getLastname());
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
| IOException | ClassNotFoundException | BadPaddingException | InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}




- How To Zipped A File Or Directory In Java
There are times when we have to zipped a file or directory recursively in Java. This is how we do it: public static void zipFile(File file) throws IOException { byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream( file.getParent()...

- How To Call Java Rest Web Service In Soapui
The following code is an explanation of how you can call a rest web service in java. Below you can find the actual java code and soapUI configuration. We enumerate 3 type of methods namely: POST, PUT and DELETE. How to call rest web service using soapUI...

- How To Send And Receive Stomp Message In Jboss 7.2
The following code is an example of how we can send and receive a stomp message with JBoss 7.2. package org.meveo.util; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Properties; import javax.naming.InitialContext;...

- Executing A Linq Query With A Bridge Table Like Aspnet_users And Aspnet_roles
Using model first approach, we add aspnet_Users, aspnet_Roles and aspnet_UsersInRoles in the edmx file. But why is aspnet_UsersInRoles missing? It's because aspnet_Users has one-to-many relationship to aspnet_Roles. To get the role of the user, we...

- Entity Framework Inserting Or Updating A Table With Foreign Key
For example you have 2 lookup entities: Country and Province And Your Person Entity has country and province field. In short both CountryId and ProvinceId are foreign keys to Person table. How to setup the Insert Statement (Update is almost the same,...



Tech-Today








.