示例#1
0
 /**
  * self-test routine for AES cipher
  *
  * @param args command line arguments
  */
 public String _cryptAll(String data, int mode) {
   AesAlgorithm aesAlgorithm = this;
   if (data.length() / 16 > ((int) data.length() / 16)) {
     int rest = data.length() - ((int) data.length() / 16) * 16;
     for (int i = 0; i < rest; i++) data += " ";
   }
   int nParts = (int) data.length() / 16;
   byte[] res = new byte[data.length()];
   String partStr = "";
   byte[] partByte = new byte[16];
   for (int p = 0; p < nParts; p++) {
     partStr = data.substring(p * 16, p * 16 + 16);
     partByte = static_stringToByteArray(partStr);
     if (mode == 1) partByte = aesAlgorithm.encrypt(partByte);
     if (mode == 2) partByte = aesAlgorithm.decrypt(partByte);
     for (int b = 0; b < 16; b++) res[p * 16 + b] = partByte[b];
   }
   return static_byteArrayToString(res);
 }
示例#2
0
  /**
   * self-test routine for AES cipher
   *
   * @param hkey key to test in hex
   * @param hplain plaintext to test in hex
   * @param hcipher ciphertext to test in hex
   * @param lev trace level to use
   */
  public static void self_test(String hkey, String hplain, String hcipher, int lev) {

    // AES test triple (128-bit key test value from FIPS-197)
    byte[] key = Util.hex2byte(hkey);
    byte[] plain = Util.hex2byte(hplain);
    byte[] cipher = Util.hex2byte(hcipher);
    byte[] result;

    AesAlgorithm testAES = new AesAlgorithm(); // create new AES instance to test triple
    testAES.traceLevel = lev; // select level of trace info
    testAES.setKey(key); // set key and display trace info
    System.out.print(testAES.traceInfo);

    result = testAES.encrypt(plain); // test encryption
    System.out.print(testAES.traceInfo);
    if (Arrays.equals(result, cipher)) System.out.print("Test OKn");
    else System.out.print("Test Failed. Result was " + Util.toHEX(result) + "n");

    result = testAES.decrypt(cipher); // test decryption
    System.out.print(testAES.traceInfo);
    if (Arrays.equals(result, plain)) System.out.print("Test OKn");
    else System.out.print("Test Failed. Result was " + Util.toHEX(result) + "n");
    System.out.println();
  }