-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab 8 - String Encryption and Decryption.c
More file actions
54 lines (41 loc) · 1.23 KB
/
Lab 8 - String Encryption and Decryption.c
File metadata and controls
54 lines (41 loc) · 1.23 KB
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
50
51
52
53
54
#include <stdio.h>
#include <string.h>
// Encrypt function
char *encrypt(char *str)
{
// loop through each character of the string
for(int i = 0; str[i] != '\0'; i++)
// add 20 to each character in the string
str[i] += 20;
// return the encrypted string
return str;
}
// Decrypt function
char *decrypt(char *str)
{
// loop through each character of the string
for(int i = 0; str[i] != '\0'; i++)
// subtract 20 from each character in the string
str[i] -= 20;
// return the decrypted string
return str;
}
// main function
int main()
{
//Variables
char string[255];
char *encrypted, *decrypted;
// take an input string to be encrypted from the user
printf("Enter a string to be encrypted: ");
gets(string);
// invoke the encrypt() function with the input string
encrypted = encrypt(string);
// print the encrypted message
printf("\nThe encrypted message is: %s\n", encrypted);
// invoke the decrypt() function with the encrypted string
decrypted = decrypt(encrypted);
// print the decrypted message
printf("\nThe decrypted message is: %s\n", decrypted);
return 0;
}