-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenc and decry using vigener cipher.c
96 lines (92 loc) · 2.27 KB
/
enc and decry using vigener cipher.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
87
88
89
90
91
92
93
94
95
96
// Progam for Encryption and Decryption using Vigener Cipher technique //
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
char cstr[100];
char code[5] = "SSPC",c;
char cap_code[5] = "SSPC"; // Assuming this is meant to store the uppercase version of the code
int indexcode = 0, ascii;
int i,j,number,a,b, len = 0;
clrscr(); // Clearing the console screen
// Input of plain text
printf("\n\n\t\tEnter Plain Text = ");
gets(str);
i = strlen(str);
len = strlen(code) - 1;
// Encryption
for(j = 0; j < i; j++)
{
if(str[j] != '')
{
ascii = (int)str[j];
if(ascii >= 65 && ascii <=90)
{
int index = indexcode % len;
cstr[j] = code[index];
}
indexcode++;
}
else
cstr[j] = '';
}
cstr[j] = '\0'; // Adding null terminator to the end of the string
printf("\n\n\t\tKey Code = "); // Outputting the key code
puts(cstr)
for(j = 0; j < i; j++)
{
ascii = (int)str[j];
if(str[j] != '')
{
if(ascii >= 65 && ascii <= 90)
{
a = (int)str[j] % 65;
b = (int)cstr[j] % 65;
number = (a + b) % 26;
number = number + 97;
}
else if(ascii >= 97 && ascii <= 122)
{
a = (int)str[j] % 97;
b = (int)cstr[j] % 97;
number = (a + b) % 26;
number = number + 97;
}
str[j] = (char) number;
}
else str[j] = '';
}
str[j] = '\0'; // Adding null terminator to the end of the string
printf("\n\n\t\tEncrypted Text = "); // Outputting the encrypted text
puts(str);
// Decryption (same as Encryption in this case, assuming it's intended to demonstrate decryption)
for(j = 0; j++)
{
ascii = (int)str[j];
if(str[j] != '')
{
if(ascii >= 65 && ascii <= 90)
{
a = (int)str[j] % 65;
b = (int)cstr[j] % 65;
number = (a + b) % 26;
number = number + 97;
}
else if(ascii >= 97 && ascii <= 122)
{
a = (int)str[j] % 97;
b = (int)cstr[j] % 97;
number = (a + b) % 26;
number = number + 97;
}
str[j] = (char) number;
}
else str[j] = '';
}
str[j] = '\0'; // Adding null terminator to the end of the string
printf("\n\n\t\tPlain Text = "); // Outputting the decrypted text
puts(str);
getch(); // Wait for a key press
}