Skip to content

Commit

Permalink
Fixed restoring encrypted notes
Browse files Browse the repository at this point in the history
On first restore, when no password was present into app's preferences yet, the encrypted notes were stored into database without content due to password check failure. On the other side restoring when password was already set broke notes' content due to multiple encryption.
  • Loading branch information
federicoiosue committed May 24, 2021
1 parent 0c593df commit 15f7ff0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,7 @@ private void importNotes() {
File backupDir = StorageHelper.getOrCreateBackupDir(backups.get(position));
long size = StorageHelper.getSize(backupDir) / 1024;
String sizeString = size > 1024 ? size / 1024 + "Mb" : size + "Kb";

// Check preference presence
String prefName = StorageHelper.getSharedPreferencesFile(getActivity()).getName();
boolean hasPreferences = (new File(backupDir, prefName)).exists();

String message = String.format("%s (%s %s)", backups.get(position), sizeString,
hasPreferences ? getString(R.string.settings_included) : "");
String message = String.format("%s (%s)", backups.get(position), sizeString);

new MaterialAlertDialogBuilder(getActivity())
.setTitle(R.string.confirm_restoring_backup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.pixplicity.easyprefs.library.Prefs;
import it.feio.android.omninotes.MainActivity;
import it.feio.android.omninotes.OmniNotes;
import it.feio.android.omninotes.R;
Expand All @@ -48,7 +48,6 @@ public class DataBackupIntentService extends IntentService implements OnAttachin
public static final String ACTION_DATA_IMPORT_LEGACY = "action_data_import_legacy";
public static final String ACTION_DATA_DELETE = "action_data_delete";

private SharedPreferences prefs;
private NotificationsHelper mNotificationsHelper;

// {
Expand Down Expand Up @@ -138,7 +137,7 @@ private synchronized void importData(Intent intent) {
getString(R.string.click_to_refresh_application), backupDir);

// Performs auto-backup filling after backup restore
// if (prefs.getBoolean(Constants.PREF_ENABLE_AUTOBACKUP, false)) {
// if (Prefs.getBoolean(Constants.PREF_ENABLE_AUTOBACKUP, false)) {
// File autoBackupDir = StorageHelper.getBackupDir(Constants.AUTO_BACKUP_DIR);
// BackupHelper.exportNotes(autoBackupDir);
// BackupHelper.exportAttachments(autoBackupDir);
Expand Down Expand Up @@ -185,9 +184,9 @@ private void createNotification(Intent intent, Context mContext, String title, S
NotificationsHelper notificationsHelper = new NotificationsHelper(mContext);
notificationsHelper.createStandardNotification(NotificationChannelNames.BACKUPS,
R.drawable.ic_content_save_white_24dp, title, notifyIntent)
.setMessage(message).setRingtone(prefs.getString("settings_notification_ringtone", null))
.setMessage(message).setRingtone(Prefs.getString("settings_notification_ringtone", null))
.setLedActive();
if (prefs.getBoolean("settings_notification_vibration", true)) {
if (Prefs.getBoolean("settings_notification_vibration", true)) {
notificationsHelper.setVibration();
}
notificationsHelper.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@


import static it.feio.android.omninotes.utils.ConstantsBase.DATABASE_NAME;
import static it.feio.android.omninotes.utils.ConstantsBase.PREF_PASSWORD;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.pixplicity.easyprefs.library.Prefs;
import it.feio.android.omninotes.OmniNotes;
import it.feio.android.omninotes.R;
import it.feio.android.omninotes.async.DataBackupIntentService;
Expand All @@ -32,6 +34,7 @@
import it.feio.android.omninotes.helpers.notifications.NotificationsHelper;
import it.feio.android.omninotes.models.Attachment;
import it.feio.android.omninotes.models.Note;
import it.feio.android.omninotes.utils.Security;
import it.feio.android.omninotes.utils.StorageHelper;
import it.feio.android.omninotes.utils.TextHelper;
import java.io.File;
Expand All @@ -45,7 +48,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;
Expand All @@ -64,6 +66,9 @@ public static void exportNotes(File backupDir) {
}

public static void exportNote(File backupDir, Note note) {
if (Boolean.TRUE.equals(note.isLocked())) {
note.setContent(Security.encrypt(note.getContent(), Prefs.getString(PREF_PASSWORD, "")));
}
File noteFile = getBackupNoteFile(backupDir, note);
try {
FileUtils.write(noteFile, note.toJSON());
Expand Down Expand Up @@ -165,6 +170,9 @@ public static Note importNote(File file) {
if (note.getCategory() != null) {
DbHelper.getInstance().updateCategory(note.getCategory());
}
if (Boolean.TRUE.equals(note.isLocked())) {
note.setContent(Security.decrypt(note.getContent(), Prefs.getString(PREF_PASSWORD, "")));
}
DbHelper.getInstance().updateNote(note, false);
return note;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Security private constructor(){
Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT)
} catch (e: Exception) {
LogDelegate.e("Something is gone wrong encrypting", e)
""
value
}
}

Expand Down

0 comments on commit 15f7ff0

Please sign in to comment.