late night chnges

This commit is contained in:
PoliEcho 2024-12-08 21:52:37 +01:00
parent e4c86e2322
commit 057ed9fc0c
4 changed files with 87 additions and 5 deletions

View File

@ -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")
}

View File

@ -1,19 +1,24 @@
#include "helper_funcs.h"
#include <cryptopp/aes.h> #include <cryptopp/aes.h>
#include <cryptopp/base64.h> #include <cryptopp/base64.h>
#include <cryptopp/filters.h> #include <cryptopp/filters.h>
#include <cryptopp/modes.h> #include <cryptopp/modes.h>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp>
#include <string> #include <string>
int main() { using nlohmann::json;
int encrypt() {
// Key and IV (Initialization Vector) for AES // Key and IV (Initialization Vector) for AES
CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = { CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = {
'k', 'e', 'y', '1', '2', '3', '4', '5', 'k', 'e', 'y', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '1', '0', '1', '1'}; '6', '7', '8', '9', '1', '0', '1', '1'};
CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE] = {'i', 'v', '1', '2', '3', '4', CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE];
'5', '6', '7', '8', '9', '1', for (int i = 0; i < 16; i++) {
'0', '1', '1', '1'}; iv[i] = static_cast<char>(get_random_num(33, 126));
}
// Message to be encrypted // Message to be encrypted
std::string plainText = "Hello, AES!"; std::string plainText = "Hello, AES!";
@ -36,3 +41,40 @@ int main() {
return 0; 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<CryptoPP::AES>::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;
}

View File

@ -1,6 +1,8 @@
#include "main.h" #include "main.h"
#include <iostream> #include <iostream>
#include <random>
#include <unistd.h> #include <unistd.h>
void safe_exit(int code) { void safe_exit(int code) {
try { try {
std::clog << "Exiting\n"; std::clog << "Exiting\n";
@ -9,4 +11,13 @@ void safe_exit(int code) {
} catch (...) { } catch (...) {
} }
exit(code); exit(code);
}
int get_random_num(int min, int max) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(
min, max); // set range
return dist(rng);
} }

View File

@ -1 +1,2 @@
void safe_exit(int code); void safe_exit(int code);
int get_random_num(int min, int max);