Пример #1
0
 private void testBlockCiphers() {
   for (String algorithm : new String[] {"AES", "XTEA", "FOG"}) {
     byte[] test = new byte[4096];
     BlockCipher cipher = CipherFactory.getBlockCipher(algorithm);
     cipher.setKey("abcdefghijklmnop".getBytes());
     for (int i = 0; i < 10; i++) {
       cipher.encrypt(test, 0, test.length);
     }
     assertFalse(isCompressible(test));
     for (int i = 0; i < 10; i++) {
       cipher.decrypt(test, 0, test.length);
     }
     assertEquals(new byte[test.length], test);
     assertTrue(isCompressible(test));
   }
 }
Пример #2
0
  private void testAES() {
    BlockCipher test = CipherFactory.getBlockCipher("AES");

    String r;
    byte[] data;

    // test vector from
    // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
    // ECBVarTxt128e.txt
    // COUNT = 0
    test.setKey(StringUtils.convertHexToBytes("00000000000000000000000000000000"));
    data = StringUtils.convertHexToBytes("80000000000000000000000000000000");
    test.encrypt(data, 0, data.length);
    r = StringUtils.convertBytesToHex(data);
    assertEquals("3ad78e726c1ec02b7ebfe92b23d9ec34", r);

    // COUNT = 127
    test.setKey(StringUtils.convertHexToBytes("00000000000000000000000000000000"));
    data = StringUtils.convertHexToBytes("ffffffffffffffffffffffffffffffff");
    test.encrypt(data, 0, data.length);
    r = StringUtils.convertBytesToHex(data);
    assertEquals("3f5b8cc9ea855a0afa7347d23e8d664e", r);

    // test vector
    test.setKey(StringUtils.convertHexToBytes("2b7e151628aed2a6abf7158809cf4f3c"));
    data = StringUtils.convertHexToBytes("6bc1bee22e409f96e93d7e117393172a");
    test.encrypt(data, 0, data.length);
    r = StringUtils.convertBytesToHex(data);
    assertEquals("3ad77bb40d7a3660a89ecaf32466ef97", r);

    test.setKey(StringUtils.convertHexToBytes("000102030405060708090A0B0C0D0E0F"));
    byte[] in = new byte[128];
    byte[] enc = new byte[128];
    test.encrypt(enc, 0, 128);
    test.decrypt(enc, 0, 128);
    if (!Arrays.equals(in, enc)) {
      throw new AssertionError();
    }

    for (int i = 0; i < 10; i++) {
      test.encrypt(in, 0, 128);
      test.decrypt(enc, 0, 128);
    }
  }