Find us on Google+ Kill the code: Hill Cipher in C

Monday, 6 February 2012

Hill Cipher in C

Just paste this program in your Turbo C/C++ compiler...



#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);
//clrscr();
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
//printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
//printf("final : %d ",txt[i]);
//printf("\n\n");
}
clrscr();
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
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;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
getch();
}


8 comments:

  1. #include
    #include
    #include
    void main()
    {
    char c[10]={0},p[10],d[10]={0};
    int i,l,k;
    clrscr();
    printf("Enter msg:");
    gets(p);
    printf("\n");
    printf("Enter keysize:");
    scanf("%d",&k);
    puts(p);
    for(i=0;i<10;i++)
    {
    if(p[i]>=65 && p[i]<=96)
    {
    c[i]=((p[i]-65+k)%26)+65;
    }
    else if(p[i]>=97 && p[i]<=122)
    {
    c[i]=((p[i]-97+k)%26)+97;
    }
    }
    printf("\n");
    puts(c);
    for(i=0;i<10;i++)
    {
    if(c[i]>=65 && c[i]<=96)
    {
    if((c[i]-65-k)<0)
    {
    d[i]=c[i]-k+26;
    }
    else
    {
    d[i]=((c[i]-65-k)%26)+65;
    }
    }
    else if(c[i]>=97 && c[i]<=122)
    {
    if((c[i]-97-k)<0)
    {
    d[i]=c[i]-k+26;
    }
    else
    {
    d[i]=((c[i]-97-k)%26)+97;
    }
    }
    }
    printf("\n");
    puts(d);
    getch();
    }
    This is the program for caeser cipher. very simple logic,try it.....

    ReplyDelete
  2. For the hill cipher, what if you have a file that has about 100 character? what has to change in the program? when i run the program with a lot of characters it times-out.

    ReplyDelete
    Replies
    1. You just have to change the size of the c[10] to c[100] and change the condition in the for loop from i<10 to i<100....although, give me your mail address....I'll get the code to you in a day...

      Delete
  3. hi im ilackiya .im doing mphil computer science .i need coding for image encryption using hill cipher in c for project purpose.

    ReplyDelete
  4. I am not able to run the hill cipher program. :(

    ReplyDelete
  5. i have my string of 165 characters, and key of 3 X 3 matrix..... wanted to solve by Hill Cipher.... Plz help.....
    my mail id is:
    krish_mystyle05@rediffmail.com

    ReplyDelete