예제 #1
0
  private void testOldCTS(
      int id, BlockCipher cipher, CipherParameters params, byte[] input, byte[] output)
      throws Exception {
    byte[] out = new byte[input.length];
    BufferedBlockCipher engine = new OldCTSBlockCipher(cipher);

    engine.init(true, params);

    int len = engine.processBytes(input, 0, input.length, out, 0);

    engine.doFinal(out, len);

    if (!areEqual(output, out)) {
      fail(
          "failed encryption expected "
              + new String(Hex.encode(output))
              + " got "
              + new String(Hex.encode(out)));
    }

    engine.init(false, params);

    len = engine.processBytes(output, 0, output.length, out, 0);

    engine.doFinal(out, len);

    if (!areEqual(input, out)) {
      fail(
          "failed decryption expected "
              + new String(Hex.encode(input))
              + " got "
              + new String(Hex.encode(out)));
    }
  }
예제 #2
0
  private void testExceptions() throws InvalidCipherTextException {
    BufferedBlockCipher engine = new CTSBlockCipher(new DESEngine());
    CipherParameters params = new KeyParameter(new byte[engine.getBlockSize()]);
    engine.init(true, params);

    byte[] out = new byte[engine.getOutputSize(engine.getBlockSize())];

    engine.processBytes(new byte[engine.getBlockSize() - 1], 0, engine.getBlockSize() - 1, out, 0);
    try {
      engine.doFinal(out, 0);
      fail("Expected CTS encrypt error on < 1 block input");
    } catch (DataLengthException e) {
      // Expected
    }

    engine.init(true, params);
    engine.processBytes(new byte[engine.getBlockSize()], 0, engine.getBlockSize(), out, 0);
    try {
      engine.doFinal(out, 0);
    } catch (DataLengthException e) {
      fail("Unexpected CTS encrypt error on == 1 block input");
    }

    engine.init(false, params);
    engine.processBytes(new byte[engine.getBlockSize() - 1], 0, engine.getBlockSize() - 1, out, 0);
    try {
      engine.doFinal(out, 0);
      fail("Expected CTS decrypt error on < 1 block input");
    } catch (DataLengthException e) {
      // Expected
    }

    engine.init(false, params);
    engine.processBytes(new byte[engine.getBlockSize()], 0, engine.getBlockSize(), out, 0);
    try {
      engine.doFinal(out, 0);
    } catch (DataLengthException e) {
      fail("Unexpected CTS decrypt error on == 1 block input");
    }

    try {
      new CTSBlockCipher(new SICBlockCipher(new AESEngine()));
      fail("Expected CTS construction error - only ECB/CBC supported.");
    } catch (IllegalArgumentException e) {
      // Expected
    }
  }