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

📄 paddinghelp.cpp

📁 基于SD卡的软实现CSP程序
💻 CPP
字号:
/*
PaddingHelp.cpp
This source file works with the padding around things that are
encrypted, decrypted or signed.

*/

#include "PaddingHelp.h"
#include "assert.h"

/*
PKCS1_Encode_D_To_EB_01
Purpose:
	As I was originally confused by this function in the desktop
	CSP, here is the missing info which would have made life so much
	easier.  I've also rewritten the proc so that it looks closer
	to this spec.

	This function takes Data (D) and converts it to an encoded block (EB)
	of type 01 as per the PKCS#1 standard version 1.5 (available at www.rsa.com).

	What this means is:
	EB = 00 || BT || PS || 00 || D (where || means concatenate)
	where:
		BT = Block Type (00 or 01 for Priv Key operations, 02 for pub key)
		PS = Padding String (00 for BT=00, FF for BT=01, pseudorand and NOT 0 for BT = 02)
		D = Data (the parameter to the function)

	The length of PS (the padding) is made such that EB is the length of the modulus.
	(Therefore ||PS|| == modulus length - 3 - ||D||)

	(This is from PKCS#1: Section 8.1 Encryption Block Formatting)

Params:
	LPBYTE OUT pbyEncodedBlock: The buffer for the encoded block
	LPBYTE pbyData: The data buffer (D)
	DWORD dwDataLen: The length of the data buffer (||D||)
	DWORD dwModulusLen: The length of the modulus

Returns:
	Always succeeds.  Note that this function will behave unpredictably
	if an insufficiently size buffer is supplied (it needs to be
	at least dwModulusLen bytes)
*/
void PKCS1_Encode_D_To_EB_01(LPBYTE OUT pbyEncodedBlock, LPBYTE pbyData, DWORD dwDataLen, DWORD dwModulusLen)
{
	*(pbyEncodedBlock++) = 0x00;	//All EBs start with 0x00
	*(pbyEncodedBlock++) = 0x01;	//BT is 0x01
	
	//Add PS
	DWORD dwPaddingLength = dwModulusLen - 3 - dwDataLen;
	while(dwPaddingLength--)
		*pbyEncodedBlock++ = 0xFF; //When BT is 0x01, PS is filled with 0xFF

	//0x00 byte after PS
	*(pbyEncodedBlock++) = 0x00;

	//Copy in data
	while(dwDataLen--)
		*(pbyEncodedBlock++) = *(pbyData++);
}

/*
PKCS1_Decode_EB_To_D
Purpose:
	Decodes an EncodedBlock (EB) to raw Data (D) using PKCS#1
	(see function above, PKCS1_Encode_D_To_EB)

Params:
	LPBYTE pbyData: Buffer for data to be placed in
	LPBYTE pbyEncodedBlock: Encrypted data block
	DWORD dwModulusLen: dwModulus

Returns:
	Length of data copied to the supplied buffer.
*/
DWORD PKCS1_Decode_EB_To_D(LPBYTE pbyData, LPBYTE pbyEncodedBlock, DWORD dwModulusLen)
{
	//First byte of an encoded block must always be 0.
	assert(*pbyEncodedBlock == 0x00);
	if(*pbyEncodedBlock++ != 0)
		return 0;

	//Check to make sure we support the encoding type
	BYTE byEncodingType = *(pbyEncodedBlock++);
	//Keep track of how much of the block we have not 
	//yet processed (note the block length is the same as the modulus length)
	dwModulusLen -= 2;

	//Decode the block the appropriate way.
	switch(byEncodingType)
	{
	case 0x00:
		while(0==*(pbyEncodedBlock++) && dwModulusLen > 0)
			dwModulusLen--;

		dwModulusLen++;
		pbyEncodedBlock--;
		break;

	case 0x01:
	case 0x02:
		while(dwModulusLen > 0 && 0!=*(pbyEncodedBlock++) )
			dwModulusLen--;

		if (dwModulusLen) 
		    dwModulusLen--; // skip past the zero
		break;
	default:
		return 0; //unsupported type
	}

	//pbyEncodedBlock is pointing to the start of the data
	//Copy it into pbyData.
	DWORD dwDataLen = dwModulusLen;
	while(dwModulusLen--)
		*(pbyData++) = *(pbyEncodedBlock++);

	return dwDataLen;
}


/*
FixByteOrdering
Purpose:
	Fixes byte ordering from Intel (little endian) to the rest
	of the world (big endian)

Params:
	BYTE* pbyData: Pointer to the data to be swaped
	DWORD dwLength: Length of that data.

Returns:
	Nothing.  Always succeeds.
*/
void FixByteOrdering(BYTE* pbyData, DWORD dwLength)
{
	for(DWORD dwIndex = 0; dwIndex < dwLength / 2; ++dwIndex)
	{
		BYTE byTemp = pbyData[dwIndex];
		pbyData[dwIndex] = pbyData[dwLength - dwIndex - 1];
		pbyData[dwLength - dwIndex - 1] = pbyData[dwIndex];
	}
}

⌨️ 快捷键说明

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