Java Code Examples for javax.crypto.SealedObject#getObject()

The following examples show how to use javax.crypto.SealedObject#getObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WalletUtils.java    From blockchain-java with Apache License 2.0 6 votes vote down vote up
/**
 * 加载钱包数据
 */
private Wallets loadFromDisk() {
    try {
        SecretKeySpec sks = new SecretKeySpec(CIPHER_TEXT, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, sks);
        @Cleanup CipherInputStream cipherInputStream = new CipherInputStream(
                new BufferedInputStream(new FileInputStream(WALLET_FILE)), cipher);
        @Cleanup ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
        SealedObject sealedObject = (SealedObject) inputStream.readObject();
        return (Wallets) sealedObject.getObject(cipher);
    } catch (Exception e) {
        log.error("Fail to load wallet from disk ! ", e);
        throw new RuntimeException("Fail to load wallet from disk ! ");
    }
}
 
Example 2
Source File: RangerKeyStore.java    From ranger with Apache License 2.0 6 votes vote down vote up
private Key unsealKey(SealedObject sealedKey, char[] password) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("==> RangerKeyStore.unsealKey()");
    }
    // Create SecretKey
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndTripleDES");
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
    pbeKeySpec.clearPassword();

    // Get the AlgorithmParameters from RangerSealedObject
    AlgorithmParameters algorithmParameters = null;
    if (sealedKey instanceof RangerSealedObject) {
        algorithmParameters = ((RangerSealedObject) sealedKey).getParameters();
    } else {
        algorithmParameters = new RangerSealedObject(sealedKey).getParameters();
    }

    // Unseal the Key
    Cipher cipher = Cipher.getInstance("PBEWithMD5AndTripleDES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, algorithmParameters);
    if (logger.isDebugEnabled()) {
        logger.debug("<== RangerKeyStore.unsealKey()");
    }
    return (Key) sealedKey.getObject(cipher);
}
 
Example 3
Source File: CryptoSerialization.java    From JPPF with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(final InputStream is) throws Exception {
  // start by reading the secret key to use to decrypt the data
  final DataInputStream dis = new DataInputStream(is);
  // read the length of the key
  final int keyLength = dis.readInt();
  // read the encrypted key
  final byte[] keyBytes = new byte[keyLength];
  int count = 0;
  while (count < keyLength) {
    final int n = dis.read(keyBytes, count, keyLength - count);
    if (n > 0) count += n;
    else throw new EOFException("could only read " + count + " bytes of the key, out of " + keyLength);
  }
  // decrypt the key using the initial key stored in the keystore
  Cipher cipher = Cipher.getInstance(Helper.getTransformation());
  cipher.init(Cipher.UNWRAP_MODE, getSecretKey(), getInitializationVector());
  final SecretKey key = (SecretKey) cipher.unwrap(keyBytes, Helper.getAlgorithm(), Cipher.SECRET_KEY);

  // get a new cipher for the actual decryption
  cipher = Cipher.getInstance(Helper.getTransformation());
  // init the cipher in decryption mode with the retireved key
  cipher.init(Cipher.DECRYPT_MODE, key, getInitializationVector());
  // deserialize a sealed (encrypted) object
  final SealedObject sealed = (SealedObject) getDelegate().deserialize(is);
  // decrypt the sealed object into the plain riginal object
  return sealed.getObject(cipher);
}
 
Example 4
Source File: ObjectSealer.java    From chvote-1-0 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Parses a SealedObject from the given byte array and retrieves the original wrapped object
 *
 * @param encryptedObject a byte array representing a SealedObject
 * @param maxBytes        the maximum size allowed for the read object
 * @return the original Serializable object
 * @throws CryptoOperationRuntimeException
 * @see #sealObject(java.io.Serializable) the matching wrapping operation
 */
public Object unsealObject(byte[] encryptedObject, long maxBytes) {
    try {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encryptedObject);
        SealedObject sealedObject = SafeObjectReader.safeReadObject(SealedObject.class, new ArrayList<>(), MAX_OBJECTS, maxBytes, byteArrayInputStream);
        return sealedObject.getObject(key);
    } catch (IOException | ClassNotFoundException | InvalidKeyException | NoSuchAlgorithmException e) {
        throw new CryptoOperationRuntimeException("cannot unseal object", e);
    }

}
 
Example 5
Source File: PasswordKey.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public final static PrivateKey readPrivateKey(Cipher decrypt_cipher, String key_file, String algorithm) throws IOException, ClassNotFoundException, GeneralSecurityException {
	URL key_url = PasswordKey.class.getResource("/" + key_file);
	ObjectInputStream is = new ObjectInputStream(key_url.openStream());
	SealedObject sealed_key = (SealedObject)is.readObject();
	byte[] encoded_registration_key = (byte[])sealed_key.getObject(decrypt_cipher);
	return KeyManager.readPrivateKey(encoded_registration_key, algorithm);
}