playfair cipher code in c

Solutions on MaxInterview for playfair cipher code in c by the best coders in the world

showing results for - "playfair cipher code in c"
Maite
30 Jul 2018
1#include <stdio.h>
2#include <conio.h>
3#include <string.h>
4#include <ctype.h>
5#define MX 5
6
7void playfair(char ch1, char ch2, char key[MX][MX])
8{
9    int i, j, w, x, y, z;
10    FILE *out;
11    if ((out = fopen("cipher.txt", "a+")) == NULL)
12    {
13        printf("File Currupted.");
14    }
15    for (i = 0; i < MX; i++)
16    {
17        for (j = 0; j < MX; j++)
18        {
19            if (ch1 == key[i][j])
20            {
21                w = i;
22                x = j;
23            }
24            else if (ch2 == key[i][j])
25            {
26                y = i;
27                z = j;
28            }
29        }
30    }
31    //printf("%d%d %d%d",w,x,y,z);
32    if (w == y)
33    {
34        x = (x + 1) % 5;
35        z = (z + 1) % 5;
36        printf("%c%c", key[w][x], key[y][z]);
37        fprintf(out, "%c%c", key[w][x], key[y][z]);
38    }
39    else if (x == z)
40    {
41        w = (w + 1) % 5;
42        y = (y + 1) % 5;
43        printf("%c%c", key[w][x], key[y][z]);
44        fprintf(out, "%c%c", key[w][x], key[y][z]);
45    }
46    else
47    {
48        printf("%c%c", key[w][z], key[y][x]);
49        fprintf(out, "%c%c", key[w][z], key[y][x]);
50    }
51    fclose(out);
52}
53void main()
54{
55    int i, j, k = 0, l, m = 0, n;
56    char key[MX][MX], keyminus[25], keystr[10], str[25] = {0};
57    char alpa[26] = {
58        'A',
59        'B',
60        'C',
61        'D',
62        'E',
63        'F',
64        'G',
65        'H',
66        'I',
67        'J',
68        'K',
69        'L',
70        'M',
71        'N',
72        'O',
73        'P',
74        'Q',
75        'R',
76        'S',
77        'T',
78        'U',
79        'V',
80        'W',
81        'X',
82        'Y ',
83        'Z'};
84    printf("\nEnter key:");
85    gets(keystr);
86    printf("\nEnter the plain text:");
87    gets(str);
88    n = strlen(keystr);
89    //convert the characters to uppertext
90    for (i = 0; i < n; i++)
91    {
92        if (keystr[i] == 'j')
93            keystr[i] = 'i';
94        else if (keystr[i] == 'J')
95            keystr[i] = 'I';
96        keystr[i] = toupper(keystr[i]);
97    }
98    //convert all the characters of plaintext to uppertext
99    for (i = 0; i < strlen(str); i++)
100    {
101        if (str[i] == 'j')
102            str[i] = 'i';
103        else if (str[i] == 'J')
104            str[i] = 'I';
105        str[i] = toupper(str[i]);
106    }
107    // store all characters except key
108    j = 0;
109    for (i = 0; i < 26; i++)
110    {
111        for (k = 0; k < n; k++)
112        {
113            if (keystr[k] == alpa[i])
114                break;
115            else if (alpa[i] == 'J')
116                break;
117        }
118        if (k == n)
119        {
120            keyminus[j] = alpa[i];
121            j++;
122        }
123    }
124    //construct key keymatrix
125    k = 0;
126    for (i = 0; i < MX; i++)
127    {
128        for (j = 0; j < MX; j++)
129        {
130            if (k < n)
131            {
132                key[i][j] = keystr[k];
133                k++;
134            }
135            else
136            {
137                key[i][j] = keyminus[m];
138                m++;
139            }
140            printf("%c ", key[i][j]);
141        }
142        printf("\n");
143    }
144    // construct diagram and convert to cipher text
145    printf("\n\nEntered text :%s\nCipher Text :", str);
146    for (i = 0; i < strlen(str); i++)
147    {
148        if (str[i] == 'J')
149            str[i] = 'I';
150        if (str[i + 1] == '\0')
151            playfair(str[i], 'X', key);
152        else
153        {
154            if (str[i + 1] == 'J')
155                str[i + 1] = 'I';
156            if (str[i] == str[i + 1])
157                playfair(str[i], 'X', key);
158            else
159            {
160                playfair(str[i], str[i + 1], key);
161                i++;
162            }
163        }
164    }
165    getch();
166}