![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
"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. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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 On a side note, I really must get around to reading The Da Vinci Code - reading Atlas Shrugged at the mo. |
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You'd have to #include <windows.h>, but I'm sure it's possible. Google is your friend.
|
|
|
|
|
|
#5 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|