/** Creates dialog */
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Activity activity = getActivity();

    long secretKeyId = getArguments().getLong(ARG_SECRET_KEY_ID);
    mMessenger = getArguments().getParcelable(ARG_MESSENGER);

    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setTitle(R.string.title_authentication);

    final PGPSecretKey secretKey;

    if (secretKeyId == Id.key.symmetric || secretKeyId == Id.key.none) {
      secretKey = null;
      alert.setMessage(R.string.passPhraseForSymmetricEncryption);
    } else {
      // TODO: by master key id???
      secretKey =
          PGPHelper.getMasterKey(
              ProviderHelper.getPGPSecretKeyRingByMasterKeyId(activity, secretKeyId));
      // secretKey = PGPHelper.getMasterKey(PGPMain.getSecretKeyRing(secretKeyId));

      if (secretKey == null) {
        alert.setTitle(R.string.title_keyNotFound);
        alert.setMessage(getString(R.string.keyNotFound, secretKeyId));
        alert.setPositiveButton(
            android.R.string.ok,
            new OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                dismiss();
              }
            });
        alert.setCancelable(false);
        return alert.create();
      }
      String userId = PGPHelper.getMainUserIdSafe(activity, secretKey);

      Log.d(Constants.TAG, "User id: '" + userId + "'");
      alert.setMessage(getString(R.string.passPhraseFor, userId));
    }

    LayoutInflater inflater = activity.getLayoutInflater();
    View view = inflater.inflate(R.layout.passphrase, null);
    alert.setView(view);

    mPassphraseEditText = (EditText) view.findViewById(R.id.passphrase_passphrase);

    alert.setPositiveButton(
        android.R.string.ok,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            dismiss();

            String passPhrase = mPassphraseEditText.getText().toString();
            long keyId;
            if (secretKey != null) {
              try {
                PBESecretKeyDecryptor keyDecryptor =
                    new JcePBESecretKeyDecryptorBuilder()
                        .setProvider(PGPMain.BOUNCY_CASTLE_PROVIDER_NAME)
                        .build(passPhrase.toCharArray());
                PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor);
                if (testKey == null) {
                  Toast.makeText(
                          activity, R.string.error_couldNotExtractPrivateKey, Toast.LENGTH_SHORT)
                      .show();
                  return;
                }
              } catch (PGPException e) {
                Toast.makeText(activity, R.string.wrongPassPhrase, Toast.LENGTH_SHORT).show();
                return;
              }
              keyId = secretKey.getKeyID();
            } else {
              keyId = Id.key.symmetric;
            }

            // cache the new passphrase
            Log.d(Constants.TAG, "Everything okay! Caching entered passphrase");
            PassphraseCacheService.addCachedPassphrase(activity, keyId, passPhrase);

            sendMessageToHandler(MESSAGE_OKAY);
          }
        });

    alert.setNegativeButton(
        android.R.string.cancel,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            dismiss();
          }
        });

    return alert.create();
  }