?? ecoffice.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 + -