chiperSettings
This commit is contained in:
parent
7029968c47
commit
3b78c3a929
3 changed files with 68 additions and 30 deletions
|
|
@ -136,18 +136,22 @@ int main(int argc, char *argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
|
QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
|
||||||
// if (!oldSettings.allKeys().isEmpty()) {
|
if (!oldSettings.allKeys().isEmpty()) {
|
||||||
// QSettings newSettings(QSettings::Format::CustomFormat1, QSettings::UserScope,
|
QSettings newSettingsForPath(QSettings::Format::CustomFormat1, QSettings::UserScope,
|
||||||
// ORGANIZATION_NAME, APPLICATION_NAME);
|
ORGANIZATION_NAME, APPLICATION_NAME);
|
||||||
// QString oldSettingsFileName = oldSettings.fileName();
|
QString oldSettingsFileName = oldSettings.fileName();
|
||||||
// QString newSettingsFileName = newSettings.fileName();
|
QString newSettingsFileName = newSettingsForPath.fileName();
|
||||||
|
|
||||||
|
qDebug() << "New config removed:" << QFile::remove(newSettingsFileName);
|
||||||
|
QSettings newSettings(newSettingsFileName, QSettings::Format::NativeFormat);
|
||||||
|
SecureFormat::chiperSettings(oldSettings, newSettings);
|
||||||
|
|
||||||
// qDebug() << "oldSettingsFileName:" << oldSettingsFileName;
|
// qDebug() << "oldSettingsFileName:" << oldSettingsFileName;
|
||||||
// qDebug() << "newSettingsFileName:" << newSettingsFileName;
|
// qDebug() << "newSettingsFileName:" << newSettingsFileName;
|
||||||
//// qDebug() << "New config removed:" << QFile::remove(newSettingsFileName);
|
|
||||||
// qDebug() << "Old config copied:" << QFile::copy(oldSettingsFileName, newSettingsFileName);
|
// qDebug() << "Old config copied:" << QFile::copy(oldSettingsFileName, newSettingsFileName);
|
||||||
//// qDebug() << "Old config removed:" << QFile::remove(oldSettingsFileName);
|
//// qDebug() << "Old config removed:" << QFile::remove(oldSettingsFileName);
|
||||||
// }
|
}
|
||||||
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "secureformat.h"
|
#include "secureformat.h"
|
||||||
|
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
#include <QVariantMap>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "openssl/evp.h"
|
#include "openssl/evp.h"
|
||||||
|
|
@ -15,12 +16,9 @@ int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
|
||||||
unsigned char *ciphertext)
|
unsigned char *ciphertext)
|
||||||
{
|
{
|
||||||
EVP_CIPHER_CTX *ctx;
|
EVP_CIPHER_CTX *ctx;
|
||||||
|
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
int ciphertext_len;
|
int ciphertext_len;
|
||||||
|
|
||||||
|
|
||||||
/* Create and initialise the context */
|
/* Create and initialise the context */
|
||||||
if(!(ctx = EVP_CIPHER_CTX_new()))
|
if(!(ctx = EVP_CIPHER_CTX_new()))
|
||||||
handleErrors();
|
handleErrors();
|
||||||
|
|
@ -120,24 +118,6 @@ SecureFormat::SecureFormat()
|
||||||
readSecureFile,
|
readSecureFile,
|
||||||
writeSecureFile);
|
writeSecureFile);
|
||||||
qDebug() << "SecureFormat" << m_format;
|
qDebug() << "SecureFormat" << m_format;
|
||||||
|
|
||||||
unsigned char plainText[] = "Hello world!";
|
|
||||||
qDebug("%s", plainText);
|
|
||||||
unsigned char key[] = "12345qwerty";
|
|
||||||
unsigned char iv[] = "000000000000";
|
|
||||||
unsigned char chipherText[1024];
|
|
||||||
unsigned char decryptPlainText[1024];
|
|
||||||
gcm_encrypt(plainText, std::strlen((const char *)plainText),
|
|
||||||
key,
|
|
||||||
iv, 12,
|
|
||||||
chipherText);
|
|
||||||
qDebug("%s", chipherText);
|
|
||||||
|
|
||||||
gcm_decrypt(chipherText, std::strlen((const char *)chipherText),
|
|
||||||
key,
|
|
||||||
iv, 12,
|
|
||||||
decryptPlainText);
|
|
||||||
qDebug("%s", decryptPlainText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SecureFormat::readSecureFile(QIODevice& device, QSettings::SettingsMap& map) {
|
bool SecureFormat::readSecureFile(QIODevice& device, QSettings::SettingsMap& map) {
|
||||||
|
|
@ -148,7 +128,9 @@ bool SecureFormat::readSecureFile(QIODevice& device, QSettings::SettingsMap& map
|
||||||
QTextStream inStream(&device);
|
QTextStream inStream(&device);
|
||||||
while (!inStream.atEnd()) {
|
while (!inStream.atEnd()) {
|
||||||
QString line = inStream.readLine();
|
QString line = inStream.readLine();
|
||||||
qDebug() << "SecureFormat::readSecureFile" << line;
|
qDebug() << "SecureFormat::readSecureFile: " << line;
|
||||||
|
QStringList keyValue = line.split("<=>");
|
||||||
|
map.insert(keyValue.first(), keyValue.last());
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -160,10 +142,60 @@ bool SecureFormat::writeSecureFile(QIODevice& device, const QSettings::SettingsM
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextStream outStream(&device);
|
QTextStream outStream(&device);
|
||||||
|
auto keys = map.keys();
|
||||||
|
for (auto key : keys) {
|
||||||
|
outStream << key << "<=>" << map.value(key).toString();
|
||||||
|
qDebug() << "SecureFormat::writeSecureFile: " << key << "<=>" << map.value(key).toString();
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SecureFormat::chiperSettings(const QSettings &oldSetting, QSettings &newSetting) {
|
||||||
|
QVariantMap keysValuesPairs;
|
||||||
|
QStringList keys = oldSetting.allKeys();
|
||||||
|
QStringListIterator it(keys);
|
||||||
|
while ( it.hasNext() ) {
|
||||||
|
QString currentKey = it.next();
|
||||||
|
keysValuesPairs.insert(currentKey, oldSetting.value(currentKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char gcmkey[] = "12345qwerty";
|
||||||
|
unsigned char iv[] = "000000000000";
|
||||||
|
|
||||||
|
for (const QString& key : keys) {
|
||||||
|
QString value = keysValuesPairs.value(key).toString();
|
||||||
|
|
||||||
|
int plainTextSize = value.toUtf8().size();
|
||||||
|
unsigned char* plainText = new unsigned char[plainTextSize];
|
||||||
|
std::memcpy(plainText, value.toUtf8().constData(), plainTextSize);
|
||||||
|
|
||||||
|
unsigned char chipherText[UINT16_MAX];
|
||||||
|
int chipherTextSize = gcm_encrypt(plainText, plainTextSize,
|
||||||
|
gcmkey,
|
||||||
|
iv, 12,
|
||||||
|
chipherText);
|
||||||
|
QByteArray qChipherArray = QByteArray::fromRawData((const char *)chipherText, chipherTextSize);
|
||||||
|
|
||||||
|
// unsigned char decryptPlainText[UINT16_MAX];
|
||||||
|
// gcm_decrypt((unsigned char*)qChipherArray.data(), qChipherArray.size(),
|
||||||
|
// gcmkey,
|
||||||
|
// iv, 12,
|
||||||
|
// decryptPlainText);
|
||||||
|
// QString qDecryptPlainText = QString::fromUtf8((const char *)decryptPlainText);
|
||||||
|
// qDebug() << "qDecryptPlainText:" << qDecryptPlainText;
|
||||||
|
|
||||||
|
newSetting.setValue(key, qChipherArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
// newSetting.sync();
|
||||||
|
// qDebug() << "newSetting.allKeys(): " << newSetting.allKeys();
|
||||||
|
// for (const QString& key : newSetting.allKeys()) {
|
||||||
|
// QString value = keysValuesPairs.value(key).toString();
|
||||||
|
// qDebug() << "newSetting value: " << value;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
const QSettings::Format& SecureFormat::format() const{
|
const QSettings::Format& SecureFormat::format() const{
|
||||||
return m_format;
|
return m_format;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ public:
|
||||||
static bool readSecureFile(QIODevice &device, QSettings::SettingsMap &map);
|
static bool readSecureFile(QIODevice &device, QSettings::SettingsMap &map);
|
||||||
static bool writeSecureFile(QIODevice &device, const QSettings::SettingsMap &map);
|
static bool writeSecureFile(QIODevice &device, const QSettings::SettingsMap &map);
|
||||||
|
|
||||||
|
static void chiperSettings(const QSettings &oldSetting, QSettings &newSetting);
|
||||||
|
|
||||||
const QSettings::Format& format() const;
|
const QSettings::Format& format() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue