chiperSettings

This commit is contained in:
eugenyorbitsoftcom 2022-07-29 14:58:22 +06:00
parent 7029968c47
commit 3b78c3a929
3 changed files with 68 additions and 30 deletions

View file

@ -136,18 +136,22 @@ int main(int argc, char *argv[])
return 0;
}
// QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
// if (!oldSettings.allKeys().isEmpty()) {
// QSettings newSettings(QSettings::Format::CustomFormat1, QSettings::UserScope,
// ORGANIZATION_NAME, APPLICATION_NAME);
// QString oldSettingsFileName = oldSettings.fileName();
// QString newSettingsFileName = newSettings.fileName();
QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
if (!oldSettings.allKeys().isEmpty()) {
QSettings newSettingsForPath(QSettings::Format::CustomFormat1, QSettings::UserScope,
ORGANIZATION_NAME, APPLICATION_NAME);
QString oldSettingsFileName = oldSettings.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() << "newSettingsFileName:" << newSettingsFileName;
//// qDebug() << "New config removed:" << QFile::remove(newSettingsFileName);
// qDebug() << "Old config copied:" << QFile::copy(oldSettingsFileName, newSettingsFileName);
//// qDebug() << "Old config removed:" << QFile::remove(oldSettingsFileName);
// }
}
Settings settings;

View file

@ -1,6 +1,7 @@
#include "secureformat.h"
#include <QTextStream>
#include <QVariantMap>
#include <QDebug>
#include "openssl/evp.h"
@ -15,12 +16,9 @@ int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
@ -120,24 +118,6 @@ SecureFormat::SecureFormat()
readSecureFile,
writeSecureFile);
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) {
@ -148,7 +128,9 @@ bool SecureFormat::readSecureFile(QIODevice& device, QSettings::SettingsMap& map
QTextStream inStream(&device);
while (!inStream.atEnd()) {
QString line = inStream.readLine();
qDebug() << "SecureFormat::readSecureFile" << line;
qDebug() << "SecureFormat::readSecureFile: " << line;
QStringList keyValue = line.split("<=>");
map.insert(keyValue.first(), keyValue.last());
}
return true;
@ -160,10 +142,60 @@ bool SecureFormat::writeSecureFile(QIODevice& device, const QSettings::SettingsM
}
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;
}
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{
return m_format;
}

View file

@ -12,6 +12,8 @@ public:
static bool readSecureFile(QIODevice &device, 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;
private: