Class CTRMode

java.lang.Object
edu.boisestate.lowry.crypto.CTRMode
All Implemented Interfaces:
SymmetricCipher

public class CTRMode extends Object implements SymmetricCipher
An implementation of the Counter (CTR) mode of operation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final BlockCipher
    The underlying block cipher used.
    protected final int
    The underlying block cipher's block size.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a CTRMode instance with the given block cipher as the underlying cipher.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    decrypt(byte[] ciphertext, byte[] key)
    Decrypts the provided ciphertext using a symmetric key.
    byte[]
    encrypt(byte[] plaintext, byte[] key)
    Encrypts the provided plaintext using a symmetric key.
    protected void
    incrementBlock(byte[] input, int start, int end)
    Increments a specific sub-block of the given bytes, treating it as an unsigned integer (big endian).
    protected void
    runCTR(byte[] text, byte[] key, byte[] iv)
    Runs the CTR operation on the given text, using a secret key and initial counter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • blockCipher

      protected final BlockCipher blockCipher
      The underlying block cipher used.
    • blockSize

      protected final int blockSize
      The underlying block cipher's block size.
  • Constructor Details

    • CTRMode

      public CTRMode(BlockCipher cipher)
      Creates a CTRMode instance with the given block cipher as the underlying cipher.
      Parameters:
      cipher - The underlying block cipher for this instance.
  • Method Details

    • encrypt

      public byte[] encrypt(byte[] plaintext, byte[] key)
      Description copied from interface: SymmetricCipher
      Encrypts the provided plaintext using a symmetric key.
      Specified by:
      encrypt in interface SymmetricCipher
      Parameters:
      plaintext - The data to be encrypted.
      key - The secret key for encryption.
      Returns:
      The resulting ciphertext.
    • decrypt

      public byte[] decrypt(byte[] ciphertext, byte[] key)
      Description copied from interface: SymmetricCipher
      Decrypts the provided ciphertext using a symmetric key.
      Specified by:
      decrypt in interface SymmetricCipher
      Parameters:
      ciphertext - The encrypted data to be decrypted.
      key - The secret key for decryption.
      Returns:
      The resulting plaintext.
    • runCTR

      protected void runCTR(byte[] text, byte[] key, byte[] iv)
      Runs the CTR operation on the given text, using a secret key and initial counter. The data in text is modified in-place, becoming the encrypted or decrypted text.
      Parameters:
      text - The plaintext or ciphertext.
      key - The secret key.
      iv - The initial counter.
    • incrementBlock

      protected void incrementBlock(byte[] input, int start, int end)
      Increments a specific sub-block of the given bytes, treating it as an unsigned integer (big endian). This is essentially a ripple-carry addition of the sub-block and 1.
      Parameters:
      input - The input block containing the sub-block to increment.
      start - The index of the most significant byte of the sub-block, inclusive.
      end - The index of the least significant byte of the sub-block, exclusive.
      Throws:
      IllegalArgumentException - If the input is null, the indices are out of bounds, or the ending index is before the starting index.
      IllegalStateException - If the entire block overflows.