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 thesecretKey
method.CipherCrypto
(SecretKey key, int keySize) Construct with an existing key, although additional keys might need to be generated. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
Returns "AES" by default; override to change the algorithm.final void
decryptPage
(long pageIndex, int pageSize, long srcAddr, int srcOffset, long dstAddr, int dstOffset) Called by multiple threads to decrypt a fixed-size database page.final void
encryptPage
(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 SecretKey
generateKey
(int keySize) Called to generate a key, using thealgorithm
and the given key size (bits).protected void
initCipher
(Cipher cipher, int opmode, SecretKey key) Called to initialize a new or re-usedCipher
, generating a random initialization vector.protected void
initCipher
(Cipher cipher, int opmode, SecretKey key, IvParameterSpec ivSpec) Called to initialize a new or re-usedCipher
, using the given initialization vector.static void
Generates and prints a new 128-bit key.protected Cipher
Called to instantiate allCipher
instances, with the given transformation.final InputStream
Called to wrap an InputStream for supporting decryption.final OutputStream
Called to wrap an OutputStream for supporting encryption.protected Cipher
protected Cipher
Provides access to the generated secret key.static String
toString
(byte[] key) Returns a String with a parseable Java byte array declaration.static String
Returns a String with a parseable Java byte array declaration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.cojen.tupl.ext.Crypto
decryptPage, encryptPage
-
Constructor Details
-
CipherCrypto
Construct with a new key, available from thesecretKey
method.- 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:Crypto
Called by multiple threads to encrypt a fixed-size database page. Encrypted length must exactly match original length.- Specified by:
encryptPage
in 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:Crypto
Called by multiple threads to decrypt a fixed-size database page. Decrypted length must exactly match encrypted length.- Specified by:
decryptPage
in 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:Crypto
Called 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:
newEncryptingStream
in interfaceCrypto
- Parameters:
out
- encrypted data destination- Returns:
- stream which encrypts all data
- Throws:
GeneralSecurityException
IOException
-
newDecryptingStream
public final InputStream newDecryptingStream(InputStream in) throws GeneralSecurityException, IOException Description copied from interface:Crypto
Called 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:
newDecryptingStream
in interfaceCrypto
- Parameters:
in
- encrypted data source- Returns:
- stream which decrypts all data
- Throws:
GeneralSecurityException
IOException
-
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 thealgorithm
and 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 allCipher
instances, with the given transformation.- Throws:
GeneralSecurityException
-
newPageCipher
Called to instantiate aCipher
for encrypting and decrypting regular database pages, using the fixed instancealgorithm
. Default mode applied is CTR, with no padding.- Throws:
GeneralSecurityException
-
newStreamCipher
Called to instantiate aCipher
for 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
-