Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Existing Project Development (http://www.programmingforums.org/forum51.html)
-   -   "grid" encryptor (http://www.programmingforums.org/showthread.php?t=4138)

Jessehk May 27th, 2005 4:30 PM

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


Ooble May 27th, 2005 4:43 PM

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.

Jessehk May 27th, 2005 5:36 PM

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?

Ooble May 27th, 2005 6:39 PM

You'd have to #include <windows.h>, but I'm sure it's possible. Google is your friend.

Jessehk May 27th, 2005 8:17 PM

EDIT: nevermind. It was a typo. I had type input[15] instead of input[3].

Jessehk May 27th, 2005 9:31 PM

version 0.8 :D

:


#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?


All times are GMT -5. The time now is 6:21 PM.

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