public byte[] encrypt(byte[] data, byte[] key, byte[] ivec, int usage) throws KrbCryptoException {
   try {
     return Des3.encrypt(key, usage, ivec, data, 0, data.length);
   } catch (GeneralSecurityException e) {
     KrbCryptoException ke = new KrbCryptoException(e.getMessage());
     ke.initCause(e);
     throw ke;
   }
 }
 public byte[] decrypt(byte[] cipher, byte[] key, byte[] ivec, int usage)
     throws KrbApErrException, KrbCryptoException {
   try {
     return Des3.decrypt(key, usage, ivec, cipher, 0, cipher.length);
   } catch (GeneralSecurityException e) {
     KrbCryptoException ke = new KrbCryptoException(e.getMessage());
     ke.initCause(e);
     throw ke;
   }
 }
  /**
   * Calculates keyed checksum.
   *
   * @param data the data used to generate the checksum.
   * @param size length of the data.
   * @param key the key used to encrypt the checksum.
   * @return keyed checksum.
   */
  public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key, int usage)
      throws KrbCryptoException {

    try {
      return Aes128.calculateChecksum(key, usage, data, 0, size);
    } catch (GeneralSecurityException e) {
      KrbCryptoException ke = new KrbCryptoException(e.getMessage());
      ke.initCause(e);
      throw ke;
    }
  }
  /**
   * Verifies keyed checksum.
   *
   * @param data the data.
   * @param size the length of data.
   * @param key the key used to encrypt the checksum.
   * @param checksum
   * @return true if verification is successful.
   */
  public boolean verifyKeyedChecksum(byte[] data, int size, byte[] key, byte[] checksum, int usage)
      throws KrbCryptoException {

    try {
      byte[] newCksum = Aes128.calculateChecksum(key, usage, data, 0, size);
      return isChecksumEqual(checksum, newCksum);
    } catch (GeneralSecurityException e) {
      KrbCryptoException ke = new KrbCryptoException(e.getMessage());
      ke.initCause(e);
      throw ke;
    }
  }
  /**
   * Adds a service key to key table. If the specified key table does not exist, the program will
   * automatically generate a new key table.
   */
  void addEntry() {
    PrincipalName pname = null;
    try {
      pname = new PrincipalName(principal);
      if (pname.getRealm() == null) {
        pname.setRealm(Config.getInstance().getDefaultRealm());
      }
    } catch (KrbException e) {
      System.err.println("Failed to add " + principal + " to keytab.");
      e.printStackTrace();
      System.exit(-1);
    }
    if (password == null) {
      try {
        BufferedReader cis = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Password for " + pname.toString() + ":");
        System.out.flush();
        password = new StringBuffer().append(cis.readLine());
      } catch (IOException e) {
        System.err.println("Failed to read the password.");
        e.printStackTrace();
        System.exit(-1);
      }
    }
    try {
      // admin.addEntry(pname, password);
      table.addEntry(pname, password);
      // admin.save();
      table.save();
      System.out.println("Done!");
      System.out.println("Service key for " + principal + " is saved in " + table.tabName());

    } catch (KrbCryptoException e) {
      System.err.println("Failed to add " + principal + " to keytab.");
      e.printStackTrace();
      System.exit(-1);
    } catch (IOException e) {
      System.err.println("Failed to save new entry.");
      e.printStackTrace();
      System.exit(-1);
    }
  }