how to create a zig zag encryption

how to create a zig zag encryption

Page 1 of 1

4 Replies - 4726 Views - Last Post: 12 May 2007 - 03:25 AM Rate Topic: -----

#1 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-May 07

how to create a zig zag encryption

Posted 11 May 2007 - 04:39 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: how to create a zig zag encryption

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: how to create a zig zag encryption

Posted 11 May 2007 - 04:53 AM

Hi, first you should show some effort in code so that anyone would help.
Was This Post Helpful? 0
  • +
  • -

#3 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-May 07

Re: how to create a zig zag encryption

Posted 11 May 2007 - 05:20 AM

here is the code i wrote already

/*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;
}



Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: how to create a zig zag encryption

Posted 11 May 2007 - 05:31 AM

View Postthe juice, on 11 May, 2007 - 05:09 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#5 the juice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-May 07

Re: how to create a zig zag encryption

Posted 12 May 2007 - 03:25 AM

cheers, ill try that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1