protected KeyPair createKeyPairInternal(boolean useAlgorithmSpec) throws NoSuchAlgorithmException,
          KeyPairNotCreateException, InvalidAlgorithmParameterException {

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(type.toString());
    SecureRandom random = new SecureRandom();
    if ((useAlgorithmSpec) && (type == AlgoCryptType.RSA)) {
      keyGen.initialize(new RSAKeyGenParameterSpec(keySize, RSAKeyGenParameterSpec.F4), random);
    }
    else {
      keyGen.initialize(keySize, random);
    }

    KeyPair keyPair = keyGen.generateKeyPair();
    if (keyPair == null) {
      throw new KeyPairNotCreateException();
    }
    return keyPair;
  }