try read and write chipher Settings
This commit is contained in:
parent
06682c333f
commit
9a180b098f
2 changed files with 70 additions and 50 deletions
|
|
@ -144,26 +144,30 @@ int main(int argc, char *argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
|
{
|
||||||
// if (!oldSettings.allKeys().isEmpty()) {
|
Settings settingsTemp;
|
||||||
// QSettings newSettings(QSettings::Format::CustomFormat1, QSettings::SystemScope,
|
}
|
||||||
// ORGANIZATION_NAME, APPLICATION_NAME);
|
|
||||||
// QString oldSettingsFileName = oldSettings.fileName();
|
|
||||||
// QString newSettingsFileName = newSettings.fileName();
|
|
||||||
// qDebug() << "oldSettingsFileName:" << oldSettingsFileName;
|
|
||||||
// qDebug() << "newSettingsFileName:" << newSettingsFileName;
|
|
||||||
|
|
||||||
// qDebug() << "New config removed:" << QFile::remove(newSettingsFileName);
|
QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
|
||||||
// SecureFormat::chiperSettings(oldSettings, newSettings);
|
QSettings newSettings(QSettings::Format::CustomFormat1, QSettings::UserScope,
|
||||||
//// qDebug() << "Old config copied:" << QFile::copy(oldSettingsFileName, newSettingsFileName);
|
ORGANIZATION_NAME, APPLICATION_NAME);
|
||||||
////// qDebug() << "Old config removed:" << QFile::remove(oldSettingsFileName);
|
|
||||||
// }
|
|
||||||
|
|
||||||
MobileUtils::writeToKeychain("testKey", "12345");
|
// QString newSettingsFileName = newSettings.fileName();
|
||||||
qDebug() << "MobileUtils::readFromKeychain(\"testKey\"):" << MobileUtils::readFromKeychain("testKey");
|
// QFile::remove(newSettingsFileName);
|
||||||
|
|
||||||
|
if (!oldSettings.allKeys().isEmpty() && newSettings.allKeys().isEmpty()) {
|
||||||
|
QString oldSettingsFileName = oldSettings.fileName();
|
||||||
|
QString newSettingsFileName = newSettings.fileName();
|
||||||
|
qDebug() << "oldSettingsFileName:" << oldSettingsFileName << QFile::exists(oldSettingsFileName) << oldSettings.isWritable();
|
||||||
|
qDebug() << "newSettingsFileName:" << newSettingsFileName << QFile::exists(newSettingsFileName) << newSettings.isWritable();
|
||||||
|
|
||||||
|
SecureFormat::chiperSettings(oldSettings, newSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MobileUtils::writeToKeychain("testKey", "12345");
|
||||||
|
// qDebug() << "MobileUtils::readFromKeychain(\"testKey\"):" << MobileUtils::readFromKeychain("testKey");
|
||||||
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
|
|
||||||
if (settings.isSaveLogs()) {
|
if (settings.isSaveLogs()) {
|
||||||
if (!Debug::init()) {
|
if (!Debug::init()) {
|
||||||
qWarning() << "Initialization of debug subsystem failed";
|
qWarning() << "Initialization of debug subsystem failed";
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,15 @@ void handleErrors() {
|
||||||
qDebug() << "handleErrors";
|
qDebug() << "handleErrors";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int generate_key_and_iv(unsigned char *iv, unsigned char *key) {
|
||||||
|
// unsigned char key[32];
|
||||||
|
// unsigned char iv[16];
|
||||||
|
// EVP_BytesToKey(EVP_aes_256_gcm(), EVP_md5(),
|
||||||
|
// NULL,
|
||||||
|
// key_file_buf, key_size, 1, // const unsigned char *data, int datal, int count,
|
||||||
|
// key, iv);
|
||||||
|
}
|
||||||
|
|
||||||
int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
|
int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
|
||||||
unsigned char *key,
|
unsigned char *key,
|
||||||
unsigned char *iv, int iv_len,
|
unsigned char *iv, int iv_len,
|
||||||
|
|
@ -112,9 +121,35 @@ int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned char gcmkey[] = "12345qwerty";
|
||||||
|
unsigned char iv[] = "000000000000";
|
||||||
|
|
||||||
|
QByteArray encryptText(const QString& value) {
|
||||||
|
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);
|
||||||
|
delete[] plainText;
|
||||||
|
return QByteArray::fromRawData((const char *)chipherText, chipherTextSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString decryptText(const QByteArray& qEncryptArray) {
|
||||||
|
unsigned char decryptPlainText[UINT16_MAX];
|
||||||
|
gcm_decrypt((unsigned char*)qEncryptArray.data(), qEncryptArray.size(),
|
||||||
|
gcmkey,
|
||||||
|
iv, 12,
|
||||||
|
decryptPlainText);
|
||||||
|
return QString::fromUtf8((const char *)decryptPlainText);
|
||||||
|
}
|
||||||
|
|
||||||
SecureFormat::SecureFormat()
|
SecureFormat::SecureFormat()
|
||||||
{
|
{
|
||||||
m_format = QSettings::registerFormat("plist",
|
m_format = QSettings::registerFormat("sconf",
|
||||||
readSecureFile,
|
readSecureFile,
|
||||||
writeSecureFile);
|
writeSecureFile);
|
||||||
qDebug() << "SecureFormat" << m_format;
|
qDebug() << "SecureFormat" << m_format;
|
||||||
|
|
@ -128,9 +163,13 @@ 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;
|
|
||||||
QStringList keyValue = line.split("<=>");
|
QStringList keyValue = line.split("<=>");
|
||||||
map.insert(keyValue.first(), keyValue.last());
|
QString key = keyValue.first();
|
||||||
|
QString value = decryptText(keyValue.last().toUtf8());
|
||||||
|
map.insert(key, value);
|
||||||
|
|
||||||
|
qDebug() << "SecureFormat::readSecureFile: " << key << "<=>" << value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -144,8 +183,11 @@ bool SecureFormat::writeSecureFile(QIODevice& device, const QSettings::SettingsM
|
||||||
QTextStream outStream(&device);
|
QTextStream outStream(&device);
|
||||||
auto keys = map.keys();
|
auto keys = map.keys();
|
||||||
for (auto key : keys) {
|
for (auto key : keys) {
|
||||||
outStream << key << "<=>" << map.value(key).toString();
|
QString value = map.value(key).toString();
|
||||||
qDebug() << "SecureFormat::writeSecureFile: " << key << "<=>" << map.value(key).toString();
|
QByteArray qEncryptArray = encryptText(value);
|
||||||
|
outStream << key << "<=>" << qEncryptArray;
|
||||||
|
|
||||||
|
qDebug() << "SecureFormat::writeSecureFile: " << key << "<=>" << qEncryptArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -160,40 +202,14 @@ void SecureFormat::chiperSettings(const QSettings &oldSetting, QSettings &newSet
|
||||||
keysValuesPairs.insert(currentKey, oldSetting.value(currentKey));
|
keysValuesPairs.insert(currentKey, oldSetting.value(currentKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char gcmkey[] = "12345qwerty";
|
|
||||||
unsigned char iv[] = "000000000000";
|
|
||||||
|
|
||||||
for (const QString& key : keys) {
|
for (const QString& key : keys) {
|
||||||
QString value = keysValuesPairs.value(key).toString();
|
QString value = keysValuesPairs.value(key).toString();
|
||||||
|
QByteArray qEncryptArray = encryptText(value);
|
||||||
|
|
||||||
int plainTextSize = value.toUtf8().size();
|
newSetting.setValue(key, qEncryptArray);
|
||||||
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();
|
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{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue