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

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39

    Can anyone see any errors in this cipher code?

    I have this program to turn in for an assignment, and need to know if it is indented properly and I also get some ascii symbols appear instead of the actual letters on the output side. Can anyone tell me if they see an issue, and if so what line?

    THANKS

    Code:
    /*Author: John Puskar Description: Project 4
      Date:  December 10, 2012  */
    
    
    #pragma   warning(  disable:4996) 
    #include<stdio.h>
    //for small letters only
    int FindGCD(int no1,int no2);
    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]);
    	}
    printf("please enter the text message to encrypt\n");
    scanf("%[^\n]s",data);
    fflush(stdin);
    //ask user to provide atleast 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 reenter the keys");
    	}
    	//ciphere 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++;
    
    
    	}
    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;
    printf("\nthe dencrypted 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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    You'll have to fix your indentation so we can read it better

    Make sure you choose "paste as plain text"
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    my problem is still not solved by correcting the indentation. I also do not see why paste as plain text matters either. I just don't understand why I am getting ASCII code 203 symbol in my output! This is what I need help with.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If you don't want to indent your code when you are coding, that is fine. However, you have come here for help. And the people who look at your project have trouble reading it when you don't indent your project.

    We are not being paid to help you, the least you can do is help us.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Ok is the indentation any better here?

    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;
    }

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What warning have you disabled?
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    I am using Visual Studio and this is the warning:

    Warning 1 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\users\john\desktop\c+projects\projects\#4\p04\p 04\p04.cpp 46 1 P04

  8. #8
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Can anyone see any errors in this cipher code?-cipher-jpg

    Above is the screen shot of the program. The output issue is always with the vowels it seems.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Can you, in plain language, explain the algorithm which you are using to cipher and decipher the string?
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    E(x) = Ax + B (mod N) <= ENCRYPTION FUNCTION
    D(x) = A-1(x – B) (mod N) <= DECRYPTION FUNCTION
    Note: A and N must be coprime. This means they share no prime factors.
    A-1 is the inverse of A (mod N). This means A * A-1 = 1 (mod N)

    Recall that A and B are mathematical constants used to determine the “key” used by the Affine Cipher. The two parties exchanging information would agree upon a key upfront so they know how to encrypt and decrypt the messages mathematically. N is the size of the alphabet we are using. In cryptography, we typically remove all spaces, punctuation and case information from the letters. This means we must transform input files so that they do not contains these invalid characters. In essence, all message will become a series of lowercase letters, which gives us 26 unique symbols we need to deal with. This gives us an alphabet size of N = 26.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    No one can find an issue here and point me in the right direction?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    97	  k1= InvEuclid(k1,26);
    (gdb) n
    99	  i=0;
    (gdb) print k1
    $9 = 11
    (gdb) s
    100	  for(i=0;i<count;i++)
    (gdb) 
    102	    flag= cipher[i]-'a';
    (gdb) 
    104	  if (flag>=0 && flag <=25)
    (gdb) print flag
    $10 = 2
    (gdb) s
    106	    flag=(flag-k2)*k1;
    (gdb) 
    108	    flag= flag%26;
    (gdb) print flag
    $11 = -22
    Beware of maths on negative numbers.
    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.

  13. #13
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Salem View Post
    Code:
    97      k1= InvEuclid(k1,26);
    (gdb) n
    99      i=0;
    (gdb) print k1
    $9 = 11
    (gdb) s
    100      for(i=0;i<count;i++)
    (gdb) 
    102        flag= cipher[i]-'a';
    (gdb) 
    104      if (flag>=0 && flag <=25)
    (gdb) print flag
    $10 = 2
    (gdb) s
    106        flag=(flag-k2)*k1;
    (gdb) 
    108        flag= flag%26;
    (gdb) print flag
    $11 = -22
    Beware of maths on negative numbers.
    THANKS for your response. However math I am not the best student. Can you at least tell me what lines are the issue?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean you can't tell from the above debug which line of code resulted in a negative number?
    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.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 12-05-2012 at 05:31 PM.

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