From b751b070e5d7d55ceaea7439e041468bab553b92 Mon Sep 17 00:00:00 2001 From: Silarn Date: Thu, 31 Jan 2019 13:25:13 -0600 Subject: Implement using the DPAPI to encrypt credentials --- src/settings.cpp | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'src/settings.cpp') diff --git a/src/settings.cpp b/src/settings.cpp index fdc767bf..1d5da376 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -57,6 +57,7 @@ along with Mod Organizer. If not, see . #include // for qDebug, qWarning #include // For ShellExecuteW, HINSTANCE, etc +#include // For storage #include // for sort #include @@ -175,26 +176,49 @@ void Settings::registerPlugin(IPlugin *plugin) } } -QString Settings::obfuscate(const QString &password) +QString Settings::obfuscate(const QString &info) { - QByteArray temp = password.toUtf8(); + QString result; + DATA_BLOB input; + DATA_BLOB output; + BYTE *pbInput = (unsigned char *)malloc(info.size() + 1); + memcpy(pbInput, info.constData(), info.size() + 1); + DWORD cbInput = info.size() + 1; + input.pbData = pbInput; + input.cbData = cbInput; - QByteArray buffer; - for (int i = 0; i < temp.length(); ++i) { - buffer.append(temp.at(i) ^ Key2[i % 20]); + if (CryptProtectData(&input, NULL, NULL, NULL, NULL, 0, &output)) { + QByteArray buffer = QByteArray::fromRawData((const char *)output.pbData, output.cbData); + result = buffer.toBase64(); + } else { + qCritical() << "Failed to encrypt the data!"; } - return buffer.toBase64(); + LocalFree(input.pbData); + LocalFree(output.pbData); + return result; } -QString Settings::deObfuscate(const QString &password) +QString Settings::deObfuscate(const QString &info) { - QByteArray temp(QByteArray::fromBase64(password.toUtf8())); + QString result; + QByteArray realInfo = QByteArray::fromBase64(info.toUtf8()); + DATA_BLOB input; + DATA_BLOB output; + BYTE *pbInput = (unsigned char *)malloc(realInfo.size() + 1); + memcpy(pbInput, realInfo.constData(), realInfo.size() + 1); + DWORD cbInput = realInfo.size() + 1; + input.pbData = pbInput; + input.cbData = cbInput; - QByteArray buffer; - for (int i = 0; i < temp.length(); ++i) { - buffer.append(temp.at(i) ^ Key2[i % 20]); + if (CryptUnprotectData(&input, NULL, NULL, NULL, NULL, 0, &output)) { + QByteArray buffer = QByteArray::fromRawData((const char *)output.pbData, output.cbData); + result = buffer; + } else { + qCritical() << "Failed to decrypt data!"; } - return QString::fromUtf8(buffer.constData()); + LocalFree(input.pbData); + LocalFree(output.pbData); + return result; } QColor Settings::getIdealTextColor(const QColor& rBackgroundColor) -- cgit v1.3.1