Hill Cipher Algorithm Program in C

What is Hill Cipher?

Today we will learn the Hill Cipher algorithm program in c with the output. Hill Cipher algorithm was invented in 1929 by Lester S. Hill. Hill cipher was the first polygraphic cipher. In Hill cipher, each letter is represented by a number modulo 26. To encrypt a message, each n block of letters will be multiplied by the n*n matrix, against modulus 26. The matrix is called a cipher key.

Also Read: Hill cipher code in python



Below is the Hill Cipher program example to know how to compute ciphertext from plain text. Here we have given an example of 3x3 matrix.
Hill Cipher Algorithm Encryption Example
Hill Cipher Example

/*Hill Cipher Program in C */

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,ans[25][1],sum=0,mtrx[25][25],end;
    char txt[25];
    clrscr();
    printf("\nEnter the string : ");
    scanf("%s",txt);

    for(i=0;i<25;i++)
    {
        if(txt[i]>=97 && txt[i]<122) {

        }
        else
        {
            end=i;
            break;
        }
    }

    for(i=0;i<end;i++)
    {
        txt[i]=txt[i]-'a';
    }

    printf("\nEnter %dX%d key matrix row by row.\n",end,end);

    for(i=0;i<end;i++)
    {
        for(j=0;j<end;j++)
        {
    printf("key[%d][%d] : ",i,j);
            scanf("%d",&mtrx[i][j]);
        }
    }
    printf("\n");

    printf("Key matrix is :\n");
    for(i = 0; i <end; i++) {
        for(j = 0; j <end; j++)
    printf("%d\t", mtrx[i][j]);

        printf("\n");
    }

    for(i=0;i<end;i++)
    {
        sum=0;
        for(j=0;j<end;j++)
        {
            sum+=mtrx[i][j]*(int)txt[j];
        }
        ans[i][0]=sum;
    }

    printf("\nCipher Text is: ");
    for(i=0;i<end;i++)
    {
        printf(" %c",((ans[i][0])%26)+97);
    }

    getch();
}

Output:
Hill Cipher Algorithm Program in C - Encryption - Output
Hill Cipher Algorithm Program in C - Encryption - Output

Tech Tip: Move your programming tools such as emulators and IDE`s online into the cloud with high-performance Citrix vdi from CloudDesktopOnline and experience the freedom to remotely catch up with your programming work on your preferred device(PC/Mac/Android/iOS). If you prefer a server, Try dedicated GPU server hosting from Apps4Rent with 24*7*365 days live tech-support & migration assistance.

Comment below if you have any queries related to the above program for hill cipher in C. We can use the same program for 2x2, 3x3, 4x4, ...,nxn matrix.

Post a Comment

8 Comments

  1. Replies
    1. unaku output la ila dA.......

      Delete
    2. Hi Sasi Dhar, Please find updated Hill cipher program with Output.

      Delete
  2. olunga output solluda sotta thalaya..sollalana kandapadikku paesuvan da

    ReplyDelete
    Replies
    1. Hi Male Sundar, Please find updated Hill cipher program with Output.

      Delete
  3. pls help Write a program that implements Hill Cipher (substitution) and Rail fence Cipher (transposition).

    ReplyDelete