-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathText_Encryption.c
74 lines (74 loc) · 1.73 KB
/
Text_Encryption.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
#include<cs50.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void encrypt(string pt, string k)//function to encrypt the text
{
int plen, i, n;
string ct = pt;
plen = strlen(pt);
for (i = 0; i < plen; ++i)
{
if (isalpha(pt[i]))//checks if the character is an alphabet
{
if (isupper(pt[i]))
{
n = (int)pt[i] - 65;
ct[i] = toupper(k[n]);
}
else
{
n = (int)pt[i] - 97;
ct[i] = tolower(k[n]);
}
}
else
{
ct[i] = pt[i];
}
}
printf("ciphertext: %s\n", ct);//displays encrypted text
}
int main(int argc, string key[])//inputs key
{
int i, j, l, f;
string pt;
if (argc != 2)//checks if only 1 key is entered
{
printf("Usage: ./substitution key\n");
return 1;
}
l = strlen(key[1]);
if (l != 26)//checks if there are 26 characters
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (i = 0; i < 26; ++i)
{
if (!isalpha(key[1][i]))//checks if key contains only alphabets
{
printf("Key must contain only alphabets.\n");
return 1;
}
}
for (i = 0; i < 26; ++i)
{
f = 0;
for (j = i + 1; j < 26; ++j)
{
if (tolower(key[1][i]) == tolower(key [1][j]))//checks for repeated characters
{
f++;
}
}
if (f > 0)
{
printf("Key must not contain repeated characters.\n");
return 1;
}
}
pt = get_string("plaintext: ");//inputs text
encrypt(pt, key[1]);
return 0;
}