Data Encryption and Decryption Methods in C Language
In modern computer science, the security of data has become increasingly important. Encryption and decryption are effective means of protecting sensitive information. In this article, we will introduce how to implement simple data encryption and decryption methods using the C language.
What are Encryption and Decryption?
- Encryption: The process of converting original data (plaintext) into an unreadable format (ciphertext) to prevent unauthorized access.
- Decryption: The process of restoring encrypted data to a readable format so that legitimate users can access it.
Overview of Encryption Algorithms
In this tutorial, we will implement a simple substitution cipher (Caesar Cipher). This algorithm encrypts by shifting each letter in the alphabet by a fixed number of positions. For example, if we choose to shift by 3 positions, the letter ‘A’ becomes ‘D’, ‘B’ becomes ‘E’, and so on.
Caesar Cipher Example
- Plaintext: HELLO
- After shifting 3 positions: KHOOR
C Language Implementation
Below is a simple C program that demonstrates the basic implementation of the Caesar Cipher, including encryption and decryption functions.
#include <stdio.h>
#include <string.h>
#define SHIFT 3 // Define the shift amount
// Encryption function
void encrypt(char *plaintext, char *ciphertext) {
for (int i = 0; plaintext[i] != '\0'; i++) {
char ch = plaintext[i];
// Handle uppercase letters
if (ch >= 'A' && ch <= 'Z') {
ciphertext[i] = (ch - 'A' + SHIFT) % 26 + 'A';
}
// Handle lowercase letters
else if (ch >= 'a' && ch <= 'z') {
ciphertext[i] = (ch - 'a' + SHIFT) % 26 + 'a';
} else {
ciphertext[i] = ch; // Non-letter characters remain unchanged
}
}
ciphertext[strlen(plaintext)] = '\0'; // Add string terminator
}
// Decryption function
void decrypt(char *ciphertext, char *plaintext) {
for (int i = 0; ciphertext[i] != '\0'; i++) {
char ch = ciphertext[i];
// Handle uppercase letters
if (ch >= 'A' && ch <= 'Z') {
plaintext[i] = (ch - 'A' - SHIFT + 26) % 26 + 'A';
}
// Handle lowercase letters
else if (ch >= 'a' && ch <= 'z') {
plaintext[i] = (ch - 'a' - SHIFT + 26) % 26 + 'a';
} else {
plaintext[i] = ch; // Non-letter characters remain unchanged
}
}
plaintext[strlen(ciphertext)] = '\0'; // Add string terminator
}
int main() {
char text[100];
char encrypted[100];
char decrypted[100];
printf("Please enter the text to encrypt: ");
fgets(text, sizeof(text), stdin);
encrypt(text, encrypted);
printf("Encrypted text: %s\n", encrypted);
decrypt(encrypted, decrypted);
printf("Decrypted text: %s\n", decrypted);
return 0;
}
Program Explanation
-
Header File Inclusion: We include
<span>stdio.h</span>for input and output operations, and<span>string.h</span>for string operations. -
Macro Definition:
<span>#define SHIFT 3</span>defines our shift amount, set to 3 here, which can be modified as needed. -
Encrypt Function: This function takes plaintext and generates the corresponding ciphertext. It iterates through each character and converts it based on its type (uppercase or lowercase). If a non-letter character is encountered, it remains unchanged.
-
Decrypt Function: This function performs the opposite operation of encrypt, restoring the ciphertext back to plaintext. It also considers case and non-letter characters.
-
Main Function: In the main function, we first prompt the user to enter the text to encrypt, then call the encrypt and decrypt functions, and print the results.
Conclusion
In this article, we introduced a basic method for data encryption and decryption—the Caesar Cipher—and provided a specific implementation in C language. Although this method is simple, it is very helpful for learning basic programming and understanding data security concepts. We hope you can further explore more complex and secure data protection technologies based on this foundation.