-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage_encrypt_decrypt.c
More file actions
100 lines (88 loc) · 3.29 KB
/
Message_encrypt_decrypt.c
File metadata and controls
100 lines (88 loc) · 3.29 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <string.h>
const int SIZE = 500;
int encryptor(char input_msg[], int);
int decryptor(char input_msg[], int true_size);
/* We add system("pause"); before return command of main function to keep the powershell open after executing otherwise powershell automatically get closed after execution of .exe program */
/* You can also add a personal code to encrypt a message such that only that person can decrypt the message*/
/* we are using charset scanf (scanf("%[^\n]%*c")) for not to take enter as next input (for clearing the INPUT BUFFER)
GO and learn about charset scanf , input/output buffer and how to clear it ,how to return array by as a function output */
int main()
{
char option;
char input_msg[SIZE];
int true_size;
int i = 2;
while (i == 2)
{
printf("\nWelcome to A.j services \nselect any option :-\n1)Message encryption\n2)Message decryption\n3)Exit\n\nEnter option : ");
scanf("%[^\n]%*c", &option);
if (option == '1')
{
printf("CAUTION :- Message size should be less than 500 letters (including spaces)\nEnter message to encrypt : ");
gets(input_msg);
true_size = strlen(input_msg);
encryptor(input_msg, true_size);
}
else if (option == '2')
{
printf("CAUTION :- Message size should be less than 500 letters (including spaces)\nPaste the encrypted message here : ");
gets(input_msg);
true_size = strlen(input_msg);
decryptor(input_msg, true_size);
}
else if (option == '3')
{
printf("\nThank you ,For giving us a chance to Serve You. Hope you like it.\n\n");
system("pause");
return 1;
}
printf("Do you want to continue ? (y/n) :- ");
char answer;
scanf("%[^\n]%*c", &answer);\
/* Quaternary operator */
i = answer == 'y' ? 2 : 8, printf("Thank you ,For giving us a chance to Serve You. Hope you like it.");
}
system("pause");
return 0;
}
int encryptor(char input_msg[], int true_size)
{
int int_array[true_size], rem, q;
int eml = 2 * true_size; //eml-> encrypt message length
char encrypt_msg[eml + 1];
for (int i = 0; i < true_size; i++)
{
int_array[i] = (int)input_msg[true_size - 1 - i];
int_array[i] = int_array[i] + i;
rem = int_array[i] % 37;
q = (int_array[i] - rem) / 37;
encrypt_msg[2 * i] = (char)(31 + rem);
encrypt_msg[2 * i + 1] = (char)(31 + q);
}
encrypt_msg[eml] = '\0';
printf("Your encrypted message is --> %s\n\n", encrypt_msg);
printf("If you want to Decrypt a Encrypted Message (of this service) Then use our Decryption service\n");
return 0;
}
int decryptor(char input_msg[], int true_size)
{
int int_array[true_size];
for (int i = 0, p = 0; i < true_size; i++)
{
int_array[i] = (int)input_msg[i];
int_array[i] = int_array[i] - 31;
}
int dml = (true_size / 2); //dml -> decrypt message length
int final_int_array[dml];
char decrypt_msg[dml + 1];
for (int i = 0, p = 0; i < true_size; i = i + 2, p++)
{
final_int_array[p] = int_array[i] + 37 * int_array[i + 1];
final_int_array[p] = final_int_array[p] - p;
decrypt_msg[dml - 1 - p] = (char)final_int_array[p];
}
decrypt_msg[dml] = '\0';
printf("Your message is : %s\n\n", decrypt_msg);
return 0;
}