/* * crypto.c * (c)Steve Parker, 31st Oct 2003 * GPL v2 for now. * http://www.gnu.org/copyleft/gpl.html * * Aimed at challenges such as * http://www.thawte.com/cryptochallenge/lunchHour.html * Goals: * Identify letters by frequency analysis * Confirm by digrams * Confirm agian by Trigraphs * Resources: * http://library.thinkquest.org/28005/flashed/thelab/cryptograms/frequency.shtml */ /* Bmxbzs cfbs jo njoe uibu zpvs pxo sftpmvujpo up tvddffe jt npsf jnqpsubou uibo boz pof uijoh. Becomes: (ie, out-by-one) Always bear in mind that your own resolution to succeed is more important than any one thing. Abraham Lincoln */ #include #define MAXCRYPTO 999999 // Longest string to be translated // For now, assume only spaces and a-z (not even A-Z) // For testing purposes, I'm taking plain text input and hoping to re-create plain-text output. // That's the basic test for frequency analysis, after all! // With more advanced checks, such details may not be as necessary (eg, "th" go together, etc) //, '\'', ',', ',', '-','1','2','3','4','5','6','7','8','9','0'}; // http://library.thinkquest.org/28005/flashed/thelab/cryptograms/frequency.shtml //char english[]={ ' ', 'e', 't', 'n', 'r', 'i', 'o', 'a', 's', 'd', 'h', 'l', 'c', 'f', 'p', 'u', 'm', 'y', 'g', 'w', 'v', 'b', 'x', 'k', 'q', 'j', 'z' }; // http://paul.oniony.com/sections/information/frequencyanalysis.html //char english[]={' ', 'e', 't', 'a', 'o', 'n', 'i', 'r', 's', 'h', 'd', 'l', 'c', 'u', 'm', 'f', 'p', 'y', 'g', 'w', 'b', 'v', 'k', 'x', 'j', 'q', 'z' }; // From Act 1, Scene 1, "All's Well that ends well" (http://the-tech.mit.edu/Shakespeare/) // Simple version: //char english[]={ ' ', 'e', 't', 'i', 'a', 'o', 's', 'n', 'r', 'h', 'l', 'd', 'u', 'm', 'w', 'y', 'f', 'c', 'g', 'b', 'p', 'v', 'k', 'x', 'q', 'j', 'z' }; // Full version: //char english[] = { ' ', 'e', 't', 'i', 'o', 'a', 's', 'n', 'r', 'h', 'l', 'd', 'u', 'm', 'w', 'y', ',', 'f', 'c', 'g', '*', 'b', 'E', 'p', 'v', '.', 'k', 'A', 'L', 'T', '\'', ';', 'S', 'I', 'N', 'H', 'O', 'R', ':', 'U', 'P', 'W', 'M', 'B', '/', '?', 'F', '!', 'C', '-', 'x', 'q', 'j', 'Y', 'G', 'V', 'K', '[', ']', 'D', 'z' }; // From http://www.thingsmygirlfriendandihavearguedabout.com/ : char english[]={ ' ', 'e', 't', 'a', 'o', 'n', 'i', 's', 'r', 'h', 'l', 'd', 'u', 'g', 'm', 'c', 'y', 'w', 'f', '\'', 'p', ',', '.', 'b', 'v', 'I', 'k', '/', '-', 'M', 'T', '?', 'S', 'A', 'W', 'x', 'j', ':', 'O', 'B', '*', 'H', 'N', 'Y', ')', 'D', 'E', '!', 'G', '(', 'C', 'q', 'F', 'L', 'P', 'z', '"', 'R', 'K', 'V', ';', 'J', '1', '<', '>', 'U', '2', '0', '3', '8', '9', '[', ']', '5', '4', 'Q', '_', '7', '=', '+', 'Z', '~', '%', '6', '#', '&', '@', 'X' }; // From freq_analysis.c: //char english[]={ ' ', 'i', 't', 'r', 'f', ';', 'n', 'c', 'e', 'a', '(', ')', 's', 'o', '[', ']', '.', 'p', 'l', '=', 'h', '+', '*', '0', ',', 'u', '{', '}', '5', '/', 'g', '2', '"', 'b', 'd', '1', '<', 'j', 'm', 'q', 'w', '\'', 'v', '9', 'y', 'P', '%', 'A', 'C', 'O', 'T', '\\', '-', '3', 'I', 'L', 'M', 'R', 'S', 'X', 'Y', '_', 'k', ' ', '#', '&', ':', '>', '!', '4', '6', '7', '8', 'E', 'F', 'G', 'x' }; struct freq { int f; char c; char t; }; void bubblesort (struct freq a[], int n) { int i, j; struct freq tmp; for (j = 0; j < n - 1; j++) { for (i = 0; i < n - 1; i++) { if (a[i].f < a[i + 1].f) { tmp = a[i]; a[i] = a[i + 1]; a[i + 1] = tmp; } } } } void freq_analysis (char *p) { int i, j; int k; struct freq f[255]; char p2[MAXCRYPTO]; for (i = 0; i < 255; i++) { f[i].f = 0; f[i].c = '\0'; f[i].t = '\0'; } for (i = 0; i < strlen (p); i++) { if (p[i] > 31) { f[(int) p[i]].f++; f[(int) p[i]].c = p[i]; } } bubblesort (f, 256); j = 0; for (i = 0; i < 255; i++) { if (f[i].f > 0) { while (f[i].c < 0) f[i].c += 255; if (((int) f[i].c < 127) && ((int) f[i].c != 10)) { printf ("%4d : %c (%03d) = %c (%03d)\n", f[i].f, f[i].c, f[i].c, english[j], english[j]); for (k = 0; k < MAXCRYPTO; k++) { if (p[k] == f[i].c) { p2[k] = english[j]; } else { if (p[k] < 32) p2[k] = p[k]; } } j++; } } } printf ("\nTranslation:\n"); //printf("%s\n", p); //printf("Becomes:\n"); p2[strlen (p) - 2] = '\0'; printf ("%s\n", p2); } int main (int argc, char *argv[]) { // char *p="Js Jr qntrjamd un gzjk jm nzox xzzr...xgjkf sp rvbddfc jr qntrjamd pmmx jm pmf vbx."; char *p; char *s; FILE *in; p = (char *) malloc (MAXCRYPTO); s = (char *) malloc (255); in = fopen (argv[1], "rb"); while (fgets (s, 255, in)) { strcat (p, s); } fclose (in); //printf("Code: %s\n", p); freq_analysis (p); free (p); return 0; }