// Takes in a file, and pointer to a key and returns the line // which is encrypted on that file, and saves the key used to // encrypt that line to the pointer it was passed. void findNeedle(FILE * haystack, int * needleLine, char * key){ char line[MAX_LINE]; memset(line, 0, MAX_LINE); int lineNum = 1; double lowestScore = INT_MAX; char * encodedBuffer; //Find the bestScoring phrase while(fgets(line, MAX_LINE, haystack ) != NULL ){ line[strcspn(line, "\n")] = 0; int len; char * buffer = hexStrToBytes(line, &len); char possibleKey = singleByteDecrypt(buffer, len); if (possibleKey > -1){ double score = bufferScorer(buffer, len); if (score < lowestScore){ lowestScore = score; * needleLine = lineNum; * key = possibleKey; encodedBuffer = buffer; printf("Found message on line: %d, key was: %x, scored: %f\n", lineNum, possibleKey, score); printf("Message might be:"); printByteBuf(singleByteXOR(encodedBuffer, possibleKey, len), len); } } lineNum++; free(buffer); } }
int main(int argc, char *argv[]) { int bytesLen = 0; char *bytes = hexStrToBytes(sInputStr, &bytesLen); if (!bytes) { printf("Failure! Couldn't convert hex to bytes.\n"); return 1; } char *base64Str = malloc(Base64encode_len(bytesLen)); if (!base64Str) { printf("Failure! Couldn't alloc buffer for base64 string.\n"); return 1; } Base64encode(base64Str, bytes, bytesLen); if (strcmp(base64Str, sOutputStr) == 0) { printf("Success!\n"); } else { printf("Failure!\n"); } free(base64Str); return 0; }
int main(int argc, char *argv[]) { int bytesLen1 = 0; char *bytes1 = hexStrToBytes(sInputStr1, &bytesLen1); if (!bytes1) { printf("Failure! Couldn't convert hex to bytes for first input.\n"); return 1; } int bytesLen2 = 0; char *bytes2 = hexStrToBytes(sInputStr2, &bytesLen2); if (!bytes2) { printf("Failure! Couldn't convert hex to bytes for second input.\n"); return 1; } char *xorResultStr = malloc(bytesLen1); if (!xorResultStr) { printf("Failure! Couldn't alloc buffer for xor result string.\n"); return 1; } if (bytesLen1 != bytesLen2) { printf("Failure! The lengths of both inputs are different.\n"); return 1; } xorBytes(bytes1, bytes2, xorResultStr, bytesLen1); char *aHexStr = malloc(bytesLen1 * 2); if (!aHexStr) { printf("Failure! Couldn't alloc buffer for hex string.\n"); return 1; } bytesToHexStr(xorResultStr, aHexStr, bytesLen1); if (strcmp(aHexStr, sOutputStr) == 0) { printf("Success!\n"); } else { printf("Failure!\n"); } free(xorResultStr); free(aHexStr); return 0; }
int main(int argc, char *argv[]) { int len; char * aByteBuff = hexStrToBytes(aHexStr, &len); char key = singleByteDecrypt(aByteBuff, len); printf("The key was: %c \nThe message is: ", key); char * result = singleByteXOR(aByteBuff, key, len); printByteBuf(result, len); free(result); }
int main(int argc, char *argv[]) { int blen; double *freqMap = readFreqMap(freqFile); if (!freqMap) { printf("Couldn't read frequencies"); exit(1); } char* inputBytes = hexStrToBytes(inputString, &blen); if (!inputBytes || !blen) { printf("Error converting input to hex"); exit(1); } char key; double score; char *message = crackXOR(inputBytes, blen, freqMap, &key, &score); printf("The message is: %s\n", message); printf("The key is: %c\n", key); free(inputBytes); free(message); free(freqMap); return 0; }
char * decode(char* hexStr, double* currentScore, char* bestKey){ int bytesLen1 = 0; char *bytes1 = hexStrToBytes(hexStr, &bytesLen1); if (!bytes1) { printf("Failure! Couldn't convert hex to bytes.\n"); return NULL; } char *result = malloc(bytesLen1); static double frequencyTable[NUM_LETTERS] = {8.617, 1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966, .153,.772,4.025,2.406,6.749, 7.507,1.929,.095,5.987, 6.327,9.056,2.758,.0978,2.36,.15,1.974,.074 }; double topScore = 0; char* bestString = malloc(bytesLen1*2); //char* bestKey = malloc(sizeof(char));// please please hold the key for (int x = 0; x <= ASCII_MAX; x++) { char* letterCount = malloc(sizeof(char)*ASCII_MAX); for(int y = 0; y<bytesLen1; y++){ result[y] = bytes1[y] ^ (char) x; result[y] = tolower(result[y]); letterCount[result[y]] = letterCount[result[y]] + 1; } char key = (char)x; //hold this key here, for reference towards the bottom double score = 100; for(int x = 0; x < NUM_LETTERS; x++){//for 97-122 (a-z) score = score - fabs(((double)letterCount[x+97]/(double)bytesLen1)*100.0 - frequencyTable[x]); } int spaceTotal = 0; int punctTotal = 0; for(int x = 0; x <= ASCII_MAX; x++){ if(ispunct(x)){ punctTotal = punctTotal + letterCount[x]; } if(isspace(x)){ spaceTotal = spaceTotal + letterCount[x]; } } double punctScore = ((double)punctTotal)/((double)bytesLen1)*100.0; double spaceScore = ((double)spaceTotal)/((double)bytesLen1)*100.0; //will exclude strings that have too much punctuation if(punctScore>25.0){ score = -1; } //will exclude strings that have too much white space score = score - fabs(spaceScore - (1.0/6.0)*100.0); //will keep track of the top scoring string if(score>topScore){ topScore = score; *bestKey = key; for(int s = 0; s < bytesLen1; s++){ bestString[s] = result[s]; } bestString[bytesLen1] = '\0'; } } *currentScore = topScore; //printf("%c \n", *bestKey); //should print out the key that was used for the best string //printf("%s \n", bestString); return bestString; }