C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 4th, 2012, 11:11 PM
dodgetech dodgetech is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 dodgetech User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #2  
Old December 5th, 2012, 05:41 AM
ptr2void ptr2void is online now
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 43 m 29 sec
Reputation Power: 2194
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Reply With Quote
  #3  
Old December 5th, 2012, 01:53 PM
dodgetech dodgetech is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 dodgetech User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old December 5th, 2012, 02:38 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,128 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 18 h 48 m 17 sec
Reputation Power: 1949
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.

Reply With Quote
  #5  
Old December 5th, 2012, 02:43 PM
dodgetech dodgetech is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 dodgetech User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #6  
Old December 5th, 2012, 05:35 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
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];
		}
	}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Can anyone see any errors in this cipher code?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap