I want to make sure that the code I have for encrypting and decrypting a serializable object makes sense and is proper. Does it look right too?
Here's what I have so far:
public static void encrypt(Serializable object, String path) throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
try {
// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES/ECB/PKCS5Padding");
// Create cipher
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sks);
SealedObject sealedObject = new SealedObject(object, cipher);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(path)), cipher);
ObjectOutputStream outputStream = new ObjectOutputStream(cos);
outputStream.writeObject(sealedObject);
outputStream.close();
}
catch(IllegalBlockSizeException e){
e.printStackTrace();
}
}
public static void decrypt(Serializable object, String path) throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cipherInputStream = new CipherInputStream( new BufferedInputStream( new FileInputStream(path) ), cipher );
ObjectInputStream inputStream = new ObjectInputStream( cipherInputStream );
SealedObject sealedObject = null;
try {
sealedObject = (SealedObject) inputStream.readObject();
TransferData td = (TransferData) sealedObject.getObject( cipher );
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
}