diff --git a/android-app/app/src/main/java/org/pupes/tabletpccontrol/helper_funcs.kt b/android-app/app/src/main/java/org/pupes/tabletpccontrol/helper_funcs.kt new file mode 100644 index 0000000..15ea24e --- /dev/null +++ b/android-app/app/src/main/java/org/pupes/tabletpccontrol/helper_funcs.kt @@ -0,0 +1,28 @@ +package org.pupes.tabletpccontrol +import javax.crypto.Cipher +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec +import android.util.Base64 + +fun decrypt(base64CipherText: String, key: String, iv: String): String { + val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") + + val secretKey = SecretKeySpec(key.toByteArray(Charsets.UTF_8), "AES") + val ivParams = IvParameterSpec(iv.toByteArray(Charsets.UTF_8)) + + cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParams) + + val decryptedBytes = cipher.doFinal(Base64.decode(base64CipherText, Base64.DEFAULT)) + + return String(decryptedBytes, Charsets.UTF_8) +} + +// Example usage +fun main() { + val base64CipherText = "YOUR_BASE64_ENCRYPTED_TEXT_HERE" + val key = "key123456789101" // Must match the C++ key length + val iv = "iv1234567891111" // Must match the C++ IV length + + val decryptedText = decrypt(base64CipherText, key, iv) + println("Decrypted Text: $decryptedText") +} diff --git a/cpp-server/src/crypt.cpp b/cpp-server/src/crypt.cpp index e67a462..bf8d041 100644 --- a/cpp-server/src/crypt.cpp +++ b/cpp-server/src/crypt.cpp @@ -1,19 +1,24 @@ +#include "helper_funcs.h" #include #include #include #include #include #include +#include #include -int main() { +using nlohmann::json; + +int encrypt() { // Key and IV (Initialization Vector) for AES CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = { 'k', 'e', 'y', '1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '0', '1', '1'}; - CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE] = {'i', 'v', '1', '2', '3', '4', - '5', '6', '7', '8', '9', '1', - '0', '1', '1', '1'}; + CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE]; + for (int i = 0; i < 16; i++) { + iv[i] = static_cast(get_random_num(33, 126)); + } // Message to be encrypted std::string plainText = "Hello, AES!"; @@ -36,3 +41,40 @@ int main() { return 0; } + +int decrypt() { + // Key and IV (Initialization Vector) for AES + CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = { + 'k', 'e', 'y', '1', '2', '3', '4', '5', + '6', '7', '8', '9', '1', '0', '1', '1'}; + CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE] = {'i', 'v', '1', '2', '3', '4', + '5', '6', '7', '8', '9', '1', + '0', '1', '1', '1'}; + + // Base64 encoded encrypted message (replace with actual encrypted message) + std::string base64EncodedCipherText = "PgKnjjHY2xz/LRWgg1ob1Q=="; + std::string cipherText; + + // Decode the Base64 encoded message + CryptoPP::StringSource( + base64EncodedCipherText, true, + new CryptoPP::Base64Decoder(new CryptoPP::StringSink(cipherText))); + + // Decrypt using AES in CBC mode + std::string decryptedText; + CryptoPP::CBC_Mode::Decryption decryptor(key, sizeof(key), iv); + + CryptoPP::StringSource( + cipherText, true, + new CryptoPP::StreamTransformationFilter( + decryptor, new CryptoPP::StringSink(decryptedText))); + + // Display the decrypted message + std::cout << "Decrypted Text: " << decryptedText << std::endl; + + return 0; +} +int main() { + decrypt(); + return 0; +} \ No newline at end of file diff --git a/cpp-server/src/helper_funcs.cpp b/cpp-server/src/helper_funcs.cpp index 7b09f61..5946cdc 100644 --- a/cpp-server/src/helper_funcs.cpp +++ b/cpp-server/src/helper_funcs.cpp @@ -1,6 +1,8 @@ #include "main.h" #include +#include #include + void safe_exit(int code) { try { std::clog << "Exiting\n"; @@ -9,4 +11,13 @@ void safe_exit(int code) { } catch (...) { } exit(code); +} + +int get_random_num(int min, int max) { + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist( + min, max); // set range + + return dist(rng); } \ No newline at end of file diff --git a/cpp-server/src/helper_funcs.h b/cpp-server/src/helper_funcs.h index aa0e692..5f2880b 100644 --- a/cpp-server/src/helper_funcs.h +++ b/cpp-server/src/helper_funcs.h @@ -1 +1,2 @@ -void safe_exit(int code); \ No newline at end of file +void safe_exit(int code); +int get_random_num(int min, int max); \ No newline at end of file