Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,133 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,806 people online right now. Registration is fast and FREE... Join Now!




how to create a zig zag encryption

 
Reply to this topicStart new topic

how to create a zig zag encryption, how to create a zig zag encryption

the juice
11 May, 2007 - 03:39 AM
Post #1

New D.I.C Head
*

Joined: 11 May, 2007
Posts: 11


My Contributions
i am new to c++ and have been asked to create an encryption programme using the zig zag cipher. this will be saved to a file, and when it is opened the file will be decrypted.
a user will type in a message. The message is then read off in rows. For example, i am using 3 tiers and a message of 'WE ARE DISCOVERED. FLEE AT ONCE', the cipherer writes out:

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
Then reads off:

WECRL TEERD SOEEF EAOCA IVDEN

if someone could help that would be great

cheers
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: How To Create A Zig Zag Encryption
11 May, 2007 - 03:53 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, first you should show some effort in code so that anyone would help.
User is offlineProfile CardPM
+Quote Post

the juice
RE: How To Create A Zig Zag Encryption
11 May, 2007 - 04:20 AM
Post #3

New D.I.C Head
*

Joined: 11 May, 2007
Posts: 11


My Contributions
here is the code i wrote already

CODE

/*Header Files*/
#include <stdio.h>
#include <conio.h>
#include "bcio2.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*Function Prototype*/
void sleep(void);
void sleep( int nbr_seconds );
void header(void);
void filewrite(void);    
void menu(void);
int valid(char s[]);
void fileread(void);


/*Validatoion Error Message*/
char msg0[40]="\t\t\tTry Again!\a";
char msg1[60]="YOU HAVE ENTERED MORE THAN ONE CHARACTER!\a";
char msg2[40]="YOU HAVE ENTERED BETWEEN 5 AND 9!\a";
char msg3[60]="YOU HAVE FORGOTTEN TO ENTER A VALID OPTION!!\a";



/*Variables & Constants*/
char input[8];
int multi;
int flag=0;
char x;
int conv;

/*START OF MAIN FUNCTION*/
void main(void)
{
    
    header();
    sleep();
    menu();        
            
}        
    

/*HEADER SCREEN TO DISPLAY  GRAPHICS*/

void header(void)

{    int x;//inner loop counter//
    int y;//outer loop counter//
      char d = ' ';
      settextcolour(magenta, BRIGHT);
/*print top line and ends*/
        printf("\n\t%c",201);
        for(x=1;x<15;x++)
            printf("%c",205);
        printf("%c",203);
        for(x=1;x<15;x++)
                printf("%c",205);
        printf("%c\n",187);
/*print bottom right and left sides*/    

        for(y=1;y<8;y++){
                printf("\t%c",186);
                for(x=1;x<15;x++)
                        printf("%c",d);
                printf("%c",186);
                for(x=1;x<15;x++)
                        printf("%c",d);
                printf("%c\n",186);
        }

/*print middle line and edges*/
        printf("\t%c",200);
        for(x=1;x<15;x++)
                printf("%c",205);
        printf("%c",202);
        for(x=1;x<15;x++)
                printf("%c",205);
        printf("%c",206);
            for(x=1;x<30;x++)
                printf("%c",205);
        printf("%c\n",187);
/*print bottom right and left sides*/    

        for(y=1;y<8;y++){
                printf("\t\t\t\t      %c",186);
                for(x=1;x<30;x++)
                        printf("%c",d);
                printf("%c\n",186);
        }
        
/*print bottom line and edges*/
        printf("\t\t\t\t      %c",200);
        for(x=1;x<30;x++)
                printf("%c",205);
        printf("%c\n",188);
}



/*SPLASH SCREEN*/
void sleep(void)
{
    int ctr;
    int wait =1;// assigned for three seconds
    int x;//inner loop counter//
    int y;//outer loop counter//
      char d = ' ';
    clrscr();

    for (ctr=1; ctr <= wait; ctr++){
    settextcolour(magenta, BRIGHT);
    printf("\n\n\t\t\tWELCOME TO THE CANDID ECHO PRESS\n");
    puts("\t\t\tPLEASE WAIT WHILE THE PROGRAMME LOADS\n");

    header();
    printf("\n\n\t\t\tPRODUCED BY: Richard Lasowski");
       sleep( (int) 2 );  /* pause for 2 seconds */
    }
  
}

void sleep( int nbr_seconds )
{
    clock_t goal;

    goal = ( nbr_seconds * CLOCKS_PER_SEC ) + clock();

    while( goal > clock() )
    {
      ; /* loop */
    }
}




/*TO DISPLAY MENU SCREEN*/
void menu(void)
{
    
    clrscr();
    header();
    gotoxy(25,30);
    settextcolour(magenta, BRIGHT);
    puts("* CREATE A FILE .......a");
    gotoxy(25,32);
    puts("* OPEN A FILE .........b");
    gotoxy(25,34);
    puts("* EXIT THE PROGRAMME...c");

    gotoxy(25,26);
    puts("CHOOSE A VALID OPTION NUMBER:");
    gotoxy(23,27);


        gotoxy(30,36);
    puts("CHOOSE AN OPTION:");

    gotoxy(17,20);

    puts("WECLOME TO MAIN MENU SCREEN!  FILE PROCCESSING:");

    
    

    gotoxy(47,44);
    gets(input);

    flag=valid(input);
    if(!flag)
    {
        gotoxy(32,46);
        flushall();
    }
    
}


/*FILE WRITNG*/

void filewrite(void)
{

    flushall();
    clrscr();
    header();

FILE *fp;
char stuff1[333];
char stuff2[333];
char stuff3[333];
char filename[10];


     gotoxy(24,20);
    puts("FILE PROCCESSING: FILE WRITING\n\n");

    puts("Enter up to 333 characters (no spaces):");
    scanf("%s",&stuff1);
    strcpy(stuff1,stuff2);
    strcpy(stuff1,stuff3);


    puts("\n\nEnter a file name (8 characters max):");
    scanf("%s",&filename);

    fp=fopen(filename,"w");
    fprintf(fp,"%s",stuff1);
        fclose(fp);
        flushall();
        header();
        menu();

}

/*FILE READING*/
void fileread(void)
{

    flushall();
    clrscr();
    header();

    char create;
    char filename[8];
    FILE *file1;

    gotoxy(24,20);
    puts("FILE PROCCESSING: FILE READING");


    gotoxy(25,25);
    puts ("THIS PROGRAM READS (OPENS) FILE");

    gotoxy(13,28);
    fputs("Enter file name: ",stdout);
    gets(filename);
    gotoxy(13,29);
    file1 = fopen(filename ,"r");
    if (file1 == NULL)
    {
        gotoxy(13,32);
        puts("FILE MISSING");
        gotoxy(23,42);
        puts("Press any key to continue");
        gotoxy(49,42);
        getch();
        flushall();
    


    }
    gotoxy(13,31);
    printf("Reading charcters from the file: %s\n\n",filename);
        gotoxy(13,33);
    while((create=fgetc(file1)) !=EOF)
    {
        //delay(100);
        putchar(create);
    }
    fclose(file1);
    gotoxy(23,42);
    puts("Press any key to continue");
    gotoxy(49,42);
    getch();

        flushall();

}


/*VALIDATE INPUT (VALID)*/
int valid(char input[])
{
    int count=0;
    int test=0;
    flag++;
    conv = atof(input);




/*test option 'a' file write*/
    {
        int x;
          for(x=0;x<strlen(input);x++)
           {
          if(input[x]=='a')
                filewrite();
        
    }
}



/*test option 'b' file read*/
    {
        int x;
          for(x=0;x<strlen(input);x++)
           {
          if(input[x]=='b')
                fileread();
        }
    }



/* test option 'c' exit*/
    {
        int x;
          for(x=0;x<strlen(input);x++)
           {
          if(input[x]=='c')
                exit(0);
              
    
        }
    }


/* test for range 1 to 4*/
    if(conv<1 || conv>4)
    {
        
    flushall();
    clrscr();
    header();
    
    gotoxy(27,20);
    puts(" MENU (OPTION) VALIDATION ");

    
    gotoxy(15,22);
    puts (" PLEASE CHOOSE THE VALID OPTIONS AVAILABLE!");
    gotoxy(15,24);
        puts(msg0);

            flushall();
    }





/*test all input in range 0 to 4*/
    
if(count==1)
    {
        int x;
        for(x=0;x<strlen(input);x++)
        {
            //if((input[x]>='1'||input[x]=='a')&&(input[x]<='4'||input[x]=='a'));
            if(input[x]>='1' && input[x]<='4');

    }
}
else
{
    settextcolour(magenta, BRIGHT);
    gotoxy(15,28);
    puts(msg3);
    test++;
    flushall();    

}






/*if input errors set flags*/
  if(test>0)
  {
    flag--;
    

        gotoxy(23,30);
        puts(" - Press any key to continue -");
        gotoxy(54,30);
        getch();
        flushall();
        header();
        menu();
  }
else
;
return flag;
}


User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: How To Create A Zig Zag Encryption
11 May, 2007 - 04:31 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(the juice @ 11 May, 2007 - 05:09 PM) *

i am new to c++ and have been asked to create an encryption programme using the zig zag cipher. this will be saved to a file, and when it is opened the file will be decrypted.
a user will type in a message. The message is then read off in rows. For example, i am using 3 tiers and a message of 'WE ARE DISCOVERED. FLEE AT ONCE', the cipherer writes out:

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
Then reads off:

WECRL TEERD SOEEF EAOCA IVDEN

if someone could help that would be great

cheers


Whatever you are trying to do here is also called as Rail Fence Cipher.

Just have a search on the internet by this name and you will get lot of information.

One way of doing this is to have a 2D array and toggle it's rows from 0 to 2 and then 2 to 0 one after the other until you finish the string. [you can have increment and decrement flags for that.]. And then just read it sequentially.

I hope this will help you.

User is offlineProfile CardPM
+Quote Post

the juice
RE: How To Create A Zig Zag Encryption
12 May, 2007 - 02:25 AM
Post #5

New D.I.C Head
*

Joined: 11 May, 2007
Posts: 11


My Contributions
cheers, ill try that
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:28PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month