| 69 | } |
| 70 | |
| 71 | int main(int argc, char* argv[]) { |
| 72 | if (argc < 4) { |
| 73 | std::cerr << "Usage: cipher <caesar|vigenere|xor> <encode|decode> <key> <text...>" << std::endl; |
| 74 | return 1; |
| 75 | } |
| 76 | std::string cipher_type = argv[1]; |
| 77 | std::string mode = argv[2]; |
| 78 | std::string key = argv[3]; |
| 79 | std::string text; |
| 80 | for (int i = 4; i < argc; i++) { |
| 81 | if (i > 4) text += " "; |
| 82 | text += argv[i]; |
| 83 | } |
| 84 | bool decrypt = (mode == "decode" || mode == "decrypt"); |
| 85 | |
| 86 | try { |
| 87 | std::string result; |
| 88 | if (cipher_type == "caesar") { |
| 89 | int shift = std::stoi(key); |
| 90 | result = caesar(text, shift, decrypt); |
| 91 | } else if (cipher_type == "vigenere") { |
| 92 | result = vigenere(text, key, decrypt); |
| 93 | } else if (cipher_type == "xor") { |
| 94 | if (decrypt) { |
| 95 | result = xorCipher(fromHex(text), key); |
| 96 | } else { |
| 97 | result = toHex(xorCipher(text, key)); |
| 98 | } |
| 99 | } else { |
| 100 | std::cerr << "Unknown cipher: " << cipher_type << std::endl; |
| 101 | return 1; |
| 102 | } |
| 103 | std::cout << result << std::endl; |
| 104 | } catch (const std::exception& e) { |
| 105 | std::cerr << "Error: " << e.what() << std::endl; |
| 106 | return 1; |
| 107 | } |
| 108 | return 0; |
| 109 | } |