#include #include /* Written by Nick Pelling, 21st-23rd May 2008. */ unsigned char raw_cipher[14][14] = { { 75, 62, 82, 85, 91, 62, 91, 64, 81, 64, 91, 74, 85, 84 }, { 64, 74, 74, 82, 84, 83, 81, 63, 81, 81, 74, 74, 82, 62 }, { 64, 75, 83, 82, 84, 91, 75, 74, 65, 83, 75, 75, 75, 93 }, { 63, 65, 65, 81, 63, 81, 75, 85, 75, 75, 64, 62, 82, 92 }, { 85, 74, 63, 82, 75, 74, 83, 81, 65, 81, 84, 85, 64, 85 }, { 64, 85, 85, 63, 82, 72, 62, 83, 62, 81, 81, 72, 81, 64 }, { 63, 75, 82, 81, 64, 83, 63, 82, 85, 81, 63, 63, 63,104 }, { 74, 81, 91, 91, 84, 63, 85, 84, 65, 64, 85, 65, 62, 94 }, { 62, 62, 85, 91, 85, 91, 74, 91, 72, 75, 64, 65, 75, 71 }, { 65, 83, 62, 64, 74, 81, 82, 84, 62, 82, 64, 91, 81, 93 }, { 65, 62, 64, 84, 84, 91, 83, 85, 74, 91, 81, 65, 72, 74 }, { 83, 83, 85, 82, 83, 64, 62, 72, 62, 65, 62, 83, 75, 92 }, { 72, 63, 82, 82, 72, 72, 83, 82, 85, 84, 75, 82, 81, 83 }, { 72, 84, 62, 82, 83, 75, 81, 64, 75, 74, 85, 81, 62, 92 } }; unsigned char keyphrase[5][5] = { { 'A', 'B', 'C', 'D', 'E' }, { 'F', 'G', 'H', 'I', 'K' }, { 'L', 'M', 'N', 'O', 'P' }, { 'Q', 'R', 'S', 'T', 'U' }, { 'V', 'W', 'X', 'Y', 'Z' } }; unsigned char remapped_cipher[14][14]; void print_diagonal(int x, int y, int xstep, int ystep, int numsteps, int *printcounterptr, int countermax, int *lastchar, int *count, int *doubles, int *triples) { int c = printcounterptr[0]; for (; numsteps; numsteps--,x+=xstep,y+=ystep) { if ((x>=0) && (y>=0) && (x<14) && (y<14)) { int v = remapped_cipher[y][x]; printf("%c", (unsigned char) v); if (v != lastchar[0]) { lastchar[0] = v; count[0] = 1; } else { ++(count[0]); if (count[0] >= 2) doubles[0]++; if (count[0] >= 3) triples[0]++; } if (++c >= countermax) { printf("\n"); c = 0; } } } printcounterptr[0] = c; } void print_diagonals(int xx, int yy, int rowxstep, int rowystep, int columnxstep, int columnystep, int numrows, int numcolumns, int linemax) { int order; for (order=0; order<4; order++) { int row, x, y, printcounter, lastchar, count, doubles, triples; switch (order) { case 0: printf("Forward order...\n"); break; case 1: printf("Reverse order...\n"); break; case 2: printf("Simple boustrophedon (forward then reverse)...\n"); break; case 3: printf("Reverse boustrophedon (reverse then forward)...\n"); break; } lastchar = -1; count = 1; doubles = 0; triples = 0; for (row=0,x=xx,y=yy,printcounter=0; row number of doubles = %d, number of triples = %d\n", doubles, triples); } } int main(char **argv, int argc) { int i, j; for (i=0; i<14; i++) { for (j=0; j<14; j++) { int c = raw_cipher[i][j]; int x = (c % 10) - 1; int y = (c / 10) - 6; int r = keyphrase[y][x]; remapped_cipher[i][j] = (unsigned char) r; } } for (i=0; i<14; i++) { for (j=0; j<14; j++) { printf("%c ", remapped_cipher[i][j]); } printf("\n"); } printf("*** Top left corner ***\n"); print_diagonals( 0,0, 0,1, 1,-1, 40,40, 14*4); printf("*** Top right corner ***\n"); print_diagonals( 13,0, -1,0, 1, 1, 40,40, 14*4); printf("*** Bottom right corner ***\n"); print_diagonals(13,13, 0,-1, -1, 1, 40,40, 14*4); printf("*** Bottom left corner ***\n"); print_diagonals( 0,13, 1,0, -1,-1, 40,40, 14*4); return 0; }