Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 27th, 2005, 4:30 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
"grid" encryptor

I don't know if any of you have read Digital fortress by Dan Brown, but he describes an ancient method of encrypting messages by using a square.

4 letters: test

they would be displayed like this:

T E
S T

and the encrypted message would be this: TSET.

I wanted to create a tool that would encrypt and decrypt these messages for me.

This is what I have so far, and it works. The decrypter will come later, this is simply the basis for the program.

note:the code only works if you enter in 4 letters or less. It will be updated however to accept up to 100 characters.



I'm sorry, I don't like comments. I'm too lazy :p ( I know, I know...bad habit).

Any questions or suggestions would be welcome.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
	int length, x, difference;
	char input[144];
	
	
	printf("Enter a phrase to be encrypted:");
	putchar('\n');
	gets(input);
	length = strlen(input);
	if(length <=4)
	{
		if(length > 0 && length < 4)
		{
			difference = 4 - length;
			for(x=0;x<difference;x++)
			{
				strcat(input,"*");
			}
			length = strlen(input);
		}
		for(x=0;x<length;x++)
		{
			if(!isalnum(input[x]))
			{
				input[x] = '*';
			}	
		}
		printf("\nHere is you encrypted text: %c%c%c%c", input[0],input[2],input[1],input[3]);
	}
	return(0);
}

Last edited by Jessehk; May 27th, 2005 at 4:36 PM.
Jessehk is offline   Reply With Quote
Old May 27th, 2005, 4:43 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I learnt about those a while back from some book or other - pretty simple, but very interesting. Another way is to use a keyword to encrypt it:
   1 2 3 4 5
1  S N A K E
2  B C D F G
3  H I J L M
4  O P Q R T
5  U V W Y Z
(we use consider 'X' to be the same as 'Z'). You then take the numbers (L = 34, for example) and output them. When you've finished this project, perhaps that'll interest you further.

On a side note, I really must get around to reading The Da Vinci Code - reading Atlas Shrugged at the mo.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 27th, 2005, 5:36 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
EDIT: Ignore the following

Also:

Is there a way to copy the output?

I want to be able copy and paste (possibly to an email address, or word processor, etc). Is there anyway to do this in DOS?

Last edited by Jessehk; May 27th, 2005 at 5:46 PM.
Jessehk is offline   Reply With Quote
Old May 27th, 2005, 6:39 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You'd have to #include <windows.h>, but I'm sure it's possible. Google is your friend.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 27th, 2005, 8:17 PM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
EDIT: nevermind. It was a typo. I had type input[15] instead of input[3].

Last edited by Jessehk; May 27th, 2005 at 8:21 PM.
Jessehk is offline   Reply With Quote
Old May 27th, 2005, 9:31 PM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
version 0.8

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define FALSE 0
#define TRUE !FALSE


int main()
{
	int length, x, difference, choice, done, choice2,y;
	char input[144];
	
	done = FALSE;
	while(!done)
	{
		printf("\n\n  G R I D   E N C R Y P T\n\n\n");
		puts("-----------------------------------");
		printf("\n\n1 = encrypt");
		printf("\n2 = decrypt");
		printf("\n3 = quit");
		printf("\n\n\nEnter choice: ");
		scanf("%d", &choice);
		switch(choice)
		{
			case 1:
				break;
			case 2:
				break;
			default:
				break;
		}
		fflush(stdin);
		if(choice==1) /* encrypt */
		{
			printf("\n E N C R Y P T O R\n\n\n");
			printf("\nEnter a phrase to be encrypted:");
			putchar('\n');
			gets(input);
			length = strlen(input);
			if(length <=4)
			{
				if(length > 0 && length < 4)
				{
					difference = 4 - length;
					for(x=0;x<difference;x++)
					{
						strcat(input,"*");
					}
					length = strlen(input);
				}
				for(x=0;x<length;x++)
				{
					if(!isalnum(input[x]) && !ispunct(input[x]))
					{
						input[x] = '*';
					}	
				}
				
				printf("\nHere is you encrypted text: %c%c%c%c", input[0],input[2],
																 input[1],input[3]);
				/*
					 0,1,
					 2,3,
				*/
			}
			else if(length > 4 && length <=9)
			{
				if(length > 4 && length < 9)
				{
					difference = 9 - length;
					for(x=0;x<difference;x++)
					{
						strcat(input,"*");
					}
					length = strlen(input);
				}
				for(x=0;x<length;x++)
				{
					if(!isalnum(input[x]) && !ispunct(input[x]))
					{
						input[x] = '*';
					}	
				}
				printf("\nHere is your encrypted text: %c%c%c%c%c%c%c%c%c", input[0],input[3],input[6],
																		   input[1],input[4],input[7],
																		   input[2],input[5],input[8]);
				 /* 
					0,1,2,
					3,4,5,
					6,7,8 , 
				  */
			}
			
			else if(length > 9 && length <=16)  /* if(length > 9 && length <=16) */
			{
				if(length > 9 && length < 16)
				{
					difference = 16 - length;
					for(x=0;x<difference;x++)
					{
						strcat(input,"*");
					}
					length = strlen(input);
				}
				for(x=0;x<length;x++)
				{
					if(!isalnum(input[x]) && !ispunct(input[x]))
					{
						input[x] = '*';
					}	
				}
				printf("\nHere is your encrypted text: %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", input[0],input[4],input[8],input[12],
																						  input[1],input[5],input[9],input[13],
																						  input[2],input[6],input[10],input[14],
																						  input[3],input[7],input[11],input[15]);
				/*
					0,1,2,3,
					4,5,6,7,
					8,9,10,11,
					12,13,14,15
				*/
			}
		}
		
		
		else if(choice == 2)
		{
			printf("\n D E C R Y P T O R\n");
			printf("\nEnter a phrase to be decrypted: ");
			gets(input);
			length = strlen(input);
			if(length == 4)
			{
				for(x=0;x<length;x++)
				{
					if(!isalnum(input[x]) && !ispunct(input[x]))
					{
						input[x] = ' ';
					}	
				}
				for(y=0;y<length;y++)
				{
					if(input[y]=='*')
					{
						input[y] = ' ';
					}
				}
				printf("\nHere is your decrypted text: %c%c%c%c", input[0],input[2],
																  input[1],input[3]);
				/*
					 01
					 23
				*/
			}
			
			else if(length == 9) /* if(length > 4 && length <=9) */
			{
				for(x=0;x<length;x++)
				{
					if(!isalnum(input[x]) && !ispunct(input[x]))
					{
						input[x] = ' ';
					}	
				}
				for(y=0;y<length;y++)
				{
					if(input[y]=='*')
					{
						input[y] = ' ';
					}
				}
				
				printf("\nHere is you decrypted text: %c%c%c%c%c%c%c%c%c", input[0],input[3],input[6],
																		   input[1],input[4],input[7],
																		   input[2],input[5],input[8]);
				 /* 
					   012
					   345
					   678  
				  */
			}
			
			else if(length == 16)
			{
				for(x=0;x<length;x++)
				{
					if(!isalnum(input[x]) && !ispunct(input[x]))
					{
						input[x] = ' ';
					}	
				}
				for(y=0;y<length;y++)
				{
					if(input[y]=='*')
					{
						input[y] = ' ';
					}
				}
				
				printf("\nHere is your decrypted text: %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", input[0],input[4],input[8],input[12],
																						  input[1],input[5],input[9],input[13],
																						  input[2],input[6],input[10],input[14],
																						  input[3],input[7],input[11],input[15]);
				/*
					0,1,2,3,
					4,5,6,7,
					8,9,10,11,
					12,13,14,15
				*/
			}
		}
		
		puts("\n\n\n\n\n--------------------");
		printf("\n\n Quit?");
		puts("\n\n1 = no");
		puts("2 = yes");
		printf("\n\nEnter choice: ");
		scanf("%d", &choice2);
		switch(choice2)
		{
			case 1:
				break;
			case 2:
				done = TRUE;
				break;
			default:
				printf("Choose either 1 or 2!");
				done = TRUE;
				break;
		}
	}
	return(0);
}

I realise that it is quite large. Are there any shortcuts that I have not taken?
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:30 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC