Search Tutorials

Loading...

Friday, 24 May 2013

C code to Encrypt & Decrypt Message using Substitution Cipher

Here, we have given C program to implement Substitution Cipher to encrypt and decrypt a given message. Compile and Run it on Turbo C.

#include<stdio.h>

int main()
{
  char *message,*emessage,*dmessage;
  int i,j=0,k,key,temp;
  clrscr();

  printf("\nEnter the key\n");
  scanf("%d",&key);
  key=key%26;
  printf("\nEnter message\n");
  fflush(stdin);
  gets(message);

  for(i=0;message[i]!=NULL;i++)
  message[i]=tolower(message[i]);

  for(i=0;message[i]!=NULL;i++)
  {
  //printf("%c ",message[i]);
    if(message[i]==' ')
    emessage[j++]=message[i];

    else
        {

       if(message[i]>=48 && message[i]<=57)
       {
        temp=message[i]+key;
         if(temp>57)
         emessage[j++]=48+(temp-58);
         else
         emessage[j++]=temp;
       }
       else
           {
        if(message[i]>=97 && message[i]<=123)
          {
           temp=message[i]+key;
           if(temp>122)
             emessage[j++]=97+(temp-123);
           else
             emessage[j++]=temp;
           }
         else
              emessage[j++]=message[i];
        }

              // printf("%c ",emessage[j]);
    }

  }

  emessage[j]='\0';
  printf("\n\n\nEncrypted message is\n\n");
  for(i=0;emessage[i]!=NULL;i++)
  printf("%c",emessage[i]);
//  printf("\n end");

   for(i=0,j=0;emessage[i]!=NULL;i++)
   {

      if(emessage[i]==' ')
     dmessage[j++]=emessage[i];
      else
     {
     if(emessage[i]>=48 && emessage[i]<=57)
        {
         temp=emessage[i]-key;
         if(temp<48)
         dmessage[j++]=58-(48-temp);
         else
         dmessage[j++]=temp;
        }
      else
         {
        if(emessage[i]>=97 && emessage[i]<=123)
          {
           temp=emessage[i]-key;
           if(temp<97)
           dmessage[j++]=123-(97-temp);
           else
           dmessage[j++]=temp;
          }
         else
          dmessage[j++]=emessage[i];
         }
     }
   }
   dmessage[j]='\0';
  printf("\n\n\nRetrieved message is\n\n");
  for(i=0;dmessage[i]!=NULL;i++)
  printf("%c",dmessage[i]);
  getch();
  return(0);
}

//Output Of the above program:-

C code to Encrypt and Decrypt a message using Substitution Cipher
Substitution Cipher Result


For more c programs related to Network, Check the Network label. Share and comment to improve this blog.

Related Programs:-

★ Encrypt and Decrypt a message using Vernan Cipher

★ Encrypt and Decrypt a message using Transposition Cipher

★ Encrypt and Decrypt a message using PlayFair Cipher

★ Calculate compression ratio

★ Java code to implement RSA Algorithm

5 comments:

  1. whay this coding can't run

    ReplyDelete
  2. You missed some header files!

    ReplyDelete
  3. You missed some header files!

    ReplyDelete
    Replies
    1. sir can you please answer my query :
      why it is written key=key%26;
      ??

      Delete
  4. why key= key%26; ??
    why not simple key

    ReplyDelete

Back to Top