Example usage for javax.crypto SealedObject getObject

List of usage examples for javax.crypto SealedObject getObject

Introduction

In this page you can find the example usage for javax.crypto SealedObject getObject.

Prototype

public final Object getObject(Cipher c)
        throws IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Retrieves the original (encapsulated) object.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String creditCard = "1234567890";

    KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");

    Key key = keyGenerator.generateKey();
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    SealedObject so = new SealedObject(creditCard, cipher);

    String unencryptedCreditCard = (String) so.getObject(key);

    System.out.println(unencryptedCreditCard);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SecretKey key = (SecretKey) readFromFile("secretkey.dat");
    SealedObject sealedObject = (SealedObject) readFromFile("sealed.dat");
    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);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    Cipher ecipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, key);

    SealedObject so = new SealedObject(new MySecretClass(), ecipher);

    String algoName = so.getAlgorithm(); // DES

    Cipher dcipher = Cipher.getInstance("DES");
    dcipher.init(Cipher.DECRYPT_MODE, key);
    MySecretClass o = (MySecretClass) so.getObject(dcipher);

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey secretKey;//from  w ww.  ja  v  a  2 s.  c  o m
    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();
    cust.name = "Paul";
    cust.password = "password";

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

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

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

}

From source file:net.mindengine.oculus.frontend.web.Auth.java

public static User decodeUser(String encodedString) {
    try {/*from w  ww .j  a v  a2s. c o m*/
        ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(Base64.decodeBase64(encodedString.getBytes())));
        SealedObject sealedObject = (SealedObject) ois.readObject();
        ois.close();

        Cipher dcipher = Cipher.getInstance("DES");
        dcipher.init(Cipher.DECRYPT_MODE, secrectAuthKey);

        User user = (User) sealedObject.getObject(dcipher);
        return user;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.floragunn.searchguard.util.SecurityUtil.java

public static Serializable decryptAnDeserializeObject(final String string, final SecretKey key) {

    if (string == null) {
        throw new IllegalArgumentException("string must not be null");
    }//from ww  w  .j a v  a2 s.  c om

    try {
        final byte[] userr = BaseEncoding.base64().decode(string);
        final ByteArrayInputStream bis = new ByteArrayInputStream(userr);
        final ObjectInputStream in = new ObjectInputStream(bis);
        final SealedObject ud = (SealedObject) in.readObject();
        return (Serializable) ud.getObject(key);
    } catch (final Exception e) {
        log.error(e.toString(), e);
        throw new ElasticsearchException(e.toString());
    }
}

From source file:com.google.u2f.gaedemo.impl.DataStoreImpl.java

@Override
public EnrollSessionData getEnrollSessionData(String sessionId) {
    SecretKey key = new SecretKeySpec(SecretKeys.get().sessionEncryptionKey(), "AES");

    byte[] serialized = Base64.decodeBase64(sessionId);
    ByteArrayInputStream inner = new ByteArrayInputStream(serialized);
    try {//from ww w  .j  a  v  a  2s .c o  m
        ObjectInputStream in = new ObjectInputStream(inner);

        SealedObject sealed = (SealedObject) in.readObject();
        return (EnrollSessionData) sealed.getObject(key);
    } catch (InvalidKeyException | ClassNotFoundException | NoSuchAlgorithmException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.alfresco.encryption.AbstractEncryptor.java

@Override
public Serializable unsealObject(String keyAlias, Serializable input) throws InvalidKeyException {
    if (input == null) {
        return input;
    }/*w w w.  j  a  v a2  s  . c o  m*/
    // Don't unseal it if it is not sealed
    if (!(input instanceof SealedObject)) {
        return input;
    }
    // Get the Key, rather than a Cipher
    Key key = keyProvider.getKey(keyAlias);
    if (key == null) {
        // The client will be expecting to unseal the object
        throw new IllegalStateException("No key matching " + keyAlias + ".  Cannot unseal object.");
    }
    // Unseal it using the key
    SealedObject sealedInput = (SealedObject) input;
    try {
        Serializable output = (Serializable) sealedInput.getObject(key);
        // Done
        return output;
    } catch (InvalidKeyException e) {
        // let these through, can be useful to client code to know this is the cause
        throw e;
    } catch (Exception e) {
        throw new AlfrescoRuntimeException("Failed to unseal object", e);
    }
}