#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep 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;
    }
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,540
    Rep Power
    2337
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    so are you saying this board is not as good as Cboard and you can't find an issue either?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep 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!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    185
    Rep Power
    82
    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];
    		}
    	}

IMN logo majestic logo threadwatch logo seochat tools logo