MULTIPLICATIVE CIPHER in C

MULTIPLICATIVE CIPHER

Multiplicative Cipher is a classical cipher in which plaintext is mapped to a position with respect to the key value.Here we don't go that much about theory part, for that you can refer to any standard book or NPTEL Video lecture.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 30

char CT[max];

int key;

void enterKey(void);

int findInverse(void);
void senderText(void);
void receiverText(void);

void enterKey()

{
    int a,b,c;
   b=26;

   printf("Enter the Key: ");

   scanf("%d",&key);
   a=key;
   if(key>1&&key<26)
   {
       do
       {
           c=b%a;
            b=a;
        a=c;
       }while(c!=0);

       if(b!=1)

      {
           printf("\nKey is not Valid.!.!.!");
           printf("\nTry with an Valid Key....\n\n");
           exit(0);
       }
   }
   else
   {
       printf("\nKey is not Valid.!.!.!");
  printf("\nTry with an Valid Key....\n\n");
       exit(0);
   }
}

int findInverse()

{
   int i;
   for(i=0;i<26;i++)
   {
       if((key*i)%26==1)
       {
           break;
       }
   }
   return i;
}


void senderText()

{
   printf("\n\n\t*****SENDER-ENCODING*****\n");

   char text[max];
   int t,i;
   int aci_ct[max];

   printf("\nEnter the text: ");
   scanf("%s",text);
   //printf("Entered text is: %s",text);         
   t=strlen(text);
   //printf("\nText length: %d",t);
   enterKey();
   for(i=0;i<t;i++)
   {
       aci_ct[i]=(((text[i]-97)*key)%26)+97;
   }
   for(i=0;i<t;i++)
    {
       CT[i]=aci_ct[i];
   }
   printf("\n\tCipher Text: %s",CT);
}

void receiverText()

{
  printf("\n\n\t*****RECEIVER-DECODING*****\n");
   int t,i,inkey;
   t=strlen(CT);
   //printf("\nText length: %d",t);
   inkey=findInverse();
   //printf("\nInverse key: %d",inkey);
   printf("\n\tPlain Text: ");
   for(i=0;i<t;i++)
    {
       printf("%c",(((CT[i]-97)*inkey)%26)+97);                
   }
}

main()

{
   printf("\n\n\t\t\t.........MULTIPLICATIVE-CIPHER.........\n");

   senderText();

   receiverText();

   printf("\n\n");

}


Output:

.........MULTIPLICATIVE-CIPHER.........


*****SENDER-ENCODING*****

Enter the text: welcometounicoderz
Enter the Key: 7

Cipher Text: yczougcdukneouvcpt

*****RECEIVER-DECODING*****


Plain Text: welcometounicoderz


                                                     ________________

.........MULTIPLICATIVE-CIPHER.........


*****SENDER-ENCODING*****

Enter the text: welcometounicoderz
Enter the Key: 4

Key is not Valid.!.!.!
Try with an Valid Key....


For any query, please comment below.
SUBSCRIBE

https://unicoderz.blogspot.com/

Post a Comment

0 Comments