Implementing a Simple Encryption and Decryption Tool in C
In today’s article, we will implement a simple encryption and decryption tool using the C programming language. This tool is based on a very basic character substitution algorithm, allowing users to input a plaintext, encrypt it using a defined method, and then decrypt it back through another process.
Tool Overview
The functionalities we aim to implement are as follows:
- The user inputs a piece of text.
- The text is encrypted using a simple method (for example, a shift value).
- The user can choose to input the encrypted text to decrypt it back to the original text.
Code Structure
Our program needs to include several basic parts:
- Loading user input
- Encryption function
- Decryption function
- Main function to control the flow
Next, we will build this tool step by step.
Code Demonstration
The following code demonstrates the complete implementation of the tool:
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 256 // Set maximum length constant
// Encryption function, using a simple character shift method
void encrypt(char *input, char *output, int shift) {
for (int i = 0; input[i] != '\0'; i++) {
output[i] = input[i] + shift; // Shift each character
}
output[strlen(input)] = '\0'; // Add string terminator
}
// Decryption function, restoring characters using the same method
void decrypt(char *input, char *output, int shift) {
for (int i = 0; input[i] != '\0'; i++) {
output[i] = input[i] - shift; // Restore each shifted character
}
output[strlen(input)] = '\0'; // Add string terminator
}
int main() {
char text[MAX_LENGTH]; // To store user input plaintext or ciphertext
char encrypted[MAX_LENGTH]; // To store the encrypted text
char decrypted[MAX_LENGTH]; // To store the restored plaintext
int shift; // Define the shift value
printf("Please enter the text to encrypt: ");
fgets(text, MAX_LENGTH, stdin); // Read text from standard input
printf("Please enter the shift value (positive integer): ");
scanf("%d", &shift);
// Call the encryption function
encrypt(text, encrypted, shift);
printf("Encrypted text: %s\n", encrypted);
// Call the decryption function
decrypt(encrypted, decrypted, shift);
printf("Decrypted text: %s\n", decrypted);
return 0;
}
Program Explanation
-
<span>encrypt</span>
function: This function takes three parameters: the string to be encrypted, the output result, and the shift value. It iterates through each character and adjusts the ASCII code according to the shift value, generating a new string as the “ciphertext”. -
<span>decrypt</span>
function: Corresponding to<span>encrypt</span>
, here we perform the reverse operation on the input content, obtaining the original text by subtracting the shift value used during character encoding. -
Main program
<span>main</span>
:
- First, it retrieves user input, including the information to be encrypted and the specified shift value (e.g., how many positions).
- It calls the two functions mentioned above to process the content and outputs the results.
Usage Instructions
- Compile and run the program.
- Follow the prompts to input the text to be encrypted and the shift value, separated by a space.
- After the program completes, it will display the corresponding outputs, including the encrypted and decrypted texts.
I hope this tutorial helps you understand the basic programming logic in C! If you enjoy such projects, you can continue to delve into topics related to programming security and more.