-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvigenere.c
More file actions
66 lines (63 loc) · 1.63 KB
/
Copy pathvigenere.c
File metadata and controls
66 lines (63 loc) · 1.63 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
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
// Get the key to the cipher
string key = argv[1];
if (argc != 2)
{
printf("Please give me a valid key\n");
return 1;
}
else
{
//Check that it is a letter key
for (int i = 0, n = strlen(key); i < n; i++)
{
if (isalpha(key[i]))
{
}
else
{ printf("Invalid key. Try again\n");
return 1;
}
}
}
//Get the text to encrypt
//printf("What is your message? ");
string plntxt = get_string("What is your message? ");
if (plntxt != NULL)
{
//Encrypt and print
for (int i = 0, j = 0, n = strlen(plntxt); i < n; i++, j++)
{
if (j > strlen(key) - 1)
{
j = 0;
}
int c = 0;
//Check case of textual character
//Assign it an alphabet number and encrypt
if (isupper(plntxt[i]))
{
c = (((plntxt[i] - 65) + (tolower(key[j]) - 97))%26) + 65;
printf("%c", (char)c);
}
else if (islower(plntxt[i]))
{
c = (((plntxt[i] - 97) + (tolower(key[j]) - 97))%26) + 97;
printf("%c", (char)c);
}
else
{
printf("%c", plntxt[i]);
j--;
}
}
}
printf("\n");
return 0;
}