コード例 #1
0
ファイル: JSONParser.java プロジェクト: pedja1/sql-master
  private void decryptCredentials(DatabaseEntry entry, JSONObject jDatabase) {
    String databaseUsername, databasePassword = null;
    // if no encryption key or both username and password are empty, skip encryption
    if (!SettingsManager.getSyncSetting(SettingsManager.SyncKey.sync_credentials)
        || TextUtils.isEmpty(SettingsManager.getEc())
        || (TextUtils.isEmpty(databaseUsername = jDatabase.optString("database_username"))
            && TextUtils.isEmpty(databasePassword = jDatabase.optString("database_password"))))
      return;
    try {
      AesCbcWithIntegrity.SecretKeys keys = AesCbcWithIntegrity.keys(SettingsManager.getEc());

      if (!TextUtils.isEmpty(databaseUsername)) {
        AesCbcWithIntegrity.CipherTextIvMac cipherTextIvMac =
            new AesCbcWithIntegrity.CipherTextIvMac(databaseUsername);
        entry.databaseUsername = AesCbcWithIntegrity.decryptString(cipherTextIvMac, keys);
      }
      if (!TextUtils.isEmpty(databasePassword)) {
        AesCbcWithIntegrity.CipherTextIvMac cipherTextIvMac =
            new AesCbcWithIntegrity.CipherTextIvMac(databasePassword);
        entry.databasePassword = AesCbcWithIntegrity.decryptString(cipherTextIvMac, keys);
      }
    } catch (GeneralSecurityException | UnsupportedEncodingException e) {
      if (SettingsManager.DEBUG()) e.printStackTrace();
      // fail silently on any kind of error
      // user will see warning on database entry when restored without username/password
    }
  }
コード例 #2
0
  protected byte[] getHash(RSAPublicKey publicKey) throws EbicsException {
    String modulus;
    String exponent;
    String hash;
    byte[] digest;

    exponent = Hex.encodeHexString(publicKey.getPublicExponent().toByteArray());
    modulus = Hex.encodeHexString(removeFirstByte(publicKey.getModulus().toByteArray()));
    hash = exponent + " " + modulus;

    if (hash.charAt(0) == '0') {
      hash = hash.substring(1);
    }

    try {
      digest = MessageDigest.getInstance("SHA-256", "BC").digest(hash.getBytes("US-ASCII"));
    } catch (GeneralSecurityException | UnsupportedEncodingException e) {
      throw new EbicsException(e.getMessage());
    }

    return format(new String(Hex.encodeHex(digest, false))).getBytes();
  }