// Implementation of Playfair Cipher in C program
#include <stdio.h> //header file
#include <stdlib.h> //header file
#include <string.h> //header file
#define SIZE 30
// this function will convert the string to lowercase
void toUpperCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 96 && plain[i] < 123)
plain[i] -= 32;
}
}
// this function will remove all the spaces
int removeSpaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}
// this function will generate the 8x8 grid square
void generateKeyTable(char key[], int ks, char keyT[8][8])
{
int i, j, k, flag = 0, *dicty;
dicty = (int*)calloc(65, sizeof(int));
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i]-33] = 2;
}
dicty['j' - 33] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++) {
if (dicty[key[k] - 33] == 2) {
dicty[key[k] - 33] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 8) {
i++;
j = 0;
}
}
}
for (k = 33; k <= 97; k++) {
if (dicty[k-33] == 0) {
keyT[i][j] = (char)(k);
j++;
if (j == 8) {
i++;
j = 0;
}
}
}
}
// this function will search for the characters of a digraph
// in the key and return position of key
void search(char keyT[8][8], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
// this function will find the modulus with 8
int mod8(int a) { return (a % 8); }
// this function will make the plain text length even
int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'X';
str[ptrs] = '\0';
}
return ptrs;
}
// encryption will done using this function
void encrypt(char str[], char keyT[8][8], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod8(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod8(a[3] + 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod8(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod8(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// this function will encrypt cipher text using Playfair Cipher algorithm
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[8][8];
ks = strlen(key);
ks = removeSpaces(key, ks);
toUpperCase(key, ks);
ps = strlen(str);
toUpperCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
}
// this function calls the decrypt function
void decrypt(char str[], char keyT[8][8], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod8(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod8(a[3] - 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod8(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod8(a[2] - 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// this function wil call the decrypt function
void decryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[8][8];
// Key text
ks = strlen(key);
ks = removeSpaces(key, ks);
toUpperCase(key, ks);
// ciphertext
ps = strlen(str);
toUpperCase(str, ps);
ps = removeSpaces(str, ps);
generateKeyTable(key, ks, keyT);
decrypt(str, keyT, ps);
}
// main code
int main()
{
char str[SIZE], key[SIZE];
// key text
strcpy(key, "Algorithm");
printf("Key text: %s\n", key);
printf("\nEncryption\n");
// Plaintext
strcpy(str, "Programming");
printf("Plain text: %s\n", str);
// encryption using the "Playfair Cipher" algorithmn
encryptByPlayfairCipher(str, key);
printf("Cipher text: %s\n", str);
printf("\nDecryption\n");
// Ciphertext that needs to be decrypted
printf("Plain text: %s\n", str);
// encryption is done using Playfair Cipher algorithm
decryptByPlayfairCipher(str, key);
printf("Deciphered text: %s\n", str);
return 0;
}
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}