Friday, July 31, 2009

How do you crack the Rail Fence Cypher? (While programming in C?)?

OK, to fill you in on what the Rail Fence Cypher does, it goes like this





Take the plain text message





ABCDEFGHIJKL





Then put it in to a table, thus,





A D G J


B E H J


C F I L





Then read the rows to get the encrypted message





A D G J B E H J C F I L





I'm programming in C, so any C solutions, hints, tips, would be brilliant





Mine uses a fixed three rows, so you could have





A B C D E F G H I J L K M N O P Q R S T U V W X Y Z





A D G J M P S V Y


B E H K N Q T W Z


C F I L O R U X





Final Encrypted message:





A D G J M P S V Y B E H K N Q T W Z C F I L O R U X





And anything in between.





The alphabet is used for example purposes.





More letters can be used.





My arrays in C are set to 3 rows by 25 columns for encrypting the message, and 75 elements for getting the plain text message.





My code is available at http://docs.google.com/Doc?id=ddzf6sz8_9...

How do you crack the Rail Fence Cypher? (While programming in C?)?
Hi


I have attach a snippet of code that may help.


It is not concise but may help with the logic.


A couple of tips:


Try not to use 'goto'.


In the 'menu' procedure have a look at using 'switch' and 'case' rather than 'if' statements


Compact the code a bit, there is a lot of empty space.


Do you need to find out what OS the user is using I would suggest that they would know that otherwise they would not be able to compile it in the first place!


Enjoy!!!!


Graham





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;ctype.h%26gt;





char BinputMSG[75];


char Message2encode[3][25];


char DinputMSG[75];





main()


{


int C;


int R;


static int x = 0;


int NofCols, strlg;


static div_t q;


char s[1];


printf("Input a string. Up to 75 characters...");


scanf("%s",BinputMSG);


printf("Input was %s\n",BinputMSG);


strlg = strlen(BinputMSG);


q = div(strlg,3);


NofCols = q.quot; // Calculate number of columns


for(C = 0 ; C %26lt;= NofCols ; C++)


{


for ( R = 0 ; R %26lt;= 2 ; R++)


{


Message2encode[R][C] = BinputMSG[x];


x++;


}





}


x = 0;


for ( R = 0 ; R %26lt;= 2 ; R++)


{


for ( C = 0 ; C %26lt;= NofCols ; C++)


{


s[0] = Message2encode[R][C];


if (s[0] != 0) // Allow for blanks in the array


{


DinputMSG[x] = Message2encode[R][C];


x++;


}


}


}


printf("Encoded string is %s\n",DinputMSG);


}


No comments:

Post a Comment