On the previous example How do I encrypt an object with DES? we encrypt an object. In this example we will decrypt the stored object.
package org.kodejava.crypto;
import java.io.*;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
public class ObjectDecrypt {
public static void main(String[] args) throws Exception {
// Read the previously stored SecretKey.
SecretKey key = (SecretKey) readFromFile("secretkey.dat");
// Read the SealedObject
SealedObject sealedObject = (SealedObject) readFromFile("sealed.dat");
// Preparing Cipher object from decryption.
if (sealedObject != null) {
String algorithmName = sealedObject.getAlgorithm();
Cipher cipher = Cipher.getInstance(algorithmName);
cipher.init(Cipher.DECRYPT_MODE, key);
String text = (String) sealedObject.getObject(cipher);
System.out.println("Text = " + text);
}
}
// Method for reading object stored in a file.
private static Object readFromFile(String filename) {
try (FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis)) {
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}