Class AwsCrypto
encryptData(MasterKeyProvider, byte[], Map)
and decryptData(MasterKeyProvider, byte[])
to encrypt/decrypt things.
The core concepts (and classes) in this SDK are:
AwsCrypto
provides the primary way to encrypt/decrypt data. It can operate on
byte-arrays, streams, or Strings
. This data is encrypted using the
specifed CryptoAlgorithm
and a DataKey
which is unique to each encrypted message.
This DataKey
is then encrypted using one (or more) MasterKeys
. The
process is reversed on decryption with the code selecting a copy of the DataKey
protected
by a usable MasterKey
, decrypting the DataKey
, and then decrypted the message.
The main way to get a MasterKey
is through the use of a MasterKeyProvider
.
This provides a common interface for the AwsEncryptionSdk to find and retrieve
MasterKeys
. (Some MasterKeys
can also be constructed directly.)
AwsCrypto
uses the MasterKeyProvider
to determine which MasterKeys
should be used to encrypt the DataKeys
by calling MasterKeyProvider.getMasterKeysForEncryption(MasterKeyRequest)
. When more than one
MasterKey
is returned, the first MasterKeys
is used to create the DataKeys
by
calling MasterKey.generateDataKey(CryptoAlgorithm,java.util.Map)
. All of the other
MasterKeys
are then used to re-encrypt that DataKey
with MasterKey.encryptDataKey(CryptoAlgorithm,java.util.Map,DataKey)
. This list of EncryptedDataKeys
(the same DataKey
possibly encrypted multiple times)
is stored in the CiphertextHeaders
.
AwsCrypto
also uses the MasterKeyProvider
to decrypt one of the EncryptedDataKeys
from the header to retrieve the actual DataKey
necessary to decrypt the message.
Any place a MasterKeyProvider
is used, a MasterKey
can be used instead. The
MasterKey
will behave as a MasterKeyProvider
which is only capable of providing
itself. This is often useful when only one MasterKey
is 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.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic AwsCrypto.Builder
builder()
createDecryptingStream(CryptoMaterialsManager materialsManager, InputStream is)
Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
.createDecryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)
Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
.<K extends MasterKey<K>>
CryptoInputStream<K>createDecryptingStream(MasterKeyProvider<K> provider, InputStream is)
Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
.<K extends MasterKey<K>>
CryptoOutputStream<K>createDecryptingStream(MasterKeyProvider<K> provider, OutputStream os)
Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
.createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is)
Returns the equivalent to callingcreateEncryptingStream(CryptoMaterialsManager, InputStream, Map)
with an emptyencryptionContext
.createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is, Map<String,String> encryptionContext)
Returns aCryptoInputStream
which encrypts the data after reading it from the underlyingInputStream
.createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)
Returns the equivalent to callingcreateEncryptingStream(CryptoMaterialsManager, OutputStream, Map)
with an emptyencryptionContext
.createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os, Map<String,String> encryptionContext)
Returns aCryptoOutputStream
which 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 aCryptoInputStream
which 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 aCryptoOutputStream
which encrypts the data prior to passing it onto the underlyingOutputStream
.createUnsignedMessageDecryptingStream(CryptoMaterialsManager materialsManager, InputStream is)
Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
.createUnsignedMessageDecryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)
Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
.<K extends MasterKey<K>>
CryptoInputStream<K>createUnsignedMessageDecryptingStream(MasterKeyProvider<K> provider, InputStream is)
Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
.<K extends MasterKey<K>>
CryptoOutputStream<K>createUnsignedMessageDecryptingStream(MasterKeyProvider<K> provider, OutputStream os)
Returns aCryptoOutputStream
which decrypts 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 providedciphertext
by requesting that theprovider
unwrap any usableDataKey
in 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)
Deprecated.<K extends MasterKey<K>>
CryptoResult<String,K>decryptString(MasterKeyProvider<K> provider, String ciphertext)
Deprecated.Use thedecryptData(MasterKeyProvider, byte[])
andencryptData(MasterKeyProvider, byte[], Map)
APIs instead.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 ofplaintext
that has been protected withDataKeys
that 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 ofplaintext
that has been protected withDataKeys
that are in turn protected byMasterKeys
provided byprovider
.CryptoResult<String,?>
encryptString(CryptoMaterialsManager materialsManager, String plaintext)
Deprecated.Use theencryptData(CryptoMaterialsManager, byte[])
anddecryptData(CryptoMaterialsManager, byte[])
APIs instead.CryptoResult<String,?>
encryptString(CryptoMaterialsManager materialsManager, String plaintext, Map<String,String> encryptionContext)
Deprecated.Use theencryptData(CryptoMaterialsManager, byte[], Map)
anddecryptData(CryptoMaterialsManager, byte[])
APIs instead.<K extends MasterKey<K>>
CryptoResult<String,K>encryptString(MasterKeyProvider<K> provider, String plaintext)
Deprecated.Use theencryptData(MasterKeyProvider, byte[])
anddecryptData(MasterKeyProvider, byte[])
APIs instead.<K extends MasterKey<K>>
CryptoResult<String,K>encryptString(MasterKeyProvider<K> provider, String plaintext, Map<String,String> encryptionContext)
Deprecated.Use theencryptData(MasterKeyProvider, byte[], Map)
anddecryptData(MasterKeyProvider, byte[])
APIs instead.long
estimateCiphertextSize(CryptoMaterialsManager materialsManager, int plaintextSize)
Returns the equivalent to callingestimateCiphertextSize(CryptoMaterialsManager, int, Map)
with an emptyencryptionContext
.long
estimateCiphertextSize(CryptoMaterialsManager materialsManager, int plaintextSize, Map<String,String> encryptionContext)
Returns the best estimate for the output length of encrypting a plaintext with the providedplaintextSize
andencryptionContext
.<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 providedplaintextSize
andencryptionContext
.static int
Returns the frame size to use for encryption when none is explicitly selected.int
void
Sets theCryptoAlgorithm
to use when encrypting data.void
setEncryptionFrameSize(int frameSize)
Sets the framing size to use when encrypting data.static AwsCrypto
standard()
-
Method Details
-
builder
-
toBuilder
-
standard
-
getDefaultFrameSize
public static int getDefaultFrameSize()Returns the frame size to use for encryption when none is explicitly selected. Currently it is 4096. -
setEncryptionAlgorithm
Sets theCryptoAlgorithm
to use when encrypting data. This has no impact on decryption. -
getEncryptionAlgorithm
-
setEncryptionFrameSize
public void setEncryptionFrameSize(int frameSize)Sets the framing size to use when encrypting data. This has no impact on decryption. IfframeSize
is 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 providedplaintextSize
andencryptionContext
. The actual ciphertext may be shorter.This method is equivalent to calling
estimateCiphertextSize(CryptoMaterialsManager, int, Map)
with aDefaultCryptoMaterialsManager
based 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 providedplaintextSize
andencryptionContext
. 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
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 ofplaintext
that has been protected withDataKeys
that are in turn protected byMasterKeys
provided byprovider
.This method is equivalent to calling
encryptData(CryptoMaterialsManager, byte[], Map)
using aDefaultCryptoMaterialsManager
based on the given provider. -
encryptData
public CryptoResult<byte[],?> encryptData(CryptoMaterialsManager materialsManager, byte[] plaintext, Map<String,String> encryptionContext)Returns an encrypted form ofplaintext
that has been protected withDataKeys
that 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
@Deprecated public <K extends MasterKey<K>> CryptoResult<String,K> encryptString(MasterKeyProvider<K> provider, String plaintext, Map<String,String> encryptionContext)Deprecated.Use theencryptData(MasterKeyProvider, byte[], Map)
anddecryptData(MasterKeyProvider, byte[])
APIs instead.encryptString
anddecryptString
work as expected if you use them together. However, to work with other language implementations of the AWS Encryption SDK, you need to base64-decode the output ofencryptString
and base64-encode the input todecryptString
. These deprecated APIs will be removed in the future.CallsencryptData(MasterKeyProvider, byte[], Map)
on the UTF-8 encoded bytes ofplaintext
and base64 encodes the result. -
encryptString
@Deprecated public CryptoResult<String,?> encryptString(CryptoMaterialsManager materialsManager, String plaintext, Map<String,String> encryptionContext)Deprecated.Use theencryptData(CryptoMaterialsManager, byte[], Map)
anddecryptData(CryptoMaterialsManager, byte[])
APIs instead.encryptString
anddecryptString
work as expected if you use them together. However, to work with other language implementations of the AWS Encryption SDK, you need to base64-decode the output ofencryptString
and base64-encode the input todecryptString
. These deprecated APIs will be removed in the future.CallsencryptData(CryptoMaterialsManager, byte[], Map)
on the UTF-8 encoded bytes ofplaintext
and base64 encodes the result. -
encryptString
@Deprecated public <K extends MasterKey<K>> CryptoResult<String,K> encryptString(MasterKeyProvider<K> provider, String plaintext)Deprecated.Use theencryptData(MasterKeyProvider, byte[])
anddecryptData(MasterKeyProvider, byte[])
APIs instead.encryptString
anddecryptString
work as expected if you use them together. However, to work with other language implementations of the AWS Encryption SDK, you need to base64-decode the output ofencryptString
and base64-encode the input todecryptString
. These deprecated APIs will be removed in the future.Returns the equivalent to callingencryptString(MasterKeyProvider, String, Map)
with an emptyencryptionContext
. -
encryptString
@Deprecated public CryptoResult<String,?> encryptString(CryptoMaterialsManager materialsManager, String plaintext)Deprecated.Use theencryptData(CryptoMaterialsManager, byte[])
anddecryptData(CryptoMaterialsManager, byte[])
APIs instead.encryptString
anddecryptString
work as expected if you use them together. However, to work with other language implementations of the AWS Encryption SDK, you need to base64-decode the output ofencryptString
and base64-encode the input todecryptString
. These deprecated APIs will be removed in the future.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 providedciphertext
by requesting that theprovider
unwrap any usableDataKey
in 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
- theCryptoMaterialsManager
to use for decryption operations.ciphertext
- the ciphertext to attempt to decrypt.- Returns:
- the
CryptoResult
with the decrypted data.
-
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
@Deprecated public <K extends MasterKey<K>> CryptoResult<String,K> decryptString(MasterKeyProvider<K> provider, String ciphertext)Deprecated.Use thedecryptData(MasterKeyProvider, byte[])
andencryptData(MasterKeyProvider, byte[], Map)
APIs instead.encryptString
anddecryptString
work as expected if you use them together. However, to work with other language implementations of the AWS Encryption SDK, you need to base64-decode the output ofencryptString
and base64-encode the input todecryptString
. These deprecated APIs will be removed in the future.Base64 decodes theciphertext
prior to decryption and then treats the results as a UTF-8 encoded string.- See Also:
decryptData(MasterKeyProvider, byte[])
-
decryptString
@Deprecated public CryptoResult<String,?> decryptString(CryptoMaterialsManager provider, String ciphertext)Deprecated.Use thedecryptData(CryptoMaterialsManager, byte[])
andencryptData(CryptoMaterialsManager, byte[], Map)
APIs instead.encryptString
anddecryptString
work as expected if you use them together. However, to work with other language implementations of the AWS Encryption SDK, you need to base64-decode the output ofencryptString
and base64-encode the input todecryptString
. These deprecated APIs will be removed in the future.Base64 decodes theciphertext
prior 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 aCryptoOutputStream
which encrypts the data prior to passing it onto the underlyingOutputStream
. -
createEncryptingStream
public CryptoOutputStream<?> createEncryptingStream(CryptoMaterialsManager materialsManager, OutputStream os, Map<String,String> encryptionContext)Returns aCryptoOutputStream
which 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 aCryptoInputStream
which encrypts the data after reading it from the underlyingInputStream
. -
createEncryptingStream
public CryptoInputStream<?> createEncryptingStream(CryptoMaterialsManager materialsManager, InputStream is, Map<String,String> encryptionContext)Returns aCryptoInputStream
which 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
. -
createUnsignedMessageDecryptingStream
public <K extends MasterKey<K>> CryptoOutputStream<K> createUnsignedMessageDecryptingStream(MasterKeyProvider<K> provider, OutputStream os)Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
. This version only accepts unsigned messages. -
createUnsignedMessageDecryptingStream
public <K extends MasterKey<K>> CryptoInputStream<K> createUnsignedMessageDecryptingStream(MasterKeyProvider<K> provider, InputStream is)Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
. This version only accepts unsigned messages. -
createUnsignedMessageDecryptingStream
public CryptoOutputStream<?> createUnsignedMessageDecryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
. This version only accepts unsigned messages. -
createUnsignedMessageDecryptingStream
public CryptoInputStream<?> createUnsignedMessageDecryptingStream(CryptoMaterialsManager materialsManager, InputStream is)Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
. This version only accepts unsigned messages. -
createDecryptingStream
public <K extends MasterKey<K>> CryptoOutputStream<K> createDecryptingStream(MasterKeyProvider<K> provider, OutputStream os)Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
.Note that if the encrypted message includes a trailing signature, by necessity it cannot be verified until after the decrypted plaintext has been released to the underlying
OutputStream
! This behavior can be avoided by using the non-streaming #decryptData(MasterKeyProvider, byte[]) method instead, or #createUnsignedMessageDecryptingStream(MasterKeyProvider, OutputStream) if you do not need to decrypt signed messages. -
createDecryptingStream
public <K extends MasterKey<K>> CryptoInputStream<K> createDecryptingStream(MasterKeyProvider<K> provider, InputStream is)Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
.Note that if the encrypted message includes a trailing signature, by necessity it cannot be verified until after the decrypted plaintext has been produced from the
InputStream
! This behavior can be avoided by using the non-streaming #decryptData(MasterKeyProvider, byte[]) method instead, or #createUnsignedMessageDecryptingStream(MasterKeyProvider, InputStream) if you do not need to decrypt signed messages. -
createDecryptingStream
public CryptoOutputStream<?> createDecryptingStream(CryptoMaterialsManager materialsManager, OutputStream os)Returns aCryptoOutputStream
which decrypts the data prior to passing it onto the underlyingOutputStream
.Note that if the encrypted message includes a trailing signature, by necessity it cannot be verified until after the decrypted plaintext has been released to the underlying
OutputStream
! This behavior can be avoided by using the non-streaming #decryptData(CryptoMaterialsManager, byte[]) method instead, or #createUnsignedMessageDecryptingStream(CryptoMaterialsManager, OutputStream) if you do not need to decrypt signed messages. -
createDecryptingStream
public CryptoInputStream<?> createDecryptingStream(CryptoMaterialsManager materialsManager, InputStream is)Returns aCryptoInputStream
which decrypts the data after reading it from the underlyingInputStream
.Note that if the encrypted message includes a trailing signature, by necessity it cannot be verified until after the decrypted plaintext has been produced from the
InputStream
! This behavior can be avoided by using the non-streaming #decryptData(CryptoMaterialsManager, byte[]) method instead, or #createUnsignedMessageDecryptingStream(CryptoMaterialsManager, InputStream) if you do not need to decrypt signed messages.
-
decryptData(CryptoMaterialsManager, byte[])
andencryptData(CryptoMaterialsManager, byte[], Map)
APIs instead.