Encryption/Decryption Algorithm Gaming Project in C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <iostream> #include <string> using namespace std; // Function to encrypt a message using Caesar cipher string encrypt(const string& message, int shift) { string encryptedMessage = message; for (char& c : encryptedMessage) { if (isalpha(c)) { char base = islower(c) ? 'a' : 'A'; c = (c - base + shift) % 26 + base; } } return encryptedMessage; } // Function to decrypt a message using Caesar cipher string decrypt(const string& message, int shift) { return encrypt(message, 26 - shift); // Decryption is encryption with (26 - shift) } int main() { string message; int shift; cout << "Enter the message to encrypt: "; getline(cin, message); cout << "Enter the shift value (1-25): "; cin >> shift; if (shift < 1 || shift > 25) { cout << "Invalid shift value. It should be between 1 and 25." << endl; return 1; } // Encrypt the message string encryptedMessage = encrypt(message, shift); cout << "Encrypted message: " << encryptedMessage << endl; // Decrypt the message string decryptedMessage = decrypt(encryptedMessage, shift); cout << "Decrypted message: " << decryptedMessage << endl; return 0; } |
Explanation: Caesar Cipher Encryption (encrypt): The encrypt function shifts each letter in the input message by a specified number of positions in the alphabet. It loops through each character in the message: If the character is a letter, it calculates its new position by shifting it and wraps around the end of the alphabet …
Encryption/Decryption Algorithm Gaming Project in C++ Read More »
