Untitled
unknown
plain_text
2 months ago
4.2 kB
4
Indexable
#include <iostream> #include <fstream> #include <openssl/aes.h> #include <openssl/rand.h> #include <cstring> // Function to apply PKCS#7 padding to the input text void applyPadding(unsigned char* input, size_t& inputLength) { // Calculate padding needed to make the length a multiple of AES block size (16 bytes) size_t paddingLength = AES_BLOCK_SIZE - (inputLength % AES_BLOCK_SIZE); for (size_t i = inputLength; i < inputLength + paddingLength; ++i) { input[i] = paddingLength; // Padding value is the number of bytes to pad } inputLength += paddingLength; // Update the input length } // AES Encryption function for multiple blocks (with padding) void aesEncrypt(const unsigned char* key, const unsigned char* input, size_t inputLength, unsigned char* output) { AES_KEY encryptKey; AES_set_encrypt_key(key, 128, &encryptKey); // Encrypt input in 16-byte blocks for (size_t i = 0; i < inputLength; i += AES_BLOCK_SIZE) { AES_encrypt(input + i, output + i, &encryptKey); } } // AES Decryption function (also handles multiple blocks) void aesDecrypt(const unsigned char* key, const unsigned char* input, size_t inputLength, unsigned char* output) { AES_KEY decryptKey; AES_set_decrypt_key(key, 128, &decryptKey); // Decrypt input in 16-byte blocks for (size_t i = 0; i < inputLength; i += AES_BLOCK_SIZE) { AES_decrypt(input + i, output + i, &decryptKey); } // Remove padding (PKCS#7) size_t paddingLength = output[inputLength - 1]; output[inputLength - paddingLength] = '\0'; // Null-terminate the string } // Function to encrypt and write log to file void writeEncryptedLog(const std::string& logMessage, const std::string& filename, const unsigned char* key) { size_t messageLength = logMessage.length(); // Allocate buffer for the input message (padded) unsigned char input[1024] = {0}; // Max length for this example is 1024 std::memcpy(input, logMessage.c_str(), messageLength); // Apply PKCS#7 padding applyPadding(input, messageLength); // Prepare buffer for encrypted data unsigned char encrypted[1024] = {0}; // Encrypt the log message aesEncrypt(key, input, messageLength, encrypted); // Open the log file in append mode std::ofstream logFile(filename, std::ios::app | std::ios::binary); if (logFile.is_open()) { // Write encrypted data to file logFile.write(reinterpret_cast<char*>(encrypted), messageLength); logFile.close(); std::cout << "Encrypted log written to file." << std::endl; } else { std::cerr << "Error opening log file." << std::endl; } } // Function to read and decrypt logs from file void readAndDecryptLogs(const std::string& filename, const unsigned char* key) { std::ifstream logFile(filename, std::ios::binary); if (logFile.is_open()) { unsigned char encrypted[1024]; unsigned char decrypted[1024]; while (logFile.read(reinterpret_cast<char*>(encrypted), 1024)) { size_t encryptedLength = logFile.gcount(); // Length of the data read // Decrypt the log message aesDecrypt(key, encrypted, encryptedLength, decrypted); // Print decrypted log (as string) std::cout << "Decrypted log: "; std::cout << decrypted << std::endl; } logFile.close(); } else { std::cerr << "Error reading log file." << std::endl; } } int main() { // 128-bit AES key (16 bytes) unsigned char key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x97, 0x75, 0x46, 0x26, 0x8c, 0x8a}; std::string logMessage = "This is a much longer log message that will be encrypted using AES encryption. It can be of any length!"; std::string logFilename = "encrypted_log.txt"; // Write encrypted log message to the file writeEncryptedLog(logMessage, logFilename, key); // Read and decrypt logs from the file readAndDecryptLogs(logFilename, key); return 0; }
Editor is loading...
Leave a Comment