Thread: Can anyone see any errors in this cipher code?

  1. #16
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Salem View Post
    You mean you can't tell from the above debug which line of code resulted in a negative number?

    SORRY, but I have only been programming for 3 months. Before that, I knew absolutely nothing!!!

    I AM HERE TO LEARN!

  2. #17
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Adak View Post
    Two functions to check, to narrow it down: 1) The encrypt function (or block of code) has a bug in it, or 2) The decrypt function (or block of code) has a bug in it. To determine which is faulty, start with a very simple 1 vowel, and 1 consonant word, and see if it fails - you need to find a very simple test case that will show the fault.

    Now encrypt that very short word by hand - yeah - paper and pen. Write out each step. See if the program matches your hand encryption - if not, then the encrypt function has a bug in it (and keep in mind, there could be more than one bug at work here).

    If it shows no errors, then the decrypt function (or block of code) must have the error.

    Now you have the step by step encrypt and can make the step by step decrypt data you need, to see which step in the faulty function, is the one that's goofed.

    Add in print statements and getchar(); so you can follow what the variable values are, if you aren't using a de-bugger so you can step through your code.

    I am not familiar with this cipher, and would need to follow the above procedure to debug the program. You can do this, and it will help build up your own debugging skills.

    When you're writing functions, it's always good to test each one, as your finish them. That way, if there's an error, later on, 90% of the time, it will be in the last (untested) function you are coding up.
    THANKS for your help! I know what debugging is, but have never used it or seen it in action. I will try your suggestion since it makes sense. Be patient with me please!

  3. #18
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    So if I look at this correctly, my issue is in the decrypt function with: flag= flag%26;
    Do you agree?


  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by dodgetech View Post
    So if I look at this correctly, my issue is in the decrypt function with: flag= flag%26;
    Do you agree?

    No problem, dodgetech - and yes, if you are assigning a letter or a number, based on a negative index of the character set (aka ascii values) - that would be a big red flag, since there are none.

    However, I'd still like to know that the encrypt function was working OK, at least in a simple preliminary test, with vowels and consonants.

    When you're working with arrays, you always need to test or watch carefully, the ENDS of them (high and low), to see that they don't run over or under, their legit range. Vowels to test would be a and e, and u and y (if you have y as a vowel - some do, some don't). Consonants to test would be b and c and x and z.

    We know you're a beginner - but we're not going to let you get away with anything anyway! <smile>

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > SORRY, but I have only been programming for 3 months. Before that, I knew absolutely nothing!!!
    This is a maths problem, not a programming problem.

    The first thing is to understand in mathematical terms is how to turn your expression into something that always yields a positive number.
    When you figure out say that adding 26 to flag is the answer, THEN it becomes a programming problem.

    You seemed to know enough maths to do the rest of the assignment, unless this is just another "I found some code and it doesn't work" jobs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Feb 2012
    Posts
    9
    what you really want to get . //?C/C++ Language Programming

  7. #22
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    well here is what I came up with if anyone is interested:

    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 in lower case alphabet ONLY 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("%d",flag);
    			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("%d",flag);
    			decipher[i]=small[flag];
    		}
    		else
    		{
    			decipher[i]=data[i];
    		}
    	}
    	j=0;
    	// show user the de-encrypted string
    	printf("\nthe de-encrypted message 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;
    }

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please answer the question, though. Did you test both the functions (encrypt and decrypt), to see whether they both worked properly, on a variety of simple input (and output), that you know the answer to?

    Without that test, there's no way to know if your program is working correctly, or not. I certainly can't just look at your code, and expect my muse to come along with the answer, and slap me upside the back of my head with it.

    Maybe someone here can just look at it and tell, but really, that's assuming a great deal more than you should, isn't it? You should be the primary tester on your program.

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    One thing that I noticed -
    Code:
    fflush(stdin);
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    FAQ > Flush the input buffer - Cprogramming.com

    Hope this helps
    Fact - Beethoven wrote his first symphony in C

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > well here is what I came up with if anyone is interested:
    All you've done is broken it, and added some prints.

    Replacing
    flag= flag%26;
    with
    printf("%d",flag);

    So as a result, you now have massively overflowed arrays.
    Code:
    $ ./a.out 
    please enter the text message in lower case alphabet ONLY to encrypt
    hello world
    
    please enter the first key 1<k1<26 such that gcd of (k2,26)=1
    19
    
    please enter the second key 1<k2<26
     4
    EncPos=137
    EncPos=80
    EncPos=213
    EncPos=213
    EncPos=270
    EncPos=422
    EncPos=270
    EncPos=327
    EncPos=213
    EncPos=61
    
    the encrypted string is
      
    
    the de-encrypted message is: 
     hello world
    Does
    cipher[0]=small[137];
    cipher[1]=small[80];
    cipher[2]=small[213];
    look OK to you?

    Yes, it comes up with the "right" decoded answer, but it trashed a lot of memory belonging to someone else doing it. Sooner or later, this program would crash in a random way.


    I'm bored with trying, so here's the answer.
    But only because I doubt you wrote any of that code to begin with. I can't see how you came up with FindGCD() and InvEuclid(), and not be able to figure out this.
    Code:
        flag=(flag-k2)*k1;
        //printf("g%d",flag);
        flag= flag%26;
        if ( flag < 0 ) flag = flag + 26;
        decipher[i]=small[flag];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #26
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Salem View Post
    > well here is what I came up with if anyone is interested:
    All you've done is broken it, and added some prints.

    Replacing
    flag= flag%26;
    with
    printf("%d",flag);

    So as a result, you now have massively overflowed arrays.
    Code:
    $ ./a.out 
    please enter the text message in lower case alphabet ONLY to encrypt
    hello world
    
    please enter the first key 1<k1<26 such that gcd of (k2,26)=1
    19
    
    please enter the second key 1<k2<26
     4
    EncPos=137
    EncPos=80
    EncPos=213
    EncPos=213
    EncPos=270
    EncPos=422
    EncPos=270
    EncPos=327
    EncPos=213
    EncPos=61
    
    the encrypted string is
      
    
    the de-encrypted message is: 
     hello world
    Does
    cipher[0]=small[137];
    cipher[1]=small[80];
    cipher[2]=small[213];
    look OK to you?

    Yes, it comes up with the "right" decoded answer, but it trashed a lot of memory belonging to someone else doing it. Sooner or later, this program would crash in a random way.


    I'm bored with trying, so here's the answer.
    But only because I doubt you wrote any of that code to begin with. I can't see how you came up with FindGCD() and InvEuclid(), and not be able to figure out this.
    Code:
        flag=(flag-k2)*k1;
        //printf("g%d",flag);
        flag= flag%26;
        if ( flag < 0 ) flag = flag + 26;
        decipher[i]=small[flag];
    Your correct in that I didnt write all the code. The teacher gives us a shell and we need to fill in the missing parts or find what is missing.
    I appreciate your time. Remember this is a learning process for me. Yes I also understand that the array is overfilled.
    THANKS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with errors in the code
    By lukis123 in forum C Programming
    Replies: 5
    Last Post: 11-03-2011, 11:00 PM
  2. Help with errors on my code!
    By ashleyd in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2011, 01:35 PM
  3. Errors in code
    By itzme in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2005, 05:11 PM
  4. Errors (not in code)
    By Da-Spit in forum C++ Programming
    Replies: 8
    Last Post: 05-18-2002, 12:31 AM