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");
}
}
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();
}
}
}