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

📄 ecoffice.cpp

📁 椭圆曲线算法改装
💻 CPP
字号:
#include "ECOffice.h"
#include "crc32.h"

//////////////////////////////////////////////
// convert string to bit stream 
// input string and length (not include null-stop), return bit stream 
////////////////////////////////////////////////
INTVECTOR Byte2Bit(const char* str , const int len)
{
	INTVECTOR tmpVec;
	int i, j;
	BYTE *tmp = new BYTE [len];
	for(i = 0 ; i < len; i++)
	{
		tmp[i] = str[i];
		for(j = 0; j < 8; j++)
		{
			if( (tmp[i] << j) & 0x80 )
				tmpVec.push_back(1);
			else
				tmpVec.push_back(0);
		}
	}
	if(tmp != NULL)
	{
		delete tmp;
		tmp = NULL;
	}
	return tmpVec;
}

/***************************************************************************************
		函数功能:		vector转化为int, 不足位数补0
		参数:
				vec	------------------	vector形式的密码
				key	------------------	int形式的密码
				len	------------------	密码最大长度
		返回值:	
***************************************************************************************/
void vec2int(INTVECTOR vec, unsigned int *key, const int len)
{
	int i, j;
	unsigned int *temp = new unsigned int [len*8];
	for (j = 0, i = len*8-vec.size (); i < len*8; i ++, j ++)
	{
		temp[i] = vec[j];
	}
	i = vec.size ();
	for (i = 0, j = len*8-vec.size (); i < j; i ++)
		temp[i] = 0;
	for (i = 0; i < len; i ++)
	{
		for (key[i] = 0, j = 0; j < 8; j ++)
		{
			key[i] += temp[i*8+j] * pow(2, 8-j-1);
		}
	}
	delete [] temp;
}

/* Return a 32-bit CRC of the contents of the buffer. */
unsigned long crc32(const unsigned int *s, const unsigned int len)
{
	unsigned int i;
	unsigned long crc32val;

	crc32val = 0;
	for (i = 0; i < len; i ++)
	{
		crc32val = crc32_tab[(crc32val ^ s[i]) & 0xff] ^ (crc32val >> 8);
	}
	return crc32val;
}

///////////////////////////////
// 
// Convert decimate number into binary number
// Input: crc	---		decimate number
//		  bin	---		binary number, presented by binary vector
//
void dec2bin(const unsigned long crc, unsigned char *bin)
{
	int i, j;
	double x;
	unsigned char temp[4];
	unsigned long tmp;
	tmp = crc ^ 0xffffffff;

	for (i = 0; i < 4; i ++)
	{
		temp[i] = ((BYTE*)&(tmp))[i];
		for(j = -7; j <= 0; j ++)
		{
			x = floor (temp[i]*pow(2,j));
			bin[i*8+j+7] = (int)fmod(x,2);
		}
	}
}

/***************************************************************************************
				函数功能: 将用户输入密码加密后保存入文件
				参数:
					password	----------------	密码
					filename	----------------	文件名
***************************************************************************************/
extern "C" _declspec(dllexport) void _stdcall SaveUserPassword(const char *password, const char *filename)
{		//	给VB调用的DLL函数必须加上_stdcall
	try
	{
		int count;
		unsigned long crc = 0;
		INTVECTOR vec;		
		unsigned int *key = new unsigned int [MAX_PASSWORD_LENGTH];	//	二进制密码
		BYTE *bin = new BYTE [MAX_PASSWORD_LENGTH];	//	crc校验码
		FILE *fp;

		if ((fp = fopen(filename, "w+")) != NULL)
		{
			if (*password)
			{
				for (count = 0; password[count] != '\0' && count < MAX_PASSWORD_LENGTH; count ++);

				vec = Byte2Bit(password, count);
				vec2int(vec, key, MAX_PASSWORD_LENGTH);
				crc = crc32(key, MAX_PASSWORD_LENGTH);		//	密码做校验
				dec2bin(crc, bin);

				fwrite(bin, sizeof(BYTE), MAX_PASSWORD_LENGTH, fp);
			}
			else	MessageBox(NULL, "无法存储您的密码!", "密码存储失败", MB_OK);

			fclose(fp);
		}
		else	MessageBox(NULL, "密码文件无法创建, 无法存储您的密码!", "密码存储失败", MB_OK);

		delete [] key;
		delete [] bin;
		delete fp;
	}
	catch(...)
	{
		MessageBox(NULL, "安装失败, 请重新安装程序!", "未知错误", MB_OK);
	}
}

//********************************************************************
//generate EC key modual,2 parameter
//zPrivkeyfile-----------------the path to place generated private key
//zPubkeyfile-----------------the path to place generated public key
//********************************************************************
extern "C" __declspec(dllexport) void _stdcall GenerateECDKey (const char *zPrivkeyfile, const char* zPubkeyfile)
{		//	给VB调用的DLL函数必须加上_stdcall
	try
	{
		GenerateECKey(zPrivkeyfile, zPubkeyfile);
	}
	catch(...)
	{
		MessageBox(NULL, "公私钥生成失败, 请重新安装程序!", "未知错误", MB_OK);
	}
}

//******************************************************************************
//generat ECD signfile modual,4 parameter
//zPrivkeyFile--------------------------the path where the pivater key placed
//zOrigFile-----------------------------the path where the origianl file to be signed placed
//zSignFileName-------------------------the path where the signature file to be placed
//zSignFile-----------------------------this parameter be used to pass the sign information
// success return 0 ; or return 1 
//******************************************************************************
extern "C" int _declspec(dllexport) ECDSignFile(const char *zPrivkeyFile,
				 const char *zTempFile,
				 const char *SignFileName,
				 int *zSignFile)
{
	try
	{
		int i;
		unsigned char *sign = new unsigned char [EC_LENGTH];
		unsigned char *binaryData = new unsigned char [EC_BIT];

		ECSignFile(zPrivkeyFile, zTempFile, SignFileName);

		FILE *fp;
		fp = fopen(SignFileName, "r");
		fread(sign, sizeof(unsigned char), EC_BIT, fp);
		fclose(fp);

		binaryData[0]=sign[0] & 0x01;
        zSignFile[0]=binaryData[0];

		//将签名信息的字符数组转换成二进制形式
		for(i = 1; i < EC_BIT; i ++)
		{
			binaryData[i]=((sign[i/8]>>(i%8)) & 0x01);
			zSignFile[i] = binaryData[i];
		}

		delete [] sign;
		delete [] binaryData;

		return 0;
	}
	catch(...)
	{
		return 1;
	}
}

//******************************************************************************
//EC verify modual, 4 parameter
//pubkeyfile---------------------------the path where the public key placed
//origFile-----------------------------the path where the original file placed
//signFile-----------------------------the path where the signature file placed
//binaryWm-----------------------------the watermarked info extracted from image be 
//                                     used to verify the oiginal file
//******************************************************************************
extern "C" int _declspec(dllexport) ECDVerifyFile(const char *pubkeyfile,
				    const char *origFile,
				    const char *signFile,
					const int* binaryWm)
{
	try
	{
		int i, j;

		//transform binary array to string
		int temp [EC_LENGTH];
		unsigned char signArray[EC_LENGTH];

		for (i = 0; i < EC_LENGTH; i ++)
			temp[i] = 0;

		for( i = 0; i < EC_LENGTH; i ++ )
		{
			for( j = 0; j <= 7; j ++ )
			{
				if(binaryWm[i*8+j]==1)
					temp[i]=temp[i] | (int)(pow(2, j));
				else
					temp[i]=temp[i] & ~(int)(pow(2, j));
			}
		}

		for (i = 0; i < EC_LENGTH; i ++)
			signArray[i] = temp[i];

		//将得到的字符串存成文件
		FILE *fp;
		fp = fopen(signFile, "w+");
		fwrite(signArray, sizeof(unsigned char), EC_LENGTH, fp);
		fclose(fp);

		if( ECVerifyFile(pubkeyfile, origFile, signFile) )	return 0;
		else	return 1;
	}
	catch(...)
	{
		return -1;
	}
}

⌨️ 快捷键说明

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