The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Can anyone see any errors in this cipher code?
Discuss Can anyone see any errors in this cipher code? in the C Programming forum on Dev Shed. Can anyone see any errors in this cipher code? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 4th, 2012, 11:11 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 10 m 41 sec
Reputation Power: 0
|
|
|
Can anyone see any errors in this cipher code?
I need to know if there are any indentation errors and why the output of the cipher code has ASCII code 203 symbol in it?
Code:
#pragma warning( disable:4996)
#include<stdio.h>
//function
int FindGCD(int no1,int no2);
//for small letters only
int InvEuclid(int a,int N) {
int r0,r1,r2,q1,x0,x1,x2;
r0=N;
r1=a;
x0=1;
r2=r0%r1;
q1=r0/r1;
x1=-q1;
while (r2)
{
r0=r1;
r1=r2;
q1=r0/r1;
r2=r0%r1;
x2=x0-(q1*x1);
x0=x1;x1=x2;
}
if (x0>0)
return x0;
else
return (N+x0);
}
int main () {
char small[27],data[100],cipher[100],decipher[100];
int i=0,j=0,k1,k2,flag,count=0,temp;
for (i;i<26;i++){
small[i] = 'a'+i;
//printf("%c",small[i]);
}
// ask user to input message
printf("please enter the text message to encrypt\n");
scanf("%[^\n]s",data);
fflush(stdin);
//ask user to provide at least two mapping
i=0;
while(1)
{
printf("\nplease enter the first key 1<k1<26 such that gcd of (k2,26)=1\n"); //first key is: 19
scanf("%d",&k1);
printf("\nplease enter the second key 1<k2<26\n "); // second key is: 4
scanf("%d",&k2);
flag =FindGCD(k1,26);
if (flag==1)
break;
else
printf("\nPlease re-enter the keys");
}
//cipher the text and show it to the user
while (1)
{
temp = data[i];
// printf("g%d",flag);
if (temp==0)
break;
flag= data[i]-'a';
if (flag>=0 && flag <=25)
{
flag=(flag*k1) +k2;
//printf("g%d",flag);
flag= flag%26;
cipher[i]=small[flag];
}
else
{
cipher[i]=data[i];
}
count++;
i++;
}
// show encrypted text to the user
printf("\nthe encrypted string is\n ");
while(j<i)
{
printf("%c",cipher[j]);
j++;
}
printf("\n");
//decipher the ciphered text
// printf("c%d\n",count);
k1= InvEuclid(k1,26);
//printf("%d",k1);
i=0;
for(i=0;i<count;i++)
{
flag= cipher[i]-'a';
//printf("%d",flag);
if (flag>=0 && flag <=25)
{
flag=(flag-k2)*k1;
//printf("g%d",flag);
flag= flag%26;
decipher[i]=small[flag];
}
else
{
decipher[i]=data[i];
}
}
j=0;
// show user the de-encrypted string
printf("\nthe de-encrypted string is\n ");
while(j<i)
{
printf("%c",decipher[j]);
j++;
}
printf("\n");
return 0;
}
int FindGCD(int no1,int no2){
int divd,divs,r;
if (no1>no2)
{
divd=no1;
divs = no2;
}
else
{
divd= no2;
divs=no1;
}
r = divd%divs;
while (r>0)
{
divd = divs;
divs = r;
r= divd%divs;
if (r==1)
{
return 1;
}
}
if (r==0)
{
return divs;
}
return 0;
}
|

December 5th, 2012, 05:41 AM
|
|
|
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!
|

December 5th, 2012, 01:53 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 10 m 41 sec
Reputation Power: 0
|
|
|
so are you saying this board is not as good as Cboard and you can't find an issue either?
|

December 5th, 2012, 02:38 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
There's no reason for any of us to make any effort to answer your question. We'll just leave that up to those other folks on the other forums you've cross-posted to.
Of course, if they also cop the same attitude, then you'll get no answers at all. But that's the risk you run when you do rude things like cross-posting.
|

December 5th, 2012, 02:43 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 10 m 41 sec
Reputation Power: 0
|
|
|
Sorry. I didn't realize there was a war going on here. I thought the purpose of these boards was to help others when they are stuck. So far no one has found any issues so I guess I need to ask for help on yet some other forum where I may find genuine people who are looking to help others!
|

December 5th, 2012, 05:35 PM
|
|
|
Concentrate your effort in the area of the following code fragment. Your logic is flawed which at times causes the flag index to go negative
Code:
for(i=0;i<count;i++)
{
flag= cipher[i]-'a';
//printf("%d",flag);
if (flag>=0 && flag <=25)
{
flag=(flag-k2)*k1;
//printf("g%d",flag);
flag= flag%26;
decipher[i]=small[flag];
}
else
{
decipher[i]=data[i];
}
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|