From 5dd54827847505ea26ce045e4345079ba641eb23 Mon Sep 17 00:00:00 2001 From: redstrate Date: Tue, 9 Nov 2021 14:12:41 -0500 Subject: [PATCH] Add BlowfishSession class for blowfish encryption/decryption --- CMakeLists.txt | 5 ++-- external/CMakeLists.txt | 10 +++++++- src/blowfish.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ src/blowfish.h | 18 +++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/blowfish.cpp create mode 100644 src/blowfish.h diff --git a/CMakeLists.txt b/CMakeLists.txt index da088c4..12c082c 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,9 +16,10 @@ add_executable(xivlauncher src/sapphirelauncher.cpp src/squareboot.cpp src/squarelauncher.cpp - src/settingswindow.cpp) + src/settingswindow.cpp + src/blowfish.cpp) -target_link_libraries(xivlauncher Qt6::Core Qt6::Widgets Qt6::Network qt6keychain) +target_link_libraries(xivlauncher Qt6::Core Qt6::Widgets Qt6::Network qt6keychain mbedtls) # disgusting, thanks qtkeychain target_include_directories(xivlauncher PRIVATE diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index c84c545..f9f8ef5 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -9,4 +9,12 @@ FetchContent_Declare( set(BUILD_WITH_QT6 ON CACHE BOOL "" FORCE) set(QTKEYCHAIN_STATIC ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(qtkeychain) \ No newline at end of file +FetchContent_MakeAvailable(qtkeychain) + +FetchContent_Declare( + mbedtls + GIT_REPOSITORY https://github.com/ARMmbed/mbedtls.git + GIT_TAG v2.27.0 # last version with blowfish support +) + +FetchContent_MakeAvailable(mbedtls) \ No newline at end of file diff --git a/src/blowfish.cpp b/src/blowfish.cpp new file mode 100644 index 0000000..12e7003 --- /dev/null +++ b/src/blowfish.cpp @@ -0,0 +1,51 @@ +#include "blowfish.h" + +BlowfishSession::BlowfishSession() { + mbedtls_blowfish_init(&ctx); +} + +void BlowfishSession::setKey(QString key) { + std::string keyStr = key.toStdString(); + mbedtls_blowfish_setkey(&ctx, (unsigned char*)keyStr.c_str(), key.length() * 8); +} + +QByteArray BlowfishSession::encrypt(QString string) { + QByteArray finalArray; + for(int i = 0; i < string.length(); i += 8) { + unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE]; + memset(input, 0, 8); + + std::string inputStr = string.toStdString().substr(i, 8); + strcpy((char*)input, inputStr.c_str()); + + unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]; + memset(output, 0, 8); + + mbedtls_blowfish_crypt_ecb(&ctx, MBEDTLS_BLOWFISH_ENCRYPT, input, output); + + QByteArray arr((char*)output, 8); + finalArray.append(arr); + } + + return finalArray; +} + +QString BlowfishSession::decrypt(QByteArray data) { + QString finalString; + + for(int i = 0; i < data.length(); i += 8) { + unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE]; + memset(input, 0, 8); + memcpy(input, data.data() + i, 8); + + unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]; + memset(output, 0, 8); + + mbedtls_blowfish_crypt_ecb(&ctx, MBEDTLS_BLOWFISH_DECRYPT, input, output); + + QString str((char*)output); + finalString.append(str); + } + + return finalString; +} \ No newline at end of file diff --git a/src/blowfish.h b/src/blowfish.h new file mode 100644 index 0000000..9014b55 --- /dev/null +++ b/src/blowfish.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include + +class BlowfishSession { +public: + BlowfishSession(); + + void setKey(QString key); + + QByteArray encrypt(QString string); + QString decrypt(QByteArray data); + +private: + mbedtls_blowfish_context ctx; +};