Use DES To Seal And UnSeal Object : Seal Unseal « Security « Java

Use DES To Seal And UnSeal Object


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

public class MainClass {
  public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey secretKey;
    Cipher encrypter, decrypter;

    secretKey = KeyGenerator.getInstance("DES").generateKey();

    encrypter = Cipher.getInstance("DES");
    encrypter.init(Cipher.ENCRYPT_MODE, secretKey);

    decrypter = Cipher.getInstance("DES");
    decrypter.init(Cipher.DECRYPT_MODE, secretKey);

    MyClass cust, unsealed;
    SealedObject sealed;

    cust = new MyClass(); = "Paul";
    cust.password = "password";

    // Seal it, storing it in a SealedObject
    sealed = (new SealedObject(cust, encrypter));

    // Try unsealing it
    String algorithmName = sealed.getAlgorithm();
    unsealed = (MyClass) sealed.getObject(decrypter);

    System.out.println("NAME: " +;
    System.out.println("PASSWORD: " + unsealed.password);


class MyClass implements Serializable {
  public String name;

  public String password;

       1,198 k)

Related examples in the same category