Class CipherCrypto
java.lang.Object
org.cojen.tupl.ext.CipherCrypto
- All Implemented Interfaces:
Crypto
Crypto implementation which uses
Cipher and defaults to the AES algorithm. An
encrypted salt-sector initialization vector (ESSIV) scheme is
applied to all data pages in the main database file, where the initialization vector is
defined as encrypt(iv=dataPageIndex, key=dataPageKey, data=dataPageSalt). The key
and salt values are randomly generated when the database is created, and they are stored in
the database header pages. The initialization vector for the header pages is randomly
generated each time and is stored cleartext in the header pages. The secret key given to the
constructor is used for encrypting header pages, and for any redo log files.-
Constructor Summary
ConstructorsConstructorDescriptionCipherCrypto(byte[] encodedKey) Construct with an existing key, which is wrapped withSecretKeySpec, although additional keys might need to be generated.CipherCrypto(int keySize) Construct with a new key, available from thesecretKeymethod.CipherCrypto(SecretKey key, int keySize) Construct with an existing key, although additional keys might need to be generated. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringReturns "AES" by default; override to change the algorithm.final voiddecryptPage(long pageIndex, int pageSize, long srcAddr, int srcOffset, long dstAddr, int dstOffset) Called by multiple threads to decrypt a fixed-size database page.final voidencryptPage(long pageIndex, int pageSize, long srcAddr, int srcOffset, long dstAddr, int dstOffset) Called by multiple threads to encrypt a fixed-size database page.factory(byte[] encodedKey) Construct with an existing key, which is wrapped withSecretKeySpec, although additional keys might need to be generated.Construct with an existing key, although additional keys might need to be generated.protected SecretKeygenerateKey(int keySize) Called to generate a key, using thealgorithmand the given key size (bits).protected voidinitCipher(Cipher cipher, int opmode, SecretKey key) Called to initialize a new or re-usedCipher, generating a random initialization vector.protected voidinitCipher(Cipher cipher, int opmode, SecretKey key, IvParameterSpec ivSpec) Called to initialize a new or re-usedCipher, using the given initialization vector.static voidGenerates and prints a new 128-bit key.protected CipherCalled to instantiate allCipherinstances, with the given transformation.final InputStreamCalled to wrap an InputStream for supporting decryption.final OutputStreamCalled to wrap an OutputStream for supporting encryption.protected Cipherprotected CipherProvides access to the generated secret key.static StringtoString(byte[] key) Returns a String with a parseable Java byte array declaration.static StringReturns a String with a parseable Java byte array declaration.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Crypto
decryptPage, encryptPage
-
Constructor Details
-
CipherCrypto
Construct with a new key, available from thesecretKeymethod.- Parameters:
keySize- key size in bits (with AES: 128, 192, or 256)- Throws:
GeneralSecurityException
-
CipherCrypto
public CipherCrypto(byte[] encodedKey) Construct with an existing key, which is wrapped withSecretKeySpec, although additional keys might need to be generated. The key size must be permitted by the underlying algorithm, which for AES must be 128, 192, or 256 bits (16, 24, or 32 bytes). -
CipherCrypto
Construct with an existing key, although additional keys might need to be generated.- Parameters:
keySize- key size in bits for additional keys (with AES: 128, 192, or 256)
-
-
Method Details
-
main
-
factory
Construct with an existing key, which is wrapped withSecretKeySpec, although additional keys might need to be generated. The key size must be permitted by the underlying algorithm, which for AES must be 128, 192, or 256 bits (16, 24, or 32 bytes). -
factory
-
secretKey
Provides access to the generated secret key.- Throws:
IllegalStateException- if key was passed into the constructor
-
encryptPage
public final void encryptPage(long pageIndex, int pageSize, long srcAddr, int srcOffset, long dstAddr, int dstOffset) throws GeneralSecurityException Description copied from interface:CryptoCalled by multiple threads to encrypt a fixed-size database page. Encrypted length must exactly match original length.- Specified by:
encryptPagein interfaceCrypto- Parameters:
pageIndex- page index within databasesrcAddr- original unencrypted pagesrcOffset- offset into unencrypted pagedstAddr- destination for encrypted pagedstOffset- offset into encrypted page- Throws:
GeneralSecurityException
-
decryptPage
public final void decryptPage(long pageIndex, int pageSize, long srcAddr, int srcOffset, long dstAddr, int dstOffset) throws GeneralSecurityException Description copied from interface:CryptoCalled by multiple threads to decrypt a fixed-size database page. Decrypted length must exactly match encrypted length.- Specified by:
decryptPagein interfaceCrypto- Parameters:
pageIndex- page index within databasesrcAddr- encrypted pagesrcOffset- offset into encrypted pagedstAddr- destination for decrypted pagedstOffset- offset into decrypted page- Throws:
GeneralSecurityException
-
newEncryptingStream
public final OutputStream newEncryptingStream(OutputStream out) throws GeneralSecurityException, IOException Description copied from interface:CryptoCalled to wrap an OutputStream for supporting encryption. Implementation of this method must be thread-safe, but the stream doesn't need to be.- Specified by:
newEncryptingStreamin interfaceCrypto- Parameters:
out- encrypted data destination- Returns:
- stream which encrypts all data
- Throws:
GeneralSecurityExceptionIOException
-
newDecryptingStream
public final InputStream newDecryptingStream(InputStream in) throws GeneralSecurityException, IOException Description copied from interface:CryptoCalled to wrap an InputStream for supporting decryption. Implementation of this method must be thread-safe, but the stream doesn't need to be.- Specified by:
newDecryptingStreamin interfaceCrypto- Parameters:
in- encrypted data source- Returns:
- stream which decrypts all data
- Throws:
GeneralSecurityExceptionIOException
-
toString
-
toString
Returns a String with a parseable Java byte array declaration. -
algorithm
Returns "AES" by default; override to change the algorithm. -
generateKey
Called to generate a key, using thealgorithmand the given key size (bits). In general, this method is only called when creating a new database. Afterwards, the generated keys cannot change, and this method won't be called again.- Throws:
GeneralSecurityException
-
newCipher
Called to instantiate allCipherinstances, with the given transformation.- Throws:
GeneralSecurityException
-
newPageCipher
Called to instantiate aCipherfor encrypting and decrypting regular database pages, using the fixed instancealgorithm. Default mode applied is CTR, with no padding.- Throws:
GeneralSecurityException
-
newStreamCipher
Called to instantiate aCipherfor encrypting and decrypting header pages and redo logs, using the fixed instancealgorithm. Default mode applied is CTR, with no padding.- Throws:
GeneralSecurityException
-
initCipher
Called to initialize a new or re-usedCipher, generating a random initialization vector.- Throws:
GeneralSecurityException
-
initCipher
protected void initCipher(Cipher cipher, int opmode, SecretKey key, IvParameterSpec ivSpec) throws GeneralSecurityException Called to initialize a new or re-usedCipher, using the given initialization vector.- Throws:
GeneralSecurityException
-