com.healthmarketscience.jackcess.impl
Class BaseCryptCodecHandler

java.lang.Object
  extended by com.healthmarketscience.jackcess.impl.BaseCryptCodecHandler
All Implemented Interfaces:
com.healthmarketscience.jackcess.impl.CodecHandler
Direct Known Subclasses:
BaseJetCryptCodecHandler, OfficeCryptCodecHandler

public abstract class BaseCryptCodecHandler
extends Object
implements com.healthmarketscience.jackcess.impl.CodecHandler

Common CodecHandler support.

Author:
Vladimir Berezniker

Field Summary
static boolean CIPHER_DECRYPT_MODE
           
static boolean CIPHER_ENCRYPT_MODE
           
 
Constructor Summary
protected BaseCryptCodecHandler(com.healthmarketscience.jackcess.impl.PageChannel channel, byte[] encodingKey)
           
 
Method Summary
static byte[] applyPageNumber(byte[] key, int offset, int pageNumber)
          Returns a copy of the given key with the bytes of the given pageNumber applied at the given offset using XOR.
protected  void blockDecrypt(ByteBuffer inPage, ByteBuffer outPage, int pageNumber)
          Decrypts the given buffer using a block cipher.
protected  byte[] blockDecryptBytes(byte[] keyBytes, byte[] iv, byte[] encBytes)
          Decrypts the given bytes using a block cipher configured with the given key and IV into a new byte[].
protected  ByteBuffer blockEncrypt(ByteBuffer buffer, int pageNumber)
          Encrypts the given buffer using a block cipher and returns the encrypted buffer.
protected abstract  org.bouncycastle.crypto.CipherParameters computeCipherParams(int pageNumber)
          Generates the cipher parameters for the given page number.
protected static byte[] decryptBytes(org.bouncycastle.crypto.BufferedBlockCipher cipher, byte[] encBytes)
          Decrypts the given bytes using a block cipher into a new byte[].
protected static byte[] decryptBytes(StreamCipherCompat cipher, byte[] encBytes)
          Decrypts the given bytes using a stream cipher into a new byte[].
protected static org.bouncycastle.crypto.BufferedBlockCipher decryptInit(org.bouncycastle.crypto.BufferedBlockCipher cipher, org.bouncycastle.crypto.CipherParameters params)
          Inits the given cipher for decryption with the given params.
protected static StreamCipherCompat decryptInit(StreamCipherCompat cipher, org.bouncycastle.crypto.CipherParameters params)
          Inits the given cipher for decryption with the given params.
protected static org.bouncycastle.crypto.BufferedBlockCipher encryptInit(org.bouncycastle.crypto.BufferedBlockCipher cipher, org.bouncycastle.crypto.CipherParameters params)
          Inits the given cipher for encryption with the given params.
protected static StreamCipherCompat encryptInit(StreamCipherCompat cipher, org.bouncycastle.crypto.CipherParameters params)
          Inits the given cipher for encryption with the given params.
static byte[] fill(byte[] bytes, int value)
          Fills the given array with the given value and returns it.
static byte[] fixToLength(byte[] bytes, int len)
           
static byte[] fixToLength(byte[] bytes, int len, int padByte)
           
protected  org.bouncycastle.crypto.BufferedBlockCipher getBlockCipher()
           
protected  org.bouncycastle.crypto.CipherParameters getCipherParams(int pageNumber)
           
protected  byte[] getEncodingKey()
           
protected  byte[] getEncodingKey(int pageNumber)
          Gets the encoding key combined with the given page number.
protected  StreamCipherCompat getStreamCipher()
           
protected  ByteBuffer getTempBuffer()
           
static byte[] hash(org.bouncycastle.crypto.Digest digest, byte[] bytes)
          Hashes the given bytes using the given digest and returns the result.
static byte[] hash(org.bouncycastle.crypto.Digest digest, byte[] bytes1, byte[] bytes2)
          Hashes the given bytes1 and bytes2 using the given digest and returns the result.
static byte[] hash(org.bouncycastle.crypto.Digest digest, byte[] bytes1, byte[] bytes2, int resultLen)
          Hashes the given bytes1 and bytes2 using the given digest and returns the hash fixed to the given length.
static byte[] hash(org.bouncycastle.crypto.Digest digest, byte[] bytes, int resultLen)
          Hashes the given bytes using the given digest and returns the hash fixed to the given length.
protected static boolean isBlankKey(byte[] key)
           
protected static byte[] processBytesFully(org.bouncycastle.crypto.BufferedBlockCipher cipher, byte[] inArray, byte[] outArray, int inLen)
          Processes all the bytes for the given block cipher.
protected static ByteBuffer readHeaderPage(com.healthmarketscience.jackcess.impl.PageChannel pageChannel)
          Reads and returns the header page (page 0) from the given pageChannel.
protected  void streamDecrypt(ByteBuffer buffer, int pageNumber)
          Decrypts the given buffer using a stream cipher.
protected  ByteBuffer streamEncrypt(ByteBuffer buffer, int pageNumber, int pageOffset)
          Encrypts the given buffer using a stream cipher and returns the encrypted buffer.
 String toString()
           
static ByteBuffer wrap(byte[] bytes)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.healthmarketscience.jackcess.impl.CodecHandler
canDecodeInline, canEncodePartialPage, decodePage, encodePage
 

Field Detail

CIPHER_DECRYPT_MODE

public static final boolean CIPHER_DECRYPT_MODE
See Also:
Constant Field Values

CIPHER_ENCRYPT_MODE

public static final boolean CIPHER_ENCRYPT_MODE
See Also:
Constant Field Values
Constructor Detail

BaseCryptCodecHandler

protected BaseCryptCodecHandler(com.healthmarketscience.jackcess.impl.PageChannel channel,
                                byte[] encodingKey)
Method Detail

getCipherParams

protected org.bouncycastle.crypto.CipherParameters getCipherParams(int pageNumber)

getEncodingKey

protected byte[] getEncodingKey()

getStreamCipher

protected StreamCipherCompat getStreamCipher()

getBlockCipher

protected org.bouncycastle.crypto.BufferedBlockCipher getBlockCipher()

getTempBuffer

protected ByteBuffer getTempBuffer()

streamDecrypt

protected void streamDecrypt(ByteBuffer buffer,
                             int pageNumber)
Decrypts the given buffer using a stream cipher.


streamEncrypt

protected ByteBuffer streamEncrypt(ByteBuffer buffer,
                                   int pageNumber,
                                   int pageOffset)
Encrypts the given buffer using a stream cipher and returns the encrypted buffer.


blockDecrypt

protected void blockDecrypt(ByteBuffer inPage,
                            ByteBuffer outPage,
                            int pageNumber)
Decrypts the given buffer using a block cipher.


blockEncrypt

protected ByteBuffer blockEncrypt(ByteBuffer buffer,
                                  int pageNumber)
Encrypts the given buffer using a block cipher and returns the encrypted buffer.


toString

public String toString()
Overrides:
toString in class Object

decryptInit

protected static StreamCipherCompat decryptInit(StreamCipherCompat cipher,
                                                org.bouncycastle.crypto.CipherParameters params)
Inits the given cipher for decryption with the given params.


encryptInit

protected static StreamCipherCompat encryptInit(StreamCipherCompat cipher,
                                                org.bouncycastle.crypto.CipherParameters params)
Inits the given cipher for encryption with the given params.


decryptInit

protected static org.bouncycastle.crypto.BufferedBlockCipher decryptInit(org.bouncycastle.crypto.BufferedBlockCipher cipher,
                                                                         org.bouncycastle.crypto.CipherParameters params)
Inits the given cipher for decryption with the given params.


encryptInit

protected static org.bouncycastle.crypto.BufferedBlockCipher encryptInit(org.bouncycastle.crypto.BufferedBlockCipher cipher,
                                                                         org.bouncycastle.crypto.CipherParameters params)
Inits the given cipher for encryption with the given params.


decryptBytes

protected static byte[] decryptBytes(StreamCipherCompat cipher,
                                     byte[] encBytes)
Decrypts the given bytes using a stream cipher into a new byte[].


blockDecryptBytes

protected byte[] blockDecryptBytes(byte[] keyBytes,
                                   byte[] iv,
                                   byte[] encBytes)
Decrypts the given bytes using a block cipher configured with the given key and IV into a new byte[].


decryptBytes

protected static byte[] decryptBytes(org.bouncycastle.crypto.BufferedBlockCipher cipher,
                                     byte[] encBytes)
Decrypts the given bytes using a block cipher into a new byte[].


getEncodingKey

protected byte[] getEncodingKey(int pageNumber)
Gets the encoding key combined with the given page number.


readHeaderPage

protected static ByteBuffer readHeaderPage(com.healthmarketscience.jackcess.impl.PageChannel pageChannel)
                                    throws IOException
Reads and returns the header page (page 0) from the given pageChannel.

Throws:
IOException

applyPageNumber

public static byte[] applyPageNumber(byte[] key,
                                     int offset,
                                     int pageNumber)
Returns a copy of the given key with the bytes of the given pageNumber applied at the given offset using XOR.


hash

public static byte[] hash(org.bouncycastle.crypto.Digest digest,
                          byte[] bytes)
Hashes the given bytes using the given digest and returns the result.


hash

public static byte[] hash(org.bouncycastle.crypto.Digest digest,
                          byte[] bytes1,
                          byte[] bytes2)
Hashes the given bytes1 and bytes2 using the given digest and returns the result.


hash

public static byte[] hash(org.bouncycastle.crypto.Digest digest,
                          byte[] bytes,
                          int resultLen)
Hashes the given bytes using the given digest and returns the hash fixed to the given length.


hash

public static byte[] hash(org.bouncycastle.crypto.Digest digest,
                          byte[] bytes1,
                          byte[] bytes2,
                          int resultLen)
Hashes the given bytes1 and bytes2 using the given digest and returns the hash fixed to the given length.


fixToLength

public static byte[] fixToLength(byte[] bytes,
                                 int len)
Returns:
a byte array of the given length, truncating or padding the given byte array as necessary.

fixToLength

public static byte[] fixToLength(byte[] bytes,
                                 int len,
                                 int padByte)
Returns:
a byte array of the given length, truncating or padding the given byte array as necessary using the given padByte.

wrap

public static ByteBuffer wrap(byte[] bytes)
Returns:
a new ByteBuffer wrapping the given bytes with the appropriate byte order

fill

public static byte[] fill(byte[] bytes,
                          int value)
Fills the given array with the given value and returns it.


processBytesFully

protected static byte[] processBytesFully(org.bouncycastle.crypto.BufferedBlockCipher cipher,
                                          byte[] inArray,
                                          byte[] outArray,
                                          int inLen)
                                   throws org.bouncycastle.crypto.InvalidCipherTextException
Processes all the bytes for the given block cipher.

Throws:
org.bouncycastle.crypto.InvalidCipherTextException

isBlankKey

protected static boolean isBlankKey(byte[] key)
Returns:
true if the given bytes are all 0, false otherwise

computeCipherParams

protected abstract org.bouncycastle.crypto.CipherParameters computeCipherParams(int pageNumber)
Generates the cipher parameters for the given page number.



Copyright © 2010-2017 Health Market Science. All Rights Reserved.