BuildDrive Ответов: 1

Ошибка связывания: "int_cded..." уже определена в main.obj


Мне нужна помощь, но я не могу понять, что случилось.

вот в чем ошибка:
LNK2005 "int_cded encryptData(char *,int)" (?encryptData@@YAHPADH@Z) already defined in main.obj
LNK2005 "int_cded decrypttData(char *,int)" (?decryptData@@YAHPADH@Z) already defined in main.obj


Заголовок.ч
#pragma once
#define  _CRT_SECURE_NO_WARNINGS

// Main.h
//
//

#define CRYPTO_ORDER "ABCDE\0" 

// Include Files
#include <windows.h>
#include <stdio.h>
#include <io.h>

// Defines
// #define TEST_CODE

#define _CRT_SECURE_NO_WARNINGS

#define CRYPTO_ORDER "ABCDE\0"
#define var_Index -4
#define var_HopCnt -8
#define var_RoundNum -12
#define var_DataLength -16


#define FALL_2017		201708
#define SPRING_2018		201801
#define SUMMER_2018		201806
#define FALL_2018		201808

#define CURRENT_SEMESTER FALL_2018

// Structures


// Prototypes
int sha256(char* fileName, char* dataBuffer, DWORD dataLength, unsigned char sha256sum[32]);
void encryptData_01(char* data, int length);
void decryptData_01(char* data, int length);
void encryptData_02(char* data, int length);
void decryptData_02(char* data, int length);
int encryptData_03(char* data, int length);
int decryptData_03(char* data, int length);
int encryptData_Final(char* data, int length);
int decryptData_Final(char* data, int length);

// Global Variable Extern Declarations
extern unsigned char gkey[65537];
extern unsigned char* gptrKey;
extern char gPassword[256];
extern unsigned char gPasswordHash[32];
extern unsigned char* gptrPasswordHash;
extern char gCRYPTO_ORDER[8];
extern unsigned char gdebug1, gdebug2;

extern FILE* gfptrIn;
extern FILE* gfptrOut;
extern FILE* gfptrKey;
extern char gInFileName[256];
extern char gOutFileName[256];
extern char gKeyFileName[256];
extern int gOp;			// 1 = encrypt, 2 = decrypt
extern int gNumRounds1;

extern unsigned char gEncodeTable[256];
extern unsigned char gDecodeTable[256];


EncryptData_Initial.cpp
#include "Header.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
// code to encrypt the data as specified by the project assignment
int encryptData(char* data, int dataLength)
{
	int resulti = 0;

	unsigned char gdebug1 = 0;					// a couple of global variables that could be used for debugging
	unsigned char gdebug2 = 0;					// also can have a breakpoint in C code

	// You can not declare any local variables in C, but should use resulti to indicate any errors
	// Set up the stack frame and assign variables in assembly if you need to do so
	// access the parameters BEFORE setting up your own stack frame
	// Also, you cannot use a lot of global variables - work with registers

	__asm {
		// you will need to reference some of these global variables
		// (gptrPasswordHash or gPasswordHash), (gptrKey or gkey), gNumRounds

		// simple example that xors 2nd byte of data with 14th byte in the key file

	//		mov esi, gptrKey;						// put the ADDRESS of gkey into esi (since *gptrKey = gkey)
			mov esi, gptrPasswordHash				// put ADDRESS of gPasswordHash into esi (since unsigned char *gptrPasswordHash = gPasswordHash)
			xor eax, eax
			mov al, byte ptr[esi]
			shl ax, 8
			xor ecx, ecx
			mov cl, byte ptr[esi + 1]
			add ax, cx

			xor ebx, ebx
			xor ecx, ecx
			mov ecx, dataLength
			cmp ecx, 0
			sub ecx, 1
			jbe lbl_EXIT_ZERO_LENGTH

			mov edi, data
			mov esi, gptrKey
	//		mov al, byte ptr[esi]					// get first byte of password hash
	//		mov al, byte ptr[esi + 1]
	//		mov al, byte ptr[esi + 2]
	//		mov al, byte ptr[esi + 3]
	//		mov al, byte ptr[esi + 4]				// get 5th byte of password hash
	//		mov al, byte ptr[esi + 5]

	//		mov ecx, dataLength
	//		mov edi, data

	lbl_LOOP :
			mov dl, byte ptr[edi + ebx]
			xor dl, byte ptr[esi + eax]
			mov byte ptr[edi + ebx], dl

			add ebx, 1
			cmp ebx, ecx
	//		mov ebx, 2
			ja lbl_EXIT_END
			jmp lbl_LOOP
	
	lbl_EXIT_ZERO_LENGTH :
			sub ebx, 1
			jmp lbl_EXIT

	lbl_EXIT_END :
		    xor ebx, ebx

	lbl_EXIT :
			mov resulti, ebx
	//		mov al, byte ptr[esi + ebx]				// get 3rd byte of password hash
	//		mov al, byte ptr[esi + ebx * 2]			// get 5th byte of password hash

	//		mov ax, word ptr[esi + ebx * 2]			// gets 5th and 6th bytes of password hash ( gPasswordHash[4] and gPasswordHash[5] ) into ax
	//		mov eax, dword ptr[esi + ebx * 2]		// gets 4 bytes, as in:  unsigned int X = *( (unsigned int*) &gPasswordHash[4] );

	//		mov al, byte ptr[gkey]
	//		mov al, byte ptr[gkey + 1]
	//		mov al, byte ptr[gkey + ebx]			// get's 3rd byte of gkey[] data
	//		mov al, byte ptr[gkey + ebx + 1]
	//		mov al, byte ptr[gkey + ebx + 2]
	//		mov al, byte ptr[gkey + ebx + 3]

	//		mov al, byte ptr[gptrKey + ebx]			// THIS IS INCORRECT - will add the address of the gptrKey global variable (NOT the value that gptrKey holds)

	//		mov al, byte ptr[esi + 0xd];			// access 14th byte in gkey[]: 0, 1, 2 ... d is the 14th byte
													// Put ADDRESS of first data element into edi

	//		xor byte ptr[edi + 1], al				// Exclusive-or the 2nd byte of data with the 14th element of the keyfile
													// NOTE: Keyfile[14] = 0x21, that value changes the case of a letter and flips the LSB
													// Capital "B" = 0x42 becomes lowercase "c" since 0x42 xor 0x21 = 0x63
	}

	return resulti;
} // encryptData


decryptData_Initial.cpp
#include "Header.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
// code to decrypt the data as specified by the project assignment
int decryptData(char* data, int dataLength)
{
	int resulti = 0;

	unsigned char gdebug1 = 0;					// a couple of global variables that could be used for debugging
	unsigned char gdebug2 = 0;					// also can have a breakpoint in C code

	// You can not declare any local variables in C, but should use resulti to indicate any errors
	// Set up the stack frame and assign variables in assembly if you need to do so
	// access the parameters BEFORE setting up your own stack frame
	// Also, you cannot use a lot of global variables - work with registers

	__asm {
		// you will need to reference some of these global variables
		// (gptrPasswordHash or gPasswordHash), (gptrKey or gkey), gNumRounds

		// simple example that xors 2nd byte of data with 14th byte in the key file

	//	mov esi, gptrKey;						// put the ADDRESS of gkey into esi (since *gptrKey = gkey)
		mov esi, gptrPasswordHash				// put ADDRESS of gPasswordHash into esi (since unsigned char *gptrPasswordHash = gPasswordHash)
		xor eax, eax
		mov al, byte ptr[esi]
		shl ax, 8
		xor ecx, ecx
		mov cl, byte ptr[esi + 1]
		add ax, cx

		xor ebx, ebx
		xor ecx, ecx
		mov ecx, dataLength
		cmp ecx, 0
		sub ecx, 1
		jbe lbl_EXIT_ZERO_LENGTH
//		mov al, byte ptr[esi]					// get first byte of password hash
//		mov al, byte ptr[esi + 1]
//		mov al, byte ptr[esi + 2]
//		mov al, byte ptr[esi + 3]
//		mov al, byte ptr[esi + 4]				// get 5th byte of password hash
//		mov al, byte ptr[esi + 5]

		mov edi, data
		mov esi, gptrKey

	lbl_LOOP :
		mov dl, byte ptr[edi + ebx]
		xor dl, byte ptr[esi + eax]
		mov byte ptr[edi + ebx], dl

		add ebx, 1
		cmp ebx, ecx
	//	mov ebx, 2
		ja lbl_EXIT_END
		jmp lbl_LOOP

	lbl_EXIT_ZERO_LENGTH :
		sub ebx, 1
		jmp lbl_EXIT

	lbl_EXIT_END :
		xor ebx, ebx

	lbl_EXIT :
		mov resulti, ebx
//		mov al, byte ptr[esi + ebx]				// get 3rd byte of password hash
//		mov al, byte ptr[esi + ebx * 2]			// get 5th byte of password hash

//		mov ax, word ptr[esi + ebx * 2]			// gets 5th and 6th bytes of password hash ( gPasswordHash[4] and gPasswordHash[5] ) into ax
//		mov eax, dword ptr[esi + ebx * 2]		// gets 4 bytes, as in:  unsigned int X = *( (unsigned int*) &gPasswordHash[4] );

//		mov al, byte ptr[gkey]
//		mov al, byte ptr[gkey + 1]
//		mov al, byte ptr[gkey + ebx]			// get's 3rd byte of gkey[] data
//		mov al, byte ptr[gkey + ebx + 1]
//		mov al, byte ptr[gkey + ebx + 2]
//		mov al, byte ptr[gkey + ebx + 3]

//		mov al, byte ptr[gptrKey + ebx]			// THIS IS INCORRECT - will add the address of the gptrKey global variable (NOT the value that gptrKey holds)

//		mov al, byte ptr[esi + 0xd];			// access 14th byte in gkey[]: 0, 1, 2 ... d is the 14th byte
															// Put ADDRESS of first data element into edi

//		xor byte ptr[edi + 1], al				// Exclusive-or the 2nd byte of data with the 14th element of the keyfile
															// NOTE: Keyfile[14] = 0x21, that value changes the case of a letter and flips the LSB
															// Capital "B" = 0x42 becomes lowercase "c" since 0x42 xor 0x21 = 0x63
	}

	return resulti;
} // decryptData


main.cpp
#define  _CRT_SECURE_NO_WARNINGS

#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <string.h>

#include "Header.h"
// #include "sha256.h"

#include "EncryptData_Initial.cpp"
#include "DecryptData_Initial.cpp"

int encryptFile(FILE* fptrIn, FILE* fptrOut)
{
	char* buffer;
	unsigned int filesize;

	filesize = _filelength(_fileno(fptrIn));	// Linux???
	if (filesize > 0x1000000)					// 16 MB, file too large
	{
		fprintf(stderr, "Error - Input file too large.\n\n");
		return -1;
	}

	// use the password hash to encrypt
	buffer = (char*)malloc(filesize);
	if (buffer == NULL)
	{
		fprintf(stderr, "Error - Could not allocate %d bytes of memory on the heap.\n\n", filesize);
		return -1;
	}

	fread(buffer, 1, filesize, fptrIn);	// read entire file
	encryptData(buffer, filesize);
	fwrite(buffer, 1, filesize, fptrOut);
	free(buffer);

	return 0;
} // encryptFile

int decryptFile(FILE* fptrIn, FILE* fptrOut)
{
	char* buffer;
	unsigned int filesize;

	filesize = _filelength(_fileno(fptrIn));	
	if (filesize > 0x1000000)					
	{
		fprintf(stderr, "Error - Input file too large.\n\n");
		return -1;
	}

	
	buffer = (char*)malloc(filesize);
	if (buffer == NULL)
	{
		fprintf(stderr, "Error - Could not allocate %d bytes of memory on the heap.\n\n", filesize);
		return -1;
	}

	fread(buffer, 1, filesize, fptrIn);	
	decryptData(buffer, filesize);
	fwrite(buffer, 1, filesize, fptrOut);
	free(buffer);

	return 0;
} // decryptFile

FILE* openInputFile(char* filename)
{
	FILE* fptr;
	
	fptr = fopen("AfterDebugging.jpg", "rb");
	if (fptr == NULL)
	{
		fprintf(stderr, "\n\nError - Could not open input file %s!\n\n", filename);
		exit(-1);
	}
	return fptr;
} // openInputFile


FILE* openOutputFile(char* filename)
{	
	FILE* fptr;

	fptr = fopen("AfterDebugging.jpg", "wb+");
	if (fptr == NULL)
	{
		fprintf(stderr, "\n\nError - Could not open output file %s!\n\n", filename);
		exit(-1);
	}
	return fptr;
} // openOutputFile

/* int sha256(char* fileName, char* dataBuffer, DWORD dataLength, unsigned char sha256sum[32]) {
	
	char gPassword[256];

	void sha256_starts(sha256_context * ctx);
	void sha256_update(sha256_context * ctx, uint8 * input, uint32 length);
	void sha256_finish(sha256_context * ctx, uint8 digest[32]);

	
} */

void usage(char* argv[]) {

	printf("\n\nUsage:\n\n");
	printf("%s -<e=encrypt or d=decrypt> -i <message_filename> -k <keyfile> -p <password> [-r <#rounds>]\n\n", argv[0]);
	printf("-e				:encrypt the specified file\n");
	printf("-d				:decrypt the specified file\n");
	printf("-i filename		:the name of the file to encrypt or decrypt\n");
	printf("-p password		:the password to be used for encryption [default='password']\n");
	printf("-r <#rounds>	:number of encryption rounds (1 - 3)  [default = 1]\n");
	printf("-o filename		:name of the output file [default='encrypted.txt' or 'decrypted.txt'\n\n");
	exit(0);

}	// usage

void parseCommandLine(int argc, char* argv[]) {

	int cnt;
	char ch;
	bool i_flag, o_flag, k_flag, p_flag, err_flag;

	i_flag = k_flag = false;
	err_flag = p_flag = o_flag = false;

	cnt = 1;

	while (cnt < argc) {

		ch = *argv[cnt];
		
		if (ch != '-') {

			fprintf(stderr, "All options must be preceeded by a dash '-'\n\n");
			usage(argv);

		}

		ch = *(argv[cnt] + 1);

		if (0) {

		}
		else if (ch == 'e' || ch == 'E') {

			if (gOp != 0) {

				fprintf(stderr, "Error! Already specified encrypt or decrypt.\n\n");
				usage(argv);

			}

			gOp = 1;

		}
		else if (ch == 'd' || ch == 'D') {

			if (gOp != 0) {
				
				fprintf(stderr, "Error! Already specified encrypt or decrypt.\n\n");
				usage(argv);

			}

			gOp = 2;

		}
		else if (ch == 'i' || ch == 'I') {

			if (i_flag == true) {

				fprintf(stderr, "Error! Already specifed an input file.\n\n");
				usage(argv);

			}

			i_flag = true;
			cnt++;
			if (cnt >= argc) {

				fprintf(stderr, "Error! Must specify a filename after '-i'\n\n");
				usage(argv);

			}

			strncpy(gInFileName, argv[cnt], 256);

		}
		else if (ch == 'o' || ch == 'O') {

			if (o_flag == true) {

				fprintf(stderr, "Error! Already specifed an output file.\n\n");
				usage(argv);

			}

			o_flag = true;
			cnt++;
			if (cnt >= argc) {

				fprintf(stderr, "Error! Must specify a filename after '-o'\n\n");
				usage(argv);

			}

			strncpy(gOutFileName, argv[cnt], 256);

		}
		else if (ch == 'k' || ch == 'K') {

			if (k_flag == true) {

				fprintf(stderr, "Error! Already specifed a key file.\n\n");
				usage(argv);

			}

			k_flag = true;
			cnt++;
			
			if (cnt >= argc) {

				fprintf(stderr, "Error! Must specify a file name after '-k'\n\n");
				usage(argv);

			}

			strncpy(gKeyFileName, argv[cnt], 256);

		}

		else if (ch == 'p' || ch == 'P') {

			if (p_flag == true) {

				fprintf(stderr, "Error! Already specifed a password.\n\n");
				usage(argv);

			}

			p_flag = true;
			cnt++;

			if (cnt >= argc) {

				fprintf(stderr, "Error! Must enter a password after '-p'\n\n");
				usage(argv);

			}

			strncpy(gPassword, argv[cnt], 256);

		}
		else if (ch == 'r' || ch == 'R') {

			int x;

			cnt++;

			if (cnt >= argc) {

				fprintf(stderr, "Error! Must enter number between 1 and 3 after '-r'\n\n");
				usage(argv);

			}
			
			x = atoi(argv[cnt]);

			if (x < 1 || x > 3) {

				fprintf(stderr, "Warnning! Entered bad value for number of rounds. Setting it to one.\n\n");

				x = 1;

			}

				int gNumRounds = x;

		} else {

		fprintf(stderr, "Error! Illegal option in argument. %s\n\n", argv[cnt]);
		usage(argv);

		}

		cnt++;

	}

	if (gOp == 0) {

		fprintf(stderr, "Error! Encypt or Decrypt must sprecifed.\n\n");
		err_flag = true;

	}

	if (i_flag == false) {

		fprintf(stderr, "Error! No input file specified.\n\n");
		err_flag = true;

	}

	if (k_flag == false) {

		fprintf(stderr, "Error! No key file specified. \n\n");
		err_flag = true;

	}

	if (p_flag == false) {

		fprintf(stderr, "Warning! Using default 'password'.\n\n");

	}

	if (o_flag == false && err_flag == false) {

		strcpy(gOutFileName, gInFileName);

		if (gOp == 1) {

			strcat(gOutFileName, ".enc");

		}

		if (gOp == 2) {

			strcat(gOutFileName, ".dec");

		}

	}

	if (err_flag) {

		usage(argv);

	}

	return;

} // parseCommandLine

unsigned char gkey[65537];
unsigned char* gptrKey = gkey;
char gPassword[256] = "SECRET";
unsigned char gPasswordHash[32];
unsigned char* gptrPasswordHash = gPasswordHash;
char gCRYPTO_ORDER[8];

FILE* gfptrIn = NULL;
FILE* gfptrOut = NULL;
FILE* gfptrKey = NULL;
char gInFileName[256];
char gOutFileName[256];
char gKeyFileName[256];
int gOp = 0;			// 1 = encrypt, 2 = decrypt
int gNumRounds = 1;

unsigned char gEncodeTable[256];
unsigned char gDecodeTable[256];

int main(int argc, char* argv[])
{
	int length, resulti;

	parseCommandLine(argc, argv);

	gfptrIn = openInputFile(gInFileName);
	gfptrKey = openInputFile(gKeyFileName);
	gfptrOut = openOutputFile(gOutFileName);

	length = (size_t)strlen(gPassword);

	resulti = sha256(NULL, gPassword, length, gPasswordHash);		
	
	if (resulti != 0)
	{
		fprintf(stderr, "Error! Password not hashed correctly.\n\n");
		exit(-1);
	}

	length = fread(gkey, 1, 65537, gfptrKey);
	if (length != 65537)
	{
		fprintf(stderr, "Error! Length of key file is not at least 65537.\n\n");
		exit(-1);
	}
	fclose(gfptrKey);
	gfptrKey = NULL;

	if (gOp == 1)	
	{
		encryptFile(gfptrIn, gfptrOut);
	}
	else
	{
		decryptFile(gfptrIn, gfptrOut);
	}

	fclose(gfptrIn);
	fclose(gfptrOut);
	
	return 0;

} // main


Что я уже пробовал:

Я пытался найти много способов выяснить, в чем заключается проблема, например, изменить имя глобальных переменных, но я не уверен, почему ошибка говорит о двух разных файлах, уже определенных в main.obj.

1 Ответов

Рейтинг:
2

Rick York

Проблема в том, что вы включаете файлы исходного кода. Они должны быть скомпилированы отдельно, а затем связаны вместе. Ошибка заключается в том, что исходные файлы включены, EncryptData_Initial.cpp и еще DecryptData_Initial.cpp, вероятно, также будут скомпилированы, что приведет к тому, что компоновщик увидит функции дважды - отсюда и ошибка.

Прокомментируйте включение этих двух файлов, и он должен связать их просто отлично. Убедитесь, что у вас есть прототипы, определенные для функций, вызываемых в этих двух файлах. Вы увидите ошибки компиляции, если этого не сделаете.


BuildDrive

Не удается найти ссылку в разделе "Добавить существующий элемент" для Visual Studio. Ничего, если вы покажете или скажете мне, как связать эти файлы?