-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_decrypt.c
86 lines (83 loc) · 2.14 KB
/
encrypt_decrypt.c
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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
void Encrypt_Decrypt(char *array, char *string, long int key)
{
int i, j;
for (i = strlen(array) - 1; i > 0; i--)
{
j = key % i;
char temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int main()
{
char array[100] = " ", sarray[100] = " ", string[1000] = " ", result[1000] = " ", choice = ' ';
long int key = 0;
int i = 0, j = 0;
for (i = 32; i < 127; i++)
{
array[i - 32] = i;
}
strcpy(sarray, array);
choose:
printf("\n1.Encryption\n2.Decryption\n");
printf("What do you wanna do? ");
scanf("%c", &choice);
getchar();
if (choice == '1')
{
printf("\nPress Y if you want to set a custom key\nPress anything else to generate a random key: ");
scanf("%c", &choice);
getchar();
if (choice == 'Y' || choice == 'y')
{
printf("\nSet up a number key: ");
scanf("%ld",&key);
getchar();
}
else
{
srand(time(NULL));
for (i = 0; i <= 500; i++)
{
srand(rand() - (i / rand()));
}
key = rand();
printf("\nYour key- %ld\n",key);
choice = '1';
}
printf("\nEnter the string you want to Encrypt: ");
fgets(string, sizeof(string), stdin);
Encrypt_Decrypt(sarray, string, key);
}
else if (choice == '2')
{
printf("\nEnter the string you want to Decrypt: ");
fgets(string, sizeof(string), stdin);
printf("\nEnter the key: ");
scanf("%ld", &key);
Encrypt_Decrypt(array, string, key);
}
else
{
printf("\nChoose between 1 and 2\n");
goto choose;
}
for (i = 0; i < strlen(string); i++)
{
for (j = 0; j < strlen(array); j++)
{
if (string[i] == array[j])
{
result[i] = sarray[j];
continue;
}
}
}
choice == '2'?printf("\nDecryption : %s", result):printf("\nEncryption : %s", result);
return 0;
}