/** * Returns decrypted note text if it is encrypted * The crypto key has to be set in the object */ QString Note::getDecryptedNoteText() { QString noteText = this->noteText; QString encryptedNoteText = getEncryptedNoteText(); if (encryptedNoteText == "") { return noteText; } // decrypt the note text BotanWrapper botanWrapper; botanWrapper.setPassword(cryptoPassword); botanWrapper.setSalt(BOTAN_SALT); QString decryptedNoteText = botanWrapper.Decrypt(encryptedNoteText); // fallback to SimpleCrypt if (decryptedNoteText == "") { SimpleCrypt *crypto = new SimpleCrypt(static_cast<quint64>(cryptoKey)); decryptedNoteText = crypto->decryptToString(encryptedNoteText); } if (decryptedNoteText == "") { return noteText; } // get regular expression for the encrypted string QRegularExpression re = getEncryptedNoteTextRegularExpression(); // replace the encrypted text with the decrypted text noteText.replace(re, decryptedNoteText); return noteText; }
/** * Encrypts the note text with the note's crypto key */ QString Note::encryptNoteText() { // split the text into a string list QStringList noteTextLines = this->noteText.split( QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n")); // keep the first two lines unencrypted noteText = noteTextLines.at(0) + "\n" + noteTextLines.at(1) + "\n\n" + QString(NOTE_TEXT_ENCRYPTION_PRE_STRING) + "\n"; // remove the first two lines for encryption noteTextLines.removeFirst(); noteTextLines.removeFirst(); // remove the 3rd line too if it is empty if (noteTextLines.at(0) == "") { noteTextLines.removeFirst(); } // join the remaining lines QString text = noteTextLines.join("\n"); // empty notes will be detected as "can't be decrypted", // so we will add a space if (text.isEmpty()) { text = " "; } // encrypt the text BotanWrapper botanWrapper; botanWrapper.setPassword(cryptoPassword); botanWrapper.setSalt(BOTAN_SALT); QString encryptedText = botanWrapper.Encrypt(text); // SimpleCrypt *crypto = new SimpleCrypt(static_cast<quint64>(cryptoKey)); // QString encryptedText = crypto->encryptToString(text); // add the encrypted text to the new note text noteText += encryptedText + "\n" + QString(NOTE_TEXT_ENCRYPTION_POST_STRING); // store note store(); return noteText; }
/** * Checks if note text can be decrypted */ bool Note::canDecryptNoteText() { QString encryptedNoteText = getEncryptedNoteText(); if (encryptedNoteText == "") { return false; } // decrypt the note text BotanWrapper botanWrapper; botanWrapper.setPassword(cryptoPassword); botanWrapper.setSalt(BOTAN_SALT); QString decryptedNoteText = botanWrapper.Decrypt(encryptedNoteText); // fallback to SimpleCrypt if (decryptedNoteText == "") { SimpleCrypt *crypto = new SimpleCrypt(static_cast<quint64>(cryptoKey)); decryptedNoteText = crypto->decryptToString(encryptedNoteText); } return decryptedNoteText != ""; }