Class AwsCrypto
- java.lang.Object
-
- com.amazonaws.encryptionsdk.AwsCrypto
-
public class AwsCrypto extends Object
Provides the primary entry-point to the AWS Encryption SDK. All encryption and decryption operations should start here. Most people will want to use eitherencryptData(MasterKeyProvider, byte[], Map)anddecryptData(MasterKeyProvider, byte[])to encrypt/decrypt things.The core concepts (and classes) in this SDK are:
AwsCryptoprovides the primary way to encrypt/decrypt data. It can operate on byte-arrays, streams, orStrings. This data is encrypted using the specifedCryptoAlgorithmand aDataKeywhich is unique to each encrypted message. ThisDataKeyis then encrypted using one (or more)MasterKeys. The process is reversed on decryption with the code selecting a copy of theDataKeyprotected by a usableMasterKey, decrypting theDataKey, and then decrypted the message.The main way to get a
MasterKeyis through the use of aMasterKeyProvider. This provides a common interface for the AwsEncryptionSdk to find and retrieveMasterKeys. (SomeMasterKeyscan also be constructed directly.)AwsCryptouses theMasterKeyProviderto determine whichMasterKeysshould be used to encrypt theDataKeysby callingMasterKeyProvider.getMasterKeysForEncryption(MasterKeyRequest). When more than oneMasterKeyis returned, the firstMasterKeysis used to create theDataKeysby callingMasterKey.generateDataKey(CryptoAlgorithm,java.util.Map). All of the otherMasterKeysare then used to re-encrypt thatDataKeywithMasterKey.encryptDataKey(CryptoAlgorithm,java.util.Map,DataKey). This list ofEncryptedDataKeys(the sameDataKeypossibly encrypted multiple times) is stored in theCiphertextHeaders.AwsCryptoalso uses theMasterKeyProviderto decrypt one of theEncryptedDataKeysfrom the header to retrieve the actualDataKeynecessary to decrypt the message.Any place a
MasterKeyProvideris used, aMasterKeycan be used instead. TheMasterKeywill behave as aMasterKeyProviderwhich is only capable of providing itself. This is often useful when only oneMasterKeyis being used.Note regarding the use of generics: This library makes heavy use of generics to provide type safety to advanced developers. The great majority of users should be able to just use the provided type parameters or the
?wildcard.
-
-
Constructor Summary
Constructors Constructor Description AwsCrypto()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CryptoInputStream<?>createDecryptingStream(CryptoMaterialsManager materialsManager, InputStream is)Returns aCryptoInputStreamwhich decrypts the data after reading it from the underlyingInputStream.CryptoOutputStream<?>createDecryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)Returns aCryptoOutputStreamwhich decrypts the data prior to passing it onto the underlyingOutputStream.<K extends MasterKey<K>>
CryptoInputStream<K>createDecryptingStream(MasterKeyProvider<K> provider, InputStream is)Returns aCryptoInputStreamwhich decrypts the data after reading it from the underlyingInputStream.<K extends MasterKey<K>>
CryptoOutputStream<K>createDecryptingStream(MasterKeyProvider<K> provider, OutputStream os)Returns aCryptoOutputStreamwhich decrypts the data prior to passing it onto the underlyingOutputStream.CryptoInputStream<?>createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is)Returns the equivalent to callingcreateEncryptingStream(CryptoMaterialsManager, InputStream, Map)with an emptyencryptionContext.CryptoInputStream<?>createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is, Map<String,String> encryptionContext)Returns aCryptoInputStreamwhich encrypts the data after reading it from the underlyingInputStream.CryptoOutputStream<?>createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)Returns the equivalent to callingcreateEncryptingStream(CryptoMaterialsManager, OutputStream, Map)with an emptyencryptionContext.CryptoOutputStream<?>createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os, Map<String,String> encryptionContext)Returns aCryptoOutputStreamwhich encrypts the data prior to passing it onto the underlyingOutputStream.<K extends MasterKey<K>>
CryptoInputStream<K>createEncryptingStream(MasterKeyProvider<K> provider, InputStream is)Returns the equivalent to callingcreateEncryptingStream(MasterKeyProvider, InputStream, Map)with an emptyencryptionContext.<K extends MasterKey<K>>
CryptoInputStream<K>createEncryptingStream(MasterKeyProvider<K> provider, InputStream is, Map<String,String> encryptionContext)Returns aCryptoInputStreamwhich encrypts the data after reading it from the underlyingInputStream.<K extends MasterKey<K>>
CryptoOutputStream<K>createEncryptingStream(MasterKeyProvider<K> provider, OutputStream os)Returns the equivalent to callingcreateEncryptingStream(MasterKeyProvider, OutputStream, Map)with an emptyencryptionContext.<K extends MasterKey<K>>
CryptoOutputStream<K>createEncryptingStream(MasterKeyProvider<K> provider, OutputStream os, Map<String,String> encryptionContext)Returns aCryptoOutputStreamwhich encrypts the data prior to passing it onto the underlyingOutputStream.CryptoResult<byte[],?>decryptData(CryptoMaterialsManager materialsManager, byte[] ciphertext)Decrypts the provided ciphertext by delegating to the provided materialsManager to obtain the decryptedDataKey.CryptoResult<byte[],?>decryptData(CryptoMaterialsManager materialsManager, ParsedCiphertext ciphertext)<K extends MasterKey<K>>
CryptoResult<byte[],K>decryptData(MasterKeyProvider<K> provider, byte[] ciphertext)Decrypts the providedciphertextby requesting that theproviderunwrap any usableDataKeyin the ciphertext and then decrypts the ciphertext using thatDataKey.<K extends MasterKey<K>>
CryptoResult<byte[],K>decryptData(MasterKeyProvider<K> provider, ParsedCiphertext ciphertext)CryptoResult<String,?>decryptString(CryptoMaterialsManager provider, String ciphertext)Base64 decodes theciphertextprior to decryption and then treats the results as a UTF-8 encoded string.<K extends MasterKey<K>>
CryptoResult<String,K>decryptString(MasterKeyProvider<K> provider, String ciphertext)Base64 decodes theciphertextprior to decryption and then treats the results as a UTF-8 encoded string.CryptoResult<byte[],?>encryptData(CryptoMaterialsManager materialsManager, byte[] plaintext)Returns the equivalent to callingencryptData(CryptoMaterialsManager, byte[], Map)with an emptyencryptionContext.CryptoResult<byte[],?>encryptData(CryptoMaterialsManager materialsManager, byte[] plaintext, Map<String,String> encryptionContext)Returns an encrypted form ofplaintextthat has been protected withDataKeysthat are in turn protected by the given CryptoMaterialsProvider.<K extends MasterKey<K>>
CryptoResult<byte[],K>encryptData(MasterKeyProvider<K> provider, byte[] plaintext)Returns the equivalent to callingencryptData(MasterKeyProvider, byte[], Map)with an emptyencryptionContext.<K extends MasterKey<K>>
CryptoResult<byte[],K>encryptData(MasterKeyProvider<K> provider, byte[] plaintext, Map<String,String> encryptionContext)Returns an encrypted form ofplaintextthat has been protected withDataKeysthat are in turn protected byMasterKeysprovided byprovider.CryptoResult<String,?>encryptString(CryptoMaterialsManager materialsManager, String plaintext)Returns the equivalent to callingencryptString(CryptoMaterialsManager, String, Map)with an emptyencryptionContext.CryptoResult<String,?>encryptString(CryptoMaterialsManager materialsManager, String plaintext, Map<String,String> encryptionContext)CallsencryptData(CryptoMaterialsManager, byte[], Map)on the UTF-8 encoded bytes ofplaintextand base64 encodes the result.<K extends MasterKey<K>>
CryptoResult<String,K>encryptString(MasterKeyProvider<K> provider, String plaintext)Returns the equivalent to callingencryptString(MasterKeyProvider, String, Map)with an emptyencryptionContext.<K extends MasterKey<K>>
CryptoResult<String,K>encryptString(MasterKeyProvider<K> provider, String plaintext, Map<String,String> encryptionContext)CallsencryptData(MasterKeyProvider, byte[], Map)on the UTF-8 encoded bytes ofplaintextand base64 encodes the result.longestimateCiphertextSize(CryptoMaterialsManager materialsManager, int plaintextSize)Returns the equivalent to callingestimateCiphertextSize(CryptoMaterialsManager, int, Map)with an emptyencryptionContext.longestimateCiphertextSize(CryptoMaterialsManager materialsManager, int plaintextSize, Map<String,String> encryptionContext)Returns the best estimate for the output length of encrypting a plaintext with the providedplaintextSizeandencryptionContext.<K extends MasterKey<K>>
longestimateCiphertextSize(MasterKeyProvider<K> provider, int plaintextSize)Returns the equivalent to callingestimateCiphertextSize(MasterKeyProvider, int, Map)with an emptyencryptionContext.<K extends MasterKey<K>>
longestimateCiphertextSize(MasterKeyProvider<K> provider, int plaintextSize, Map<String,String> encryptionContext)Returns the best estimate for the output length of encrypting a plaintext with the providedplaintextSizeandencryptionContext.static CryptoAlgorithmgetDefaultCryptoAlgorithm()Returns theCryptoAlgorithmto be used for encryption when none is explicitly selected.static intgetDefaultFrameSize()Returns the frame size to use for encryption when none is explicitly selected.CryptoAlgorithmgetEncryptionAlgorithm()intgetEncryptionFrameSize()voidsetEncryptionAlgorithm(CryptoAlgorithm alg)Sets theCryptoAlgorithmto use when encrypting data.voidsetEncryptionFrameSize(int frameSize)Sets the framing size to use when encrypting data.
-
-
-
Method Detail
-
getDefaultCryptoAlgorithm
public static CryptoAlgorithm getDefaultCryptoAlgorithm()
Returns theCryptoAlgorithmto be used for encryption when none is explicitly selected. Currently it isCryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384.
-
getDefaultFrameSize
public static int getDefaultFrameSize()
Returns the frame size to use for encryption when none is explicitly selected. Currently it is 4096.
-
setEncryptionAlgorithm
public void setEncryptionAlgorithm(CryptoAlgorithm alg)
Sets theCryptoAlgorithmto use when encrypting data. This has no impact on decryption.
-
getEncryptionAlgorithm
public CryptoAlgorithm getEncryptionAlgorithm()
-
setEncryptionFrameSize
public void setEncryptionFrameSize(int frameSize)
Sets the framing size to use when encrypting data. This has no impact on decryption. IfframeSizeis 0, then framing is disabled and the entire plaintext will be encrypted in a single block. Note that during encryption arrays of this size will be allocated. Using extremely large frame sizes may pose compatibility issues when the decryptor is running on 32-bit systems. Additionally, Java VM limits may set a platform-specific upper bound to frame sizes.
-
getEncryptionFrameSize
public int getEncryptionFrameSize()
-
estimateCiphertextSize
public <K extends MasterKey<K>> long estimateCiphertextSize(MasterKeyProvider<K> provider, int plaintextSize, Map<String,String> encryptionContext)
Returns the best estimate for the output length of encrypting a plaintext with the providedplaintextSizeandencryptionContext. The actual ciphertext may be shorter. This method is equivalent to callingestimateCiphertextSize(CryptoMaterialsManager, int, Map)with aDefaultCryptoMaterialsManagerbased on the given provider.
-
estimateCiphertextSize
public long estimateCiphertextSize(CryptoMaterialsManager materialsManager, int plaintextSize, Map<String,String> encryptionContext)
Returns the best estimate for the output length of encrypting a plaintext with the providedplaintextSizeandencryptionContext. The actual ciphertext may be shorter.
-
estimateCiphertextSize
public <K extends MasterKey<K>> long estimateCiphertextSize(MasterKeyProvider<K> provider, int plaintextSize)
Returns the equivalent to callingestimateCiphertextSize(MasterKeyProvider, int, Map)with an emptyencryptionContext.
-
estimateCiphertextSize
public long estimateCiphertextSize(CryptoMaterialsManager materialsManager, int plaintextSize)
Returns the equivalent to callingestimateCiphertextSize(CryptoMaterialsManager, int, Map)with an emptyencryptionContext.
-
encryptData
public <K extends MasterKey<K>> CryptoResult<byte[],K> encryptData(MasterKeyProvider<K> provider, byte[] plaintext, Map<String,String> encryptionContext)
Returns an encrypted form ofplaintextthat has been protected withDataKeysthat are in turn protected byMasterKeysprovided byprovider. This method is equivalent to callingencryptData(CryptoMaterialsManager, byte[], Map)using aDefaultCryptoMaterialsManagerbased on the given provider.
-
encryptData
public CryptoResult<byte[],?> encryptData(CryptoMaterialsManager materialsManager, byte[] plaintext, Map<String,String> encryptionContext)
Returns an encrypted form ofplaintextthat has been protected withDataKeysthat are in turn protected by the given CryptoMaterialsProvider.
-
encryptData
public <K extends MasterKey<K>> CryptoResult<byte[],K> encryptData(MasterKeyProvider<K> provider, byte[] plaintext)
Returns the equivalent to callingencryptData(MasterKeyProvider, byte[], Map)with an emptyencryptionContext.
-
encryptData
public CryptoResult<byte[],?> encryptData(CryptoMaterialsManager materialsManager, byte[] plaintext)
Returns the equivalent to callingencryptData(CryptoMaterialsManager, byte[], Map)with an emptyencryptionContext.
-
encryptString
public <K extends MasterKey<K>> CryptoResult<String,K> encryptString(MasterKeyProvider<K> provider, String plaintext, Map<String,String> encryptionContext)
CallsencryptData(MasterKeyProvider, byte[], Map)on the UTF-8 encoded bytes ofplaintextand base64 encodes the result.
-
encryptString
public CryptoResult<String,?> encryptString(CryptoMaterialsManager materialsManager, String plaintext, Map<String,String> encryptionContext)
CallsencryptData(CryptoMaterialsManager, byte[], Map)on the UTF-8 encoded bytes ofplaintextand base64 encodes the result.
-
encryptString
public <K extends MasterKey<K>> CryptoResult<String,K> encryptString(MasterKeyProvider<K> provider, String plaintext)
Returns the equivalent to callingencryptString(MasterKeyProvider, String, Map)with an emptyencryptionContext.
-
encryptString
public CryptoResult<String,?> encryptString(CryptoMaterialsManager materialsManager, String plaintext)
Returns the equivalent to callingencryptString(CryptoMaterialsManager, String, Map)with an emptyencryptionContext.
-
decryptData
public <K extends MasterKey<K>> CryptoResult<byte[],K> decryptData(MasterKeyProvider<K> provider, byte[] ciphertext)
Decrypts the providedciphertextby requesting that theproviderunwrap any usableDataKeyin the ciphertext and then decrypts the ciphertext using thatDataKey.
-
decryptData
public CryptoResult<byte[],?> decryptData(CryptoMaterialsManager materialsManager, byte[] ciphertext)
Decrypts the provided ciphertext by delegating to the provided materialsManager to obtain the decryptedDataKey.- Parameters:
materialsManager-ciphertext-- Returns:
-
decryptData
public <K extends MasterKey<K>> CryptoResult<byte[],K> decryptData(MasterKeyProvider<K> provider, ParsedCiphertext ciphertext)
- See Also:
decryptData(MasterKeyProvider, byte[])
-
decryptData
public CryptoResult<byte[],?> decryptData(CryptoMaterialsManager materialsManager, ParsedCiphertext ciphertext)
-
decryptString
public <K extends MasterKey<K>> CryptoResult<String,K> decryptString(MasterKeyProvider<K> provider, String ciphertext)
Base64 decodes theciphertextprior to decryption and then treats the results as a UTF-8 encoded string.- See Also:
decryptData(MasterKeyProvider, byte[])
-
decryptString
public CryptoResult<String,?> decryptString(CryptoMaterialsManager provider, String ciphertext)
Base64 decodes theciphertextprior to decryption and then treats the results as a UTF-8 encoded string.
-
createEncryptingStream
public <K extends MasterKey<K>> CryptoOutputStream<K> createEncryptingStream(MasterKeyProvider<K> provider, OutputStream os, Map<String,String> encryptionContext)
Returns aCryptoOutputStreamwhich encrypts the data prior to passing it onto the underlyingOutputStream.
-
createEncryptingStream
public CryptoOutputStream<?> createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os, Map<String,String> encryptionContext)
Returns aCryptoOutputStreamwhich encrypts the data prior to passing it onto the underlyingOutputStream.
-
createEncryptingStream
public <K extends MasterKey<K>> CryptoOutputStream<K> createEncryptingStream(MasterKeyProvider<K> provider, OutputStream os)
Returns the equivalent to callingcreateEncryptingStream(MasterKeyProvider, OutputStream, Map)with an emptyencryptionContext.
-
createEncryptingStream
public CryptoOutputStream<?> createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)
Returns the equivalent to callingcreateEncryptingStream(CryptoMaterialsManager, OutputStream, Map)with an emptyencryptionContext.
-
createEncryptingStream
public <K extends MasterKey<K>> CryptoInputStream<K> createEncryptingStream(MasterKeyProvider<K> provider, InputStream is, Map<String,String> encryptionContext)
Returns aCryptoInputStreamwhich encrypts the data after reading it from the underlyingInputStream.
-
createEncryptingStream
public CryptoInputStream<?> createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is, Map<String,String> encryptionContext)
Returns aCryptoInputStreamwhich encrypts the data after reading it from the underlyingInputStream.
-
createEncryptingStream
public <K extends MasterKey<K>> CryptoInputStream<K> createEncryptingStream(MasterKeyProvider<K> provider, InputStream is)
Returns the equivalent to callingcreateEncryptingStream(MasterKeyProvider, InputStream, Map)with an emptyencryptionContext.
-
createEncryptingStream
public CryptoInputStream<?> createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is)
Returns the equivalent to callingcreateEncryptingStream(CryptoMaterialsManager, InputStream, Map)with an emptyencryptionContext.
-
createDecryptingStream
public <K extends MasterKey<K>> CryptoOutputStream<K> createDecryptingStream(MasterKeyProvider<K> provider, OutputStream os)
Returns aCryptoOutputStreamwhich decrypts the data prior to passing it onto the underlyingOutputStream.
-
createDecryptingStream
public <K extends MasterKey<K>> CryptoInputStream<K> createDecryptingStream(MasterKeyProvider<K> provider, InputStream is)
Returns aCryptoInputStreamwhich decrypts the data after reading it from the underlyingInputStream.
-
createDecryptingStream
public CryptoOutputStream<?> createDecryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)
Returns aCryptoOutputStreamwhich decrypts the data prior to passing it onto the underlyingOutputStream.
-
createDecryptingStream
public CryptoInputStream<?> createDecryptingStream(CryptoMaterialsManager materialsManager, InputStream is)
Returns aCryptoInputStreamwhich decrypts the data after reading it from the underlyingInputStream.
-
-