Esempio 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");
    }
  }