To implement Monoalphabetic cipher encryptiondecryption in C. (WITH OUTPUT IMAGE)



#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
                FILE *f;
                int i,j,k=97,m=65,count=0,asc;
                int ch,a[4][26];
                char *p,*c,*plain;
                f=fopen("lab2.txt","w");
                clrscr();


                                //printing 26 latters
                                for(j=0;j<26;j++)
                                {
                                                a[0][j]=k;
                                                fprintf(f,"%c",a[0][j]);
                                                k++;
                                }

                                fprintf(f,"\n");



                                                randomize();
                //making 26 bit long key
                                for(j=0;j<=25;j++)
                                {
                                 again:
                                 a[2][j]=97+random(26);

                                   for(k=0;k<=j;k++)
                                   {
                                       if(j!=k)
                                       {  if(a[2][j]==a[2][k])
                                                    goto again;
                                       }
                                    }
                                  a[3][j]=a[2][j]-32;
                                 }

                                 //printing key in small latters
                                 for(i=0;i<=25;i++)
                                 {
                                  fprintf(f,"%c",a[2][i]);
                                 }

                                 fprintf(f,"\n");
                                //alphabets in CAPITAL
                                for(j=0;j<26;j++)
                                {a[1][j]=m;
                                fprintf(f,"%c",a[1][j]);
                                m++;
                                }

                                 //key in capital latters

                                 fprintf(f,"\n");
                                 for(i=0;i<=25;i++)
                                 {
                                  fprintf(f,"%c",a[3][i]);
                                 }




                                //ENCRYPTION PART
                                printf("\n\n::::::ENTER YOUR PLAIN TEXT:::::");
                                gets(p);


                                k=0;
                                while(*(p+count)!='\0')
                                {
                                asc=toascii(*(p+count));

                                if(asc==32)
                                {*(c+count)=32;
                                }

                                //encryption of CAPITAL latters
                                else if((asc>=65)&&(asc<=90))
                                {k=asc-65;
                                *(c+count)=a[3][k];
                                }

                                //encryption of small latters
                                else if((asc>=97)&&(asc<=122))
                                {k=asc-97;
                                *(c+count)=a[2][k];
                                }

                                count++;
                                }

                                *(c+count)='\0';
                                fprintf(f,"\n\n:::::CIPHER TEXT:::::");
                                fputs(c,f);


                                //DECRYPTION PART
                                count=0;
                                k=0;
                                while(*(c+count)!='\0')
                                {
                                asc=toascii(*(c+count));

                                if(asc==32)
                                {*(plain+count)=32;
                                }

                                //decryption of CAPITAL latters
                                else if((asc>=65)&&(asc<=90))
                                {
                                  for(i=0;i<25;i++)
                                  { if(asc==a[3][i])
                                    break;
                                  }

                                *(plain+count)=a[1][i];
                                }

                                //decryption of small latters
                                else if((asc>=97)&&(asc<=122))
                                {
                                  for(i=0;i<25;i++)
                                  { if(asc==a[2][i])
                                    break;
                                  }

                                  *(plain+count)=a[0][i];
                                  }

                                count++;
                                }

                                *(plain+count)='\0';
                                fprintf(f,"\n\n:::::DECRYPTED PLAIN TEXT:::::");

                                count=0;
                                while(*(plain+count)!='\0')
                                {fprintf(f,"%c",*(plain+count));
                                count++;
                                }

                getch();
                }




OUTPUT:

abcdefghijklmnopqrstuvwxyz
lqzginrhtwacmseukjdvybofxp
ABCDEFGHIJKLMNOPQRSTUVWXYZ
LQZGINRHTWACMSEUKJDVYBOFXP

:::::CIPHER TEXT:::::QHlyvTA

:::::DECRYPTED PLAIN TEXT:::::BHautIK



NOTE: output will be stored in a file having name as "lab2.txt"



Comments

Post a Comment