C Online Compiler
Example: XOR Cipher Encryption and Decryption in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// XOR Cipher Encryption and Decryption #include <stdio.h> #include <string.h> // Function to encrypt/decrypt a message using XOR cipher void xorCipher(char text[], char key_char) { int i; for (i = 0; text[i] != '\0'; ++i) { text[i] = text[i] ^ key_char; // XOR each character with the key } } int main() { // Step 1: Declare variables for message and key char message[100]; char key_char; // Single character key for simplicity printf("Enter a message to encrypt: "); fgets(message, sizeof(message), stdin); message[strcspn(message, "\n")] = 0; // Remove trailing newline printf("Enter a single character key: "); scanf(" %c", &key_char); // Note the space before %c to consume newline // Step 2: Encrypt the message char encryptedMessage[100]; strcpy(encryptedMessage, message); // Copy original message xorCipher(encryptedMessage, key_char); printf("Encrypted message: %s\n", encryptedMessage); // Step 3: Decrypt the message (XOR is symmetric, same function) char decryptedMessage[100]; strcpy(decryptedMessage, encryptedMessage); // Copy encrypted message xorCipher(decryptedMessage, key_char); // Apply XOR again printf("Decrypted message: %s\n", decryptedMessage); return 0; }
Output
Clear
ADVERTISEMENTS