Write a C++ command-line application that implements a custom 10-layer encryption and…
- User
Write a C++ command-line application that implements a custom 10-layer encryption and decryption system. The logic should use rotating keys that change periodically. The system must work without internet access and without any direct connection between clients, yet still somehow synchronize keys across all clients. Ensure that the entire system is self-contained and functions purely in offline mode.
Assistant
I'll create a C++ application that implements a 10-layer encryption system with rotating keys that synchronize across offline clients. The key synchronization will work through a shared time-based algorithm, where all clients generate the same keys at the same time periods without needing to communicate.
#include <iostream> #include <string> #include <vector> #include <ctime> #include <cstring> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <random> class MultiLayerCrypto { private: static const int NUM_LAYERS = 10; static const int KEY_SIZE = 32; static const int ROTATION_INTERVAL = 3600; // 1 hour in seconds struct LayerKey { std::vector<unsigned char> key; int layer_id; }; std::vector<LayerKey> current_keys; time_t last_rotation; std::string master_seed; // Generate deterministic random bytes from seed and timestamp std::vector<unsigned char> generateDeterministicKey(const std::string& seed, time_t timestamp, int layer) { std::string combined = seed + std::to_string(timestamp) + std::to_string(layer); // Simple hash function for deterministic key generation std::mt19937 gen(std::hash<std::string>{}(combined)); std::uniform_int_distribution<> dis(0, 255); std::vector<unsigned char> key(KEY_SIZE); for (int i = 0; i < KEY_SIZE; i++) { key[i] = static_cast<unsigned char>(dis(gen)); } return key; } // Get current time period (rounded to rotation interval) time_t getCurrentPeriod() { time_t now = time(nullptr); return (now / ROTATION_INTERVAL) * ROTATION_INTERVAL; } // Rotate keys if necessary void checkAndRotateKeys() { time_t current_period = getCurrentPeriod(); if (current_period != last_rotation) { generateKeys(current_period); last_rotation = current_period; } } // Generate all layer keys for a given time period void generateKeys(time_t period) { current_keys.clear(); for (int i = 0; i < NUM_LAYERS; i++) { LayerKey lk; lk.layer_id = i; lk.key = generateDeterministicKey(master_seed, period, i); current_keys.push_back(lk); } } // XOR cipher for a single layer std::vector<unsigned char> xorLayer(const std::vector<unsigned char>& data, const std::vector<unsigned char>& key) { std::vector<unsigned char> result(data.size()); for (size_t i = 0; i < data.size(); i++) { result[i] = data[i] ^ key[i % key.size()]; } return result; } // Substitution cipher for a single layer std::vector<unsigned char> substitutionLayer(const std::vector<unsigned char>& data, const std::vector<unsigned char>& key, bool encrypt) { // Create substitution table from key std::vector<unsigned char> sbox(256); std::vector<unsigned char> inv_sbox(256); // Initialize with identity for (int i = 0; i < 256; i++) { sbox[i] = i; } // Shuffle based on key std::mt19937 gen(key[0] | (key[1] << 8) | (key[2] << 16) | (key[3] << 24)); std::shuffle(sbox.begin(), sbox.end(), gen); // Create inverse for (int i = 0; i < 256; i++) { inv_sbox[sbox[i]] = i; } std::vector<unsigned char> result(data.size()); const std::vector<unsigned char>& table = encrypt ? sbox : inv_sbox; for (size_t i = 0; i < data.size(); i++) { result[i] = table[data[i]]; } return result; } // Transposition cipher for a single layer std::vector<unsigned char> transpositionLayer(const std::vector<unsigned char>& data, const std::vector<unsigned char>& key, bool encrypt) { if (data.empty()) return data; // Use key to determine block size int block_size = (key[0] % 14) + 2; // Block size between 2 and 15 std::vector<unsigned char> result = data; // Create permutation from key std::vector<int> perm(block_size); for (int i = 0; i < block_size; i++) { perm[i] = i; } std::mt19937 gen(key[1] | (key[2] << 8)); std::shuffle(perm.begin(), perm.end(), gen); // Create inverse permutation std::vector<int> inv_perm(block_size); for (int i = 0; i < block_size; i++) { inv_perm[perm[i]] = i; } const std::vector<int>& active_perm = encrypt ? perm : inv_perm; // Apply transposition to each block for (size_t i = 0; i < data.size(); i += block_size) { size_t remaining = std::min(static_cast<size_t>(block_size), data.size() - i); std::vector<unsigned char> block(block_size, 0); for (size_t j = 0; j < remaining; j++) { block[j] = data[i + j]; } std::vector<unsigned char> permuted(block_size); for (int j = 0; j < block_size; j++) { permuted[j] = block[active_perm[j]]; } for (size_t j = 0; j < remaining; j++) { result[i + j] = permuted[j]; } } return result; } public: MultiLayerCrypto(const std::string& seed) : master_seed(seed), last_rotation(0) { checkAndRotateKeys(); } std::vector<unsigned char> encrypt(const std::vector<unsigned char>& plaintext) { checkAndRotateKeys(); std::vector<unsigned char> data = plaintext; // Apply 10 layers of encryption for (int layer = 0; layer < NUM_LAYERS; layer++) { const auto& key = current_keys[layer].key; // Alternate between different cipher types switch (layer % 3) { case 0: data = xorLayer(data, key); break; case 1: data = substitutionLayer(data, key, true); break; case 2: data = transpositionLayer(data, key, true); break; } } return data; } std::vector<unsigned char> decrypt(const std::vector<unsigned char>& ciphertext) { checkAndRotateKeys(); std::vector<unsigned char> data = ciphertext; // Apply 10 layers of decryption in reverse order for (int layer = NUM_LAYERS - 1; layer >= 0; layer--) { const auto& key = current_keys[layer].key; // Alternate between different cipher types switch (layer % 3) { case 0: data = xorLayer(data, key); break; case 1: data = substitutionLayer(data, key, false); break; case 2: data = transpositionLayer(data, key, false); break; } } return data; } void displayCurrentKeys() { std::cout << "Current keys (first 8 bytes of each):\n"; for (int i = 0; i < NUM_LAYERS; i++) { std::cout << "Layer " << i << ": "; for (int j = 0; j < 8 && j < KEY_SIZE; j++) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(current_keys[i].key[j]) << " "; } std::cout << "...\n"; } std::cout << std::dec; } time_t getNextRotationTime() { time_t current_period = getCurrentPeriod(); return current_period + ROTATION_INTERVAL; } }; // Helper functions for string/vector conversion std::vector<unsigned char> stringToVector(const std::string& str) { return std::vector<unsigned char>(str.begin(), str.end()); } std::string vectorToString(const std::vector<unsigned char>& vec) { return std::string(vec.begin(), vec.end()); } std::string vectorToHex(const std::vector<unsigned char>& vec) { std::stringstream ss; for (unsigned char byte : vec) { ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte); } return ss.str(); } std::vector<unsigned char> hexToVector(const std::string& hex) { std::vector<unsigned char> result; for (size_t i = 0; i < hex.length(); i += 2) { std::string byte_str = hex.substr(i, 2); unsigned char byte = static_cast<unsigned char>(std::stoi(byte_str, nullptr, 16)); result.push_back(byte); } return result; } void printUsage() { std::cout << "Multi-Layer Encryption System\n"; std::cout << "Usage:\n"; std::cout << " 1. encrypt <message> - Encrypt a message\n"; std::cout << " 2. decrypt <hex_ciphertext> - Decrypt a message\n"; std::cout << " 3. keys - Display current keys\n"; std::cout << " 4. time - Show next key rotation time\n"; std::cout << " 5. file-encrypt <input_file> <output_file> - Encrypt a file\n"; std::cout << " 6. file-decrypt <input_file> <output_file> - Decrypt a file\n"; std::cout << " 7. exit - Exit the program\n\n"; } int main() { // Master seed - this must be the same across all clients const std::string MASTER_SEED = "SharedMasterSeed2024!@#$%^&*()"; MultiLayerCrypto crypto(MASTER_SEED); std::cout << "=== 10-Layer Offline Encryption System ===\n"; std::cout << "All clients using the same master seed will generate\n"; std::cout << "identical keys at the same time periods.\n\n"; printUsage(); std::string command; while (true) { std::cout << "\n> "; std::cin >> command; if (command == "encrypt") { std::string message; std::cin.ignore(); std::getline(std::cin, message); auto plaintext = stringToVector(message); auto ciphertext = crypto.encrypt(plaintext); std::cout << "Encrypted (hex): " << vectorToHex(ciphertext) << "\n"; } else if (command == "decrypt") { std::string hex_cipher; std::cin >> hex_cipher; try { auto ciphertext = hexToVector(hex_cipher); auto plaintext = crypto.decrypt(ciphertext); std::cout << "Decrypted: " << vectorToString(plaintext) << "\n"; } catch (...) { std::cout << "Error: Invalid hex format\n"; } } else if (command == "keys") { crypto.displayCurrentKeys(); } else if (command == "time") { time_t next_rotation = crypto.getNextRotationTime(); std::cout << "Next key rotation: " << ctime(&next_rotation); time_t now = time(nullptr); int seconds_left = next_rotation - now; std::cout << "Time until rotation: " << seconds_left << " seconds\n"; } else if (command == "file-encrypt") { std::string input_file, output_file; std::cin >> input_file >> output_file; std::ifstream in(input_file, std::ios::binary); if (!in) { std::cout << "Error: Cannot open input file\n"; continue; } std::vector<unsigned char> data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); in.close(); auto encrypted = crypto.encrypt(data); std::ofstream out(output_file, std::ios::binary); if (!out) { std::cout << "Error: Cannot create output file\n"; continue; } out.write(reinterpret_cast<const char*>(encrypted.data()), encrypted.size()); out.close(); std::cout << "File encrypted successfully\n"; } else if (command == "file-decrypt") { std::string input_file, output_file; std::cin >> input_file >> output_file; std::ifstream in(input_file, std::ios::binary); if (!in) { std::cout << "Error: Cannot open input file\n"; continue; } std::vector<unsigned char> data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); in.close(); auto decrypted = crypto.decrypt(data); std::ofstream out(output_file, std::ios::binary); if (!out) { std::cout << "Error: Cannot create output file\n"; continue; } out.write(reinterpret_cast<const char*>(decrypted.data()), decrypted.size()); out.close(); std::cout << "File decrypted successfully\n"; } else if (command == "exit") { break; } else { std::cout << "Unknown command. Type a number 1-7 or command name.\n"; printUsage(); } } return 0; }This implementation provides a complete 10-layer encryption system with the following features:
Key Synchronization Without Communication:
- All clients use the same master seed
- Keys are generated deterministically based on the current time period
- Time periods are rounded to hourly intervals (configurable)
- Any client at the same time will generate identical keys
**10-Layer
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.