How do I decrypt an object with DES?

On the previous example How do I encrypt an object with DES? we encrypt an object. In this example we’ll decrypt the stored object.

package org.kodejava.example.security;

import java.io.*;

import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;

public class ObjectDecrypt {
    //
    // Method for reading object stored in a file.
    //
    private static Object readFromFile(String filename) throws Exception {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object object = null;

        try {
            fis = new FileInputStream(new File(filename));
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                ois.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return object;
    }

    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.
        //
        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);
    }
}

How do I encrypt an object with DES?

This example demonstrate encrypting an object using DES algorithm. After the object was encrypted we store it in a file which will be decrypted in the next example here How do I decrypt an object with DES?.

package org.kodejava.example.security;

import java.io.*;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

public class ObjectEncrypt {
    //
    // Store object in a file for future use.
    //
    private static void writeToFile(String filename, Object object) throws Exception {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = new FileOutputStream(new File(filename));
            oos = new ObjectOutputStream(fos);
            oos.writeObject(object);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                oos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        //
        // Generating a temporary key and store it in a file.
        //
        SecretKey key = KeyGenerator.getInstance("DES").generateKey();
        writeToFile("secretkey.dat", key);

        //
        // Preparing Cipher object for encryption.
        //
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        //
        // Here we seal (encrypt) a simple string message (a string object).
        //
        SealedObject sealedObject = new SealedObject("THIS IS A SECRET MESSAGE!", cipher);

        //
        // Write the object out as a binary file.
        //
        writeToFile("sealed.dat", sealedObject);
    }
}