Pages

Friday, 24 May 2013

C code to Encrypt & Decrypt Message using Transposition Cipher

C program to implement Transposition Cipher to encrypt and decrypt a given message. This program is tested on Turbo C software.

#include<stdio.h>
#include<string.h>

void cipher(int i,int c);
int findMin();
void makeArray(int,int);

char arr[22][22],darr[22][22],emessage[111],retmessage[111],key[55];
char temp[55],temp2[55];
int k=0;

int main()
{
  char *message,*dmessage;

  int i,j,klen,emlen,flag=0;
  int r,c,index,min,rows;
  clrscr();

  printf("Enetr the key\n");
  fflush(stdin);
  gets(key);

  printf("\nEnter message to be ciphered\n");
  fflush(stdin);
  gets(message);

  strcpy(temp,key);
  klen=strlen(key);

  k=0;
  for(i=0; ;i++)
  {
    if(flag==1)
    break;

    for(j=0;key[j]!=NULL;j++)
    {
      if(message[k]==NULL)
       {
         flag=1;
         arr[i][j]='-';
       }
       else
       {
       arr[i][j]=message[k++];
       }
     }
  }
  r=i;
  c=j;

  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
    printf("%c ",arr[i][j]);
    }
    printf("\n");
  }

k=0;

  for(i=0;i<klen;i++)
  {
   index=findMin();
   cipher(index,r);
  }

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

   printf("\n\n");
   //deciphering

   emlen=strlen(emessage);
   //emlen is length of encrypted message

   strcpy(temp,key);

   rows=emlen/klen;
   //rows is no of row of the array to made from ciphered message
   rows;
   j=0;


   for(i=0,k=1;emessage[i]!=NULL;i++,k++)
    {
      //printf("\nEmlen=%d",emlen);
      temp2[j++]=emessage[i];
      if((k%rows)==0)
       {
       temp2[j]='\0';
       index=findMin();
       makeArray(index,rows);
       j=0;
       }
    }

    printf("\nArray Retrieved is\n");

     k=0;
     for(i=0;i<r;i++)
     {
    for(j=0;j<c;j++)
    {
    printf("%c ",darr[i][j]);
    //retrieving message
    retmessage[k++]=darr[i][j];

    }
    printf("\n");
     }
       retmessage[k]='\0';

     printf("\nMessage retrieved is\n");

     for(i=0;retmessage[i]!=NULL;i++)
     printf("%c",retmessage[i]);

  getch();
  return(0);
}

void cipher(int i,int r)
{
  int j;
  for(j=0;j<r;j++)
  {
    {
    emessage[k++]=arr[j][i];
    }
  }
 // emessage[k]='\0';
}

void makeArray(int col,int row)
{
 int i,j;

    for(i=0;i<row;i++)
       {
       darr[i][col]=temp2[i];
       }
}

int findMin()
{
  int i,j,min,index;

       min=temp[0];
       index=0;
       for(j=0;temp[j]!=NULL;j++)
       {
    if(temp[j]<min)
      {
      min=temp[j];
      index=j;
      }
       }

      temp[index]=123;
       return(index);
}

Output of the above program:-

C code to encrypt and decrypt a message using Transposition Cipher
Encrypted Message using Transposition Cipher

C code to encrypt and decrypt a message using Transposition Cipher
Decrypted Message using Transposition Cipher

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

Related Programs:-

Encrypt and Decrypt a message using PlayFair Cipher

Calculate compression ratio

Java code to implement RSA Algorithm

Java code to implement MD5 Algorithm

Java code to send and receive Text or Image File

4 comments:

  1. looks like the cipher function is broken. there is mismatched braces, it should iterate over i as well.
    void cipher(int i,int r)
    {
    int j;
    for(j=0;j<r;j++)
    {
    {
    emessage[k++]=arr[j][i];
    }
    }
    // emessage[k]='\0';
    }

    ReplyDelete
  2. #include
    #include
    #include
    void rowtrans(char p[10][10]);
    void coltrans(char p[10][10]);
    int row,col,i,j;
    char p[10][10];
    void main()
    {
    int ch=0,l=0;
    char plain[100];
    clrscr();
    printf("Enter the plain text = ");
    gets(plain);
    printf("Enter no of rows and column = ");
    scanf("%d",&row);
    scanf("%d",&col);
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    if(plain[l]!='\0')
    {
    if(plain[l]==' ')
    l++;
    p[i][j]=plain[l];
    l++;
    }
    else
    { break; }
    }
    }
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c ",p[i][j]);
    }
    printf("\n");
    }
    while(ch<3)
    {
    printf("\n\n1.Row Transposition \n2.Column Transposition \n3.Exit");
    printf("\nEnter your choice = ");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
    rowtrans(p);
    break;
    case 2:
    coltrans(p);
    break;
    }
    }
    getch();
    }
    void rowtrans(char p[10][10])
    {
    int l=0,k[10],u;
    char c[10][10],x[10][10];
    printf("Enter the key to arrange 1 to %d row =\n",row);
    for(i=0;i<row;i++)
    scanf("%d",&k[i]);
    printf("\n**********Encryption**********\n");
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    u=k[l]-1;
    c[i][j]=p[u][j];
    }
    l++;
    }
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c ",c[i][j]);
    }
    printf("\n");
    }
    printf("Cipher Text = ");
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c",c[i][j]);
    }
    }
    printf("\n**********Decryption**********\n");
    l=0;
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    u=k[l]-1;
    x[u][j]=c[i][j];
    }
    l++;
    }
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c ",x[i][j]);
    }
    printf("\n");
    }
    printf("Plain Text = ");
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c",x[i][j]);
    }
    }
    }
    void coltrans(char p[10][10])
    {
    int l=0,k[10],u;
    char c[10][10],x[10][10];
    printf("Enter the key to arrange 1 to %d column =\n",col);
    for(i=0;i<col;i++)
    scanf("%d",&k[i]);
    printf("\n**********Encryption**********\n");
    for(i=0;i<row;i++)
    { l=0;
    for(j=0;j<col;j++)
    {
    u=k[l]-1;
    c[i][j]=p[i][u];
    l++;
    }
    }
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c ",c[i][j]);
    }
    printf("\n");
    }
    printf("Cipher Text = ");
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c",c[i][j]);
    }
    }
    printf("\n**********Decryption**********\n");
    l=0;
    for(i=0;i<row;i++)
    { l=0;
    for(j=0;j<col;j++)
    {
    u=k[l]-1;
    x[i][u]=c[i][j];
    l++;
    }
    }
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c ",x[i][j]);
    }
    printf("\n");
    }
    printf("Plain Text = ");
    for(i=0;i<row;i++)
    {
    for(j=0;j<col;j++)
    {
    printf("%c",x[i][j]);
    }
    }
    }

    ReplyDelete
    Replies
    1. Can U Share Any Youtube video to explain this.

      Delete

Back to Top