public void generateLocalKeyPair(String fullUserId) {

    String userId = Address.stripResource(fullUserId);

    OtrDebugLogger.log("generating local key pair for: " + userId);

    KeyPair keyPair;
    try {

      KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALG);
      kpg.initialize(KEY_SIZE);

      keyPair = kpg.genKeyPair();
    } catch (NoSuchAlgorithmException e) {
      OtrDebugLogger.log("no such algorithm", e);
      return;
    }

    OtrDebugLogger.log("SUCCESS! generating local key pair for: " + userId);

    // Store Private Key.
    PrivateKey privKey = keyPair.getPrivate();
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privKey.getEncoded());

    this.store.setProperty(userId + ".privateKey", pkcs8EncodedKeySpec.getEncoded());

    // Store Public Key.
    PublicKey pubKey = keyPair.getPublic();
    storeLocalPublicKey(userId, pubKey);

    store.save();
  }
  public String getRemoteFingerprint(String fullUserId) {

    // if (!Address.hasResource(fullUserId))
    //  return null;

    byte[] fingerprint = this.store.getPropertyHexBytes(fullUserId + ".fingerprint");
    if (fingerprint != null) {
      // If we have a fingerprint stashed, assume it is correct.
      return new String(Hex.encode(fingerprint, 0, fingerprint.length));
    }

    PublicKey remotePublicKey = loadRemotePublicKeyFromStore(fullUserId);
    if (remotePublicKey == null) return null;
    try {
      // Store the fingerprint, for posterity.
      String fingerprintString = new OtrCryptoEngineImpl().getFingerprint(remotePublicKey);
      this.store.setPropertyHex(fullUserId + ".fingerprint", Hex.decode(fingerprintString));

      store.save();
      return fingerprintString;
    } catch (OtrCryptoException e) {
      OtrDebugLogger.log("OtrCryptoException getting remote fingerprint", e);
      return null;
    }
  }
    private void loadOpenSSL(String password) throws IOException {

      if (!mStoreFile.exists()) return;

      if (mStoreFile.length() == 0) return;

      FileInputStream fis = null;
      OpenSSLPBEInputStream encIS = null;
      try {

        fis = new FileInputStream(mStoreFile);

        // Decrypt the bytes
        encIS = new OpenSSLPBEInputStream(fis, STORE_ALGORITHM, 1, password.toCharArray());
        mProperties.load(encIS);
      } catch (IllegalArgumentException iae) {
        // might be a unicode character in the password
        encIS =
            new OpenSSLPBEInputStream(
                fis, STORE_ALGORITHM, 1, Base64.encodeBytes(password.getBytes()).toCharArray());
        mProperties.load(encIS);

      } catch (FileNotFoundException fnfe) {
        OtrDebugLogger.log("Properties store file not found: First time?");
        mStoreFile.getParentFile().mkdirs();
      } finally {
        encIS.close();
        fis.close();
      }
    }
  public boolean importOtrKeyStoreWithPassword(String fileOtrKeyStore, String importPassword) {

    boolean overWriteExisting = true;
    boolean deleteImportedFile = true;
    try {
      return importKeyStore(fileOtrKeyStore, importPassword, overWriteExisting, deleteImportedFile);
    } catch (IOException e) {
      OtrDebugLogger.log("error importing key store", e);
      return false;
    }
  }
    public SimplePropertiesStore(File storeFile, final String password, boolean isImportFromKeySync)
        throws IOException {

      OtrDebugLogger.log("Loading store from encrypted file");
      mStoreFile = storeFile;
      mProperties.clear();

      if (password == null) throw new IOException("invalid password");

      mPassword = password;

      if (isImportFromKeySync) loadAES(mPassword);
      else loadOpenSSL(mPassword);
    }
  public static boolean handleKeyScanResult(
      int requestCode, int resultCode, Intent data, Activity activity) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    if (scanResult != null) {

      String otrKeyPassword = scanResult.getContents();

      SharedPreferences prefs =
          PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());

      String otrKeyStorePath = prefs.getString("keystoreimport", null);

      Log.d("OTR", "got password: "******" for path: " + otrKeyStorePath);

      if (otrKeyPassword != null && otrKeyStorePath != null) {

        otrKeyPassword =
            otrKeyPassword.replace("\n", "").replace("\r", ""); // remove any padding, newlines, etc

        try {
          File otrKeystoreAES = new File(otrKeyStorePath);
          if (otrKeystoreAES.exists()) {
            try {

              IOtrKeyManager keyMan =
                  ((ImApp) activity.getApplication()).getRemoteImService().getOtrKeyManager();

              return keyMan.importOtrKeyStoreWithPassword(
                  otrKeystoreAES.getCanonicalPath(), otrKeyPassword);

            } catch (Exception e) {

              OtrDebugLogger.log("error getting keyman", e);
              return false;
            }
          }
        } catch (Exception e) {
          Toast.makeText(activity, "unable to open keystore for import", Toast.LENGTH_LONG).show();
          return false;
        }
      } else {
        Log.d("OTR", "no key store path saved");
        return false;
      }
    }

    return false;
  }
  public String getLocalFingerprint(String fullUserId) {

    String userId = Address.stripResource(fullUserId);

    KeyPair keyPair = loadLocalKeyPair(userId);

    if (keyPair == null) return null;

    PublicKey pubKey = keyPair.getPublic();

    try {
      String fingerprint = cryptoEngine.getFingerprint(pubKey);

      OtrDebugLogger.log("got fingerprint for: " + userId + "=" + fingerprint);

      return fingerprint;

    } catch (OtrCryptoException e) {
      e.printStackTrace();
      return null;
    }
  }