Reputation: 31
i am trying to implement rsa algorithm using c program. it works well in most cases.But in some cases it does not encrypt and again decrypt back to same stage.for example: "--" after encryption and decryption changes to "ƒƒ" here is the code for encryption and decryption. help would be appreciated. thanks in advance
void encrypt(uChar state[16])
{
long int pt,ct,key=e[0],k;
i=0;
while(i<=16)
{
pt=state[i];
pt=pt-96;
k=1;
for (j=0;j<key;j++)
{
k=k*pt;
k=k%n;
}
ct=k+96;
state[i]=ct;
i++;
}
}
void decrypt(uChar state[16])
{
long int pt,ct,key=d[0],k;
i=0;
while(i<=16)
{
ct=state[i]-96;
k=1;
for (j=0;j<key;j++)
{
k=k*ct;
k=k%n;
}
pt=k+96;
state[i]=pt;
i++;
}
}
Upvotes: 0
Views: 1014
Reputation: 34540
You are indexing outside the bounds of the array with
while(i<=16)
You can index state[16]
only up to 15
so (in both functions) it should be
while(i < 16)
Upvotes: 2