1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-23 04:57:44 +00:00

Add BlowfishSession class for blowfish encryption/decryption

This commit is contained in:
redstrate 2021-11-09 14:12:41 -05:00
parent d812d97be6
commit 5dd5482784
4 changed files with 81 additions and 3 deletions

View file

@ -16,9 +16,10 @@ add_executable(xivlauncher
src/sapphirelauncher.cpp src/sapphirelauncher.cpp
src/squareboot.cpp src/squareboot.cpp
src/squarelauncher.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 # disgusting, thanks qtkeychain
target_include_directories(xivlauncher PRIVATE target_include_directories(xivlauncher PRIVATE

View file

@ -9,4 +9,12 @@ FetchContent_Declare(
set(BUILD_WITH_QT6 ON CACHE BOOL "" FORCE) set(BUILD_WITH_QT6 ON CACHE BOOL "" FORCE)
set(QTKEYCHAIN_STATIC ON CACHE BOOL "" FORCE) set(QTKEYCHAIN_STATIC ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(qtkeychain) 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)

51
src/blowfish.cpp Normal file
View file

@ -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;
}

18
src/blowfish.h Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <QString>
#include <mbedtls/blowfish.h>
class BlowfishSession {
public:
BlowfishSession();
void setKey(QString key);
QByteArray encrypt(QString string);
QString decrypt(QByteArray data);
private:
mbedtls_blowfish_context ctx;
};