Esempio n. 1
0
/****************************************************************************
Desc:	Compose a key buffer from the vector's components.
****************************************************************************/
RCODE XFLAPI F_DataVector::outputKey(
	IF_Db *			ifpDb,
	FLMUINT			uiIndexNum,
	FLMUINT,			// uiMatchFlags,	//VISIT: Need to remove this from the interface.
	FLMBYTE *		pucKeyBuf,
	FLMUINT			uiKeyBufSize,
	FLMUINT *		puiKeyLen)
{
	RCODE	rc = NE_XFLM_OK;
	IXD *	pIxd;

	if (RC_BAD( rc = ((F_Db *)ifpDb)->m_pDict->getIndex( uiIndexNum, NULL, &pIxd, TRUE)))
	{
		goto Exit;
	}

	if (RC_BAD( rc = outputKey( pIxd, XFLM_MATCH_IDS | XFLM_MATCH_DOC_ID, pucKeyBuf,
								uiKeyBufSize, puiKeyLen, 0)))
	{
		goto Exit;
	}

Exit:

	return( rc);
}
Esempio n. 2
0
int main(int argc, char **argv) {

	char key[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	FILE *fin = stdin;

	/*
	 * Get command line arguments. Setup key and input location
	 */
	switch (argc) {
		case 2:
			fin = fopen(argv[1], "r");
			if (fin == NULL) {
				printf("Invalid file: %s. Unable to open for reading.\n", argv[3]);
				return -1;
			}
		case 1:
			if (!removeLetter(key, 'J')) {
				printf("Could not remove letter J from cipher key.\n");
				return -1;
			}
			break;
		case 4:
			fin = fopen(argv[3], "r");
			if (fin == NULL) {
				printf("Invalid file: %s. Unable to open for reading.\n", argv[3]);
				return -1;
			}
		case 3:
			if (strcmp(argv[1], "-r")) {
				printf("Optional parameter '-r' not found. '%s' found instead.\n", argv[1]);
				return -1;
			}
			if(!removeLetter(key, argv[2][0])) {
				printf("Could not remove letter %c from cipher key.\n", argv[2][0]);
				return -1;
			}
			break;
		default:
			printf("Invalid usage. See below for proper usage.\n");
			printf("\t./%s [ -r <character_to_remove> ] [ ciphertext_filepath ]\n", argv[0]);
			return -1;
	}

	/*	
	 * Input cipher and ensure it is valid
	 */
	char *ciphertext, *plaintext;
	int messageLen;
	ciphertext = readCipher(fin, INPUT_STEP_SIZE);
	messageLen = strlen(ciphertext);
	if (validateText(ciphertext, &messageLen) != 0) {
		free(ciphertext);
		return -1;
	}
	ciphertext = realloc(ciphertext, sizeof(*ciphertext) * (messageLen + 1));
	ciphertext[messageLen] = '\0';
	plaintext = calloc(messageLen + 1, sizeof(*plaintext));
	strcpy(plaintext, ciphertext);	
	
	// close the file as long as it is not stdin
	if (fin != stdin)
		fclose(fin);

	// Output relevant information for error checking
	printf("Attempting to crack the following ciphertext with key: %s\n", key);
	printf("%s\n", ciphertext);

	int iter = 0;
	double score = -DBL_MAX, maxScore = -DBL_MAX;
	srand(time(NULL)); // randomize seed
	// Run until max iteration met 
	while (iter < MAX_ITERATIONS) {
		iter++;
		score = simulatedAnnealing(key, ciphertext, plaintext, messageLen);
		if (score > maxScore) {
			maxScore = score;
			decipher(key, ciphertext, plaintext, messageLen);
			printf("\nPossible Plaintext found using key:\n");
			outputKey(key);
			printf("%s\n\n", plaintext);
		}
	}
	
	free(plaintext);
	free(ciphertext);
	return 0;
}