⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aescryptosystem.cpp

📁 AES implementation in C++. Input: key file and the file for encryption Output: the crypted file.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//		fileSize += quote;
//	}
//
//	if (*ppSrc != NULL)
//		delete [] *ppSrc;
//	*ppSrc = new byte[fileSize];
//	
//	memset(*ppSrc, CHAR_TO_COMPLETE_TEXT, fileSize );
//
//	if (ppDst != NULL)
//	{
//		if (*ppDst != NULL)
//			delete [] *ppDst;
//		
//		*ppDst = new byte[fileSize];
//		memset(*ppSrc, CHAR_TO_COMPLETE_TEXT, fileSize );
//	}
//
//	int iBytesRead = 0;
//	int iBytesTotalRead = 0;
//
//	do 
//	{
//		if ( (iBytesRead = read(fd, *ppSrc + iBytesTotalRead, fileSize)) < 0 )
//		{
//			perror("Error at reading file");
//			exit(1);
//		}
//	
//		iBytesTotalRead += iBytesRead;
//	}
//	while (iBytesRead != 0 && iBytesTotalRead < fileSize);
//
//	close(fd);
//}
//
//void AESCryptoSystem::SaveText(const string& s, int fileSize, byte* pText)
//{
//	if (!pText)
//	{
//		cout << "Nothing to save";
//		return;
//	}
//	
//	int fd = open(s.c_str(), O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IWRITE | S_IREAD);
//
//	if (fd == -1)
//	{
//		perror(s.c_str());
//		return;
//	}
//
//	int iBytesWrite = 0;
//	int iBytesTotalWritten = 0;
//
//	do 
//	{
//		if ( (iBytesWrite = write(fd, pText + iBytesTotalWritten, fileSize)) < 0 )
//		{
//			perror("Error at reading file");
//			return;
//		}
//	
//		iBytesTotalWritten += iBytesWrite;
//	}
//	while (iBytesWrite != 0 && iBytesTotalWritten < fileSize);
//
//	close(fd);
//	
//}
void AESCryptoSystem::ErrorExit(LPTSTR lpszFunction) const
{ 
    TCHAR szBuf[80]; 
    LPVOID lpMsgBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    sprintf(szBuf, 
        "%s failed with error %d: %s", 
        lpszFunction, dw, lpMsgBuf); 
 
    //MessageBox(NULL, szBuf, "Error", MB_OK); 
	std::cout << szBuf;

    LocalFree(lpMsgBuf);
	exit(1);
}


void AESCryptoSystem::SetFile(const string& sFileName, DWORD& fileSize, byte** ppSrcText, byte** ppDstText, bool bIsCryptoText)
{

	HANDLE handle = CreateFile(sFileName.c_str(),  // file name 
        GENERIC_READ,                   // open for reading 
        0,                              // do not share 
        NULL,                           // default security 
        OPEN_EXISTING,                  // existing file only 
        FILE_ATTRIBUTE_NORMAL,          // normal file 
        NULL);                          // no template 
	if (handle == INVALID_HANDLE_VALUE)
		ErrorExit("Open file error: ");
		
	fileSize = GetFileSize(handle, NULL);
	if (fileSize == 0)
	{
		cout << "Empty File : " << sFileName;
		exit(1);
	}

	if (ppSrcText == NULL)
		throw "Invalid pSrcText parameter received in SetFile method";
	
	DWORD dwBytesRead = 0;
	DWORD dwBytesTotalRead = 0;

	if (bIsCryptoText)
	{
		ReadFile(handle, &m_iOriginalTextSize, sizeof(DWORD), &dwBytesRead, NULL);
		fileSize = m_iOriginalTextSize;
	}

	int quote = fileSize % BYTE_SIZE;
	if (quote != 0)
	{
		quote = BYTE_SIZE - quote;
		fileSize = fileSize + quote;
	}

	if (*ppSrcText != NULL)
		delete [] *ppSrcText;
	*ppSrcText = new byte[fileSize];
	memset(*ppSrcText, CHAR_TO_COMPLETE_TEXT, fileSize );

	if (ppDstText != NULL)
	{
		if (*ppDstText != NULL)
			delete [] *ppDstText;
		
		*ppDstText = new byte[fileSize];
		memset(*ppSrcText, CHAR_TO_COMPLETE_TEXT, fileSize );
	}

	fileSize -= quote;
	do 
	{
		if (ReadFile(handle, &(*ppSrcText)[dwBytesTotalRead], fileSize, &dwBytesRead, NULL) == 0 )
			ErrorExit("Read Error: ");
	
		dwBytesTotalRead += dwBytesRead;
	}
	while (dwBytesRead != 0 && dwBytesTotalRead < fileSize);

	if(!bIsCryptoText)
		m_iOriginalTextSize = dwBytesTotalRead;

	fileSize = dwBytesTotalRead;
	quote = fileSize % BYTE_SIZE;
	if (quote != 0)
		fileSize += BYTE_SIZE - quote;
	CloseHandle(handle);
}

void AESCryptoSystem::SaveFile(const std::string& sFileName, DWORD bytesToWrite, const byte* pText, bool bIsCryptedText) const
{
	if (pText == NULL)
	{
		cout << "No Encryption/ Decryption Made";
		return;
	}

	HANDLE hDstFile = CreateFile(sFileName.c_str(),  // file name 
        GENERIC_WRITE, // open for read/write 
        0,                            // do not share 
        NULL,                         // default security 
        CREATE_ALWAYS,                // overwrite existing file
        FILE_ATTRIBUTE_NORMAL,        // normal file 
        NULL);                        // no template 

	if (hDstFile == INVALID_HANDLE_VALUE)
		ErrorExit("Create file error: ");

	DWORD dwBytesWritten = 0;
	DWORD dwBytesTotalWirtten = 0;

	if (bIsCryptedText)
		WriteFile(hDstFile, (LPVOID)&m_iOriginalTextSize, sizeof(DWORD), &dwBytesWritten, NULL);
	else
		bytesToWrite = m_iOriginalTextSize;

	while (dwBytesTotalWirtten < bytesToWrite)
	{
		if (WriteFile(hDstFile, &pText[dwBytesTotalWirtten], bytesToWrite, &dwBytesWritten, NULL) == 0)
				ErrorExit("Write Error: ");

		dwBytesTotalWirtten += dwBytesWritten;
	}

	CloseHandle(hDstFile);
}


void AESCryptoSystem::PrintToScreen(const byte* pT, DWORD size)
{
	for (int i = 0; i < size; i++)
		printf("%x", pT[i]);
	printf("\n");

}

void AESCryptoSystem::PrintToScreen(const word &w)
{
	for (int i = 0; i < 4; i++)
		printf("%x", w[i]);
	printf(" ");

}

void AESCryptoSystem::PrintToScreen(const state &s)
{
	printf("\n");
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			printf("%x ", s[i][j]);
		printf("\n");
	}

}
void AESCryptoSystem::PrintToScreenKey(const word* pW)
{
	printf("\n");
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			printf("%x ", pW[j][i]);
		printf("\n");
	}

}

//**************************************************************
// Crypto Function
//**************************************************************
void AESCryptoSystem::CalculateRoundKey()
{
	word temp;

	int idx = 0;
	if (m_pExpandedKey)
		delete [] m_pExpandedKey;

	int expKeySize = m_ciNb * (m_iNr + 1);
	m_pExpandedKey = new word[expKeySize];
	for (; idx < m_iNk; idx++)
	{
		m_pExpandedKey[idx][0] = m_pKey[4 * idx];
		m_pExpandedKey[idx][1] = m_pKey[4 * idx + 1];
		m_pExpandedKey[idx][2] = m_pKey[4 * idx + 2];
		m_pExpandedKey[idx][3] = m_pKey[4 * idx + 3];
	}

	for (; idx < expKeySize; idx++)
	{
		temp[0] = m_pExpandedKey[idx - 1][0];
		temp[1] = m_pExpandedKey[idx - 1][1];
		temp[2] = m_pExpandedKey[idx - 1][2];
		temp[3] = m_pExpandedKey[idx - 1][3];

		if (idx % m_iNk == 0)
		{
			//PrintToScreen(temp);
			RotByte(temp);
			//PrintToScreen(temp);
			SubBytes(temp);
			//PrintToScreen(temp);
			temp[0] = temp[0] ^ st_cRcon[ idx / m_iNk - 1];
			//PrintToScreen(temp);
		}
		else
			if( (m_iNk > 6) && (idx % m_iNk == 4))
				SubBytes(temp);
		m_pExpandedKey[idx][0] = m_pExpandedKey[idx - m_iNk][0] ^ temp[0];
		m_pExpandedKey[idx][1] = m_pExpandedKey[idx - m_iNk][1] ^ temp[1];
		m_pExpandedKey[idx][2] = m_pExpandedKey[idx - m_iNk][2] ^ temp[2];
		m_pExpandedKey[idx][3] = m_pExpandedKey[idx - m_iNk][3] ^ temp[3];
	}
}

void AESCryptoSystem::RotByte(word& w)
{
	byte temp = w[0];
	w[0] = w[1];
	w[1] = w[2];
	w[2] = w[3];
	w[3] = temp;
}
void AESCryptoSystem::SubBytes(word& w)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -