Exemplo n.º 1
0
  public static final void main(String[] args) throws Exception {

    // Generate a PBE key
    SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
    SecretKey skey = skFac.generateSecret(new PBEKeySpec("test123".toCharArray()));

    // Initialize the PBE cipher
    Cipher cipher = Cipher.getInstance(ALGO);
    cipher.init(Cipher.ENCRYPT_MODE, skey);

    // Permit access to the Cipher.spi field (a CipherSpi object)
    Field spi = Cipher.class.getDeclaredField("spi");
    spi.setAccessible(true);
    Object value = spi.get(cipher);

    // Permit access to the CipherSpi.engineGetKeySize method
    Method engineGetKeySize =
        PKCS12PBECipherCore$PBEWithSHA1AndDESede.class.getDeclaredMethod(
            "engineGetKeySize", Key.class);
    engineGetKeySize.setAccessible(true);

    // Check the key size
    int keySize = (int) engineGetKeySize.invoke(value, skey);
    if (keySize == KEYSIZE) {
      System.out.println(ALGO + ".engineGetKeySize returns " + keySize + " bits, as expected");
      System.out.println("OK");
    } else {
      throw new Exception("ERROR: " + ALGO + " key size is incorrect");
    }
  }
Exemplo n.º 2
0
  /**
   * Gets the features of a specific <tt>DiscoverInfo</tt> in the form of a read-only
   * <tt>Feature</tt> <tt>Iterator<tt/> by calling the internal method {@link
   * DiscoverInfo#getFeatures()}.
   *
   * @param discoverInfo the <tt>DiscoverInfo</tt> the features of which are to be retrieved
   * @return a read-only <tt>Feature</tt> <tt>Iterator</tt> which lists the features of the
   *     specified <tt>discoverInfo</tt>
   */
  @SuppressWarnings("unchecked")
  private static Iterator<DiscoverInfo.Feature> getDiscoverInfoFeatures(DiscoverInfo discoverInfo) {
    Method getFeaturesMethod;

    try {
      getFeaturesMethod = DiscoverInfo.class.getDeclaredMethod("getFeatures");
    } catch (NoSuchMethodException nsmex) {
      throw new UndeclaredThrowableException(nsmex);
    }
    getFeaturesMethod.setAccessible(true);
    try {
      return (Iterator<DiscoverInfo.Feature>) getFeaturesMethod.invoke(discoverInfo);
    } catch (IllegalAccessException iaex) {
      throw new UndeclaredThrowableException(iaex);
    } catch (InvocationTargetException itex) {
      throw new UndeclaredThrowableException(itex);
    }
  }