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 will decrypt the stored object.

package org.kodejava.example.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) throws Exception {
        try (FileInputStream fis = new FileInputStream(new File(filename));
             ObjectInputStream ois = new ObjectInputStream(fis)) {

            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you šŸ™‚.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.