Quote:
problem in coding for inverse the key matrix used for encryption and decryption in Hill cipher
This tell us absolutely nothing because every question here is about something that don't work.
A description of how things go wrong would be a good start.
Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char aa[26]="abcdefghijklmnopqrstuvwxyz";
char pt[10];
int m,d,q=0,i,j,k[2][2],p[4],pp[4],t[5];
int k1[2][2],k2[2][2],det;
printf("enter the plaintext:" );
scanf("%s",pt);
m=strlen(pt);
printf("enter the numbers:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&k[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=i;j<26;j++)
{
if(pt[i]==aa[j])
{
t[q]=j%26;
++q;
}
}
}
p[0]=(k[0][0]*t[0])+(k[0][1]*t[1]);
p[1]=(k[1][0]*t[0])+(k[1][1]*t[1]);
p[2]=(k[0][0]*t[2])+(k[0][1]*t[3]);
p[3]=(k[1][0]*t[2])+(k[1][1]*t[3]);
k1[0][0]=k[1][1];
k1[0][1]=-(k[0][1]);
k1[1][0]=-(k[1][0]);
k1[1][1]=k[0][0];
det=(k1[0][0]*k1[1][1])-(k1[0][1]*k1[1][0]);
for(i=0;i<26;i++)
{
if((det*i)%26==1)
{
d=i;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
k2[i][j]=(d*k1[i][j])%26;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(k2[i][j]<0)
k2[i][j]+=26;
}
}
pp[0]=((k2[0][0]*p[0])+(k2[0][1]*p[1]))%26;
pp[1]=((k2[1][0]*p[0])+(k2[1][1]*p[1]))%26;
pp[2]=((k2[0][0]*p[2])+(k2[0][1]*p[3]))%26;
pp[3]=((k2[1][0]*p[2])+(k2[1][1]*p[3]))%26;
for(i=0;i<m;i++)
{
printf("\nThe decrypted plain text :%c",aa[pp[i]]);
}
getch();
}
Professional programmer's editors have this feature and others such as parenthesis matching and syntax highlighting.
Notepad++ Home[
^]
ultraedit[
^]
Wheb you don't understand what go wrong in your code, the answer is debugger.
There is a tool that allow you to see what your code is doing, its name is
debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is
debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[
^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[
^]
Basic Debugging with Visual Studio 2010 - YouTube[
^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.