Example usage for javax.crypto SealedObject SealedObject

List of usage examples for javax.crypto SealedObject SealedObject

Introduction

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

Prototype

public SealedObject(Serializable object, Cipher c) throws IOException, IllegalBlockSizeException 

Source Link

Document

Constructs a SealedObject from any Serializable object.

Usage

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 {
    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 = KeyGenerator.getInstance("DES").generateKey();
    writeToFile("secretkey.dat", key);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    SealedObject sealedObject = new SealedObject("THIS IS A SECRET MESSAGE!", cipher);
    writeToFile("sealed.dat", sealedObject);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey secretKey;// w  w  w  . j a  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 String encodeUser(User user) throws Exception {
    if (user == null) {
        throw new IllegalArgumentException("User should not be null");
    }//  ww w. j  av a 2s.  c  o m
    if (secrectAuthKey == null) {
        throw new IllegalArgumentException("Couldn't generate secret key");
    }

    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, secrectAuthKey);

    SealedObject sealedUser = new SealedObject(user, cipher);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(sealedUser);
    oos.close();
    return new String(Base64.encodeBase64(baos.toByteArray()));
}

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

@Override
public String storeSessionData(EnrollSessionData sessionData) {

    SecretKey key = new SecretKeySpec(SecretKeys.get().sessionEncryptionKey(), "AES");
    byte[] ivBytes = new byte[16];
    random.nextBytes(ivBytes);/*from   w ww.j  av a 2  s . co  m*/
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, IV);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }

    SealedObject sealed;
    try {
        sealed = new SealedObject(sessionData, cipher);
    } catch (IllegalBlockSizeException | IOException e) {
        throw new RuntimeException(e);
    }

    ByteArrayOutputStream out;
    try {
        out = new ByteArrayOutputStream();
        ObjectOutputStream outer = new ObjectOutputStream(out);

        outer.writeObject(sealed);
        outer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return Base64.encodeBase64URLSafeString(out.toByteArray());
}

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

public static String encryptAndSerializeObject(final Serializable object, final SecretKey key) {

    if (object == null) {
        throw new IllegalArgumentException("object must not be null");
    }/*from   w  w w.  j a  va  2 s .  c  om*/

    try {
        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        final SealedObject sealedobject = new SealedObject(object, cipher);
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(sealedobject);
        final byte[] bytes = bos.toByteArray();
        return BaseEncoding.base64().encode(bytes);
    } catch (final Exception e) {
        log.error(e.toString(), e);
        throw new ElasticsearchException(e.toString());
    }
}

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

@Override
public Serializable sealObject(String keyAlias, AlgorithmParameters params, Serializable input) {
    if (input == null) {
        return null;
    }/* ww w  .ja v a 2  s  .  c o  m*/
    Cipher cipher = getCipher(keyAlias, params, Cipher.ENCRYPT_MODE);
    if (cipher == null) {
        return input;
    }
    try {
        return new SealedObject(input, cipher);
    } catch (Exception e) {
        throw new AlfrescoRuntimeException("Failed to seal object", e);
    }
}

From source file:org.jasig.portlet.courses.dao.xml.SecureRequestCredentials.java

/**
 * @param username Username to store//from  w w  w  .j a  v a 2 s.  c  o  m
 * @param password Password to store, the array will be filled with empty strings before this constructor returns
 */
public SecureRequestCredentials(String username, char[] password) {
    try {
        final Cipher cipher = getCipher(username, Cipher.ENCRYPT_MODE);

        final SealedObject sp;
        try {
            sp = new SealedObject(password, cipher);
        } catch (IllegalBlockSizeException e) {
            throw new Error("Failed to create SealedObject. " + SecureRequestCredentials.class.getSimpleName()
                    + " will not work", e);
        } catch (IOException e) {
            throw new Error("Failed to create SealedObject. " + SecureRequestCredentials.class.getSimpleName()
                    + " will not work", e);
        }

        this.username = username;
        this.sealedPassword = sp;
    } finally {
        //Nuke the password array in memory as soon as we're done with it
        Arrays.fill(password, ' ');
    }
}