Pages

Monday, December 26, 2016

Security - Playfair Key Matrix Generation



Playfair Key Matrix Generation: 

1. Read a keyword from User
2. Validate a keyword as per specification.
3. Generate and Display Playfair Key Matrix

#include<stdio.h>
int main()
{
  char arr[5][5], key[10], temp[26]="";
  int i,j,flag=0, cnt=0, key_check[26]={};
  printf("Enter your Keyword:");
  scanf("%s",key);
  //Logic to check given keyword....MONARCHY
  for(i=0; i<strlen(key); i++)
  {
key_check[key[i]%65]++;
  }
  for(i=0; i<26; i++)
  {
if(key_check[i] > 1)
flag=1;
//printf("%c=%d \n",i+65, key_check[i]);
  } 
  if(flag==1)
  {
printf("Enter proper keyword...\n");
    exit(0);
  }
  else
printf("Keyword %s is ok.\n", key);

  //Logic to fill Key Matrix using keyword..
  strcpy(arr,key);
//logic to initialize remaining cell of key matrix
  for(i=0; i<26; i++)
  {
if(key_check[i]==0)
{
temp[cnt++]=(char)key_check[i]+65+i;
//printf("%c",key_check[i]+65+i);
}
  }
  temp[cnt]='\0';
  strcat(arr,temp);
  //Logic to print Key Matrix
  printf("Your Playfair Key Matrix:\n");
  for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
printf("\n");
  }
  return 0;
}

Output of the program:

Enter your Keyword:MONARCHY
Keyword MONARCHY is ok.
Your Playfair Key Matrix:
M O N A R
C H Y B D
E F G I J
K L P Q S
T U V W X


No comments:

Post a Comment