protected String[] getPathAndNameFromDatabase(byte[] id, byte[] iv, byte[] key) throws RuntimeException { String idBase64 = Base64.getEncoder().encodeToString(id); try (Connection conn = coreHelper.getConnection(); PreparedStatement ps = conn.prepareStatement(coreHelper.getSql(conn.getMetaData(), "select-path-name")); ) { ps.setString(1, idBase64); ResultSet rs = ps.executeQuery(); if (rs.next()) { String encryptedPathBase64 = rs.getString(1); String encryptedNameBase64 = rs.getString(2); return new String[] { decryptDatabaseString(encryptedPathBase64, iv, key), decryptDatabaseString(encryptedNameBase64, iv, key) }; } } catch (Exception e) { if (isInvalidEncryptionException(e)) { // usually bad IV/key return null; } throw new RuntimeException(e); } return null; }
protected void createDatabaseRecord(EncryptedData ed) throws RuntimeException { try (Connection conn = coreHelper.getConnection(); PreparedStatement ps = conn.prepareStatement(coreHelper.getSql(conn.getMetaData(), "insert-id-path-name")); ) { ps.setString(1, Base64.getEncoder().encodeToString(ed.getId())); ps.setString(2, Base64.getEncoder().encodeToString(ed.getEncryptedPath())); ps.setString(3, Base64.getEncoder().encodeToString(ed.getEncryptedName())); if (1 != ps.executeUpdate()) { throw new RuntimeException("Cannot insert data"); } conn.commit(); } catch (SQLException e) { throw new RuntimeException(e); } }