加密文件

#include "stdafx.h" //#include #include #include #include //#define FUNC_EXPORT __declspec(dllimport) #include "../加密卡/BaseHSM.h" #include "../subasn1.h" #define SOFT_KEY #define FRAME_SIZE 4096 #define MYTCRYPTFILE "/x00/x77/x02/x15/x77/x11/x12/x00" typedef unsigned long int UINT4; extern "C" int CSP_RSAPublicEncrypt ( unsigned char *output, unsigned int *outputLen, unsigned char *input, unsigned int inputLen, unsigned char *publicKey, unsigned int publicKeyLen); typedef struct { UINT4 state[4]; /* state (ABCD) */ UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ unsigned char buffer[64]; /* input buffer */ } MD5_CTX; typedef struct { UINT4 subkeys[32]; /* subkeys */ UINT4 iv[2]; /* initializing vector */ UINT4 originalIV[2]; /* for restarting the context */ int encrypt; /* encrypt flag */ } DES_CBC_CTX; typedef struct { UINT4 subkeys[3][32]; /* subkeys */ UINT4 iv[2]; /* initializing vector */ UINT4 originalIV[2]; /* for restarting the context */ int encrypt; /* encrypt flag */ } DES3_CBC_CTX; typedef struct{ MD5_CTX ctx; DES3_CBC_CTX text; }MYTSEC_CTX; #ifdef __cplusplus extern "C" { #endif void MD5Init (MD5_CTX *ctx); void MD5Update (MD5_CTX *ctx, unsigned char *in, unsigned int inLen); void MD5Final (unsigned char out[16], MD5_CTX *ctx); void DES3_CBCInit (DES3_CBC_CTX *, unsigned char *, unsigned char *, int); int DES3_CBCUpdate (DES3_CBC_CTX *, unsigned char *, unsigned char *, unsigned int); void DES3_CBCRestart (DES3_CBC_CTX *); #ifdef __cplusplus } #endif //int FUNC_GenerateKey(int keyType,int kl,unsigned char*keyBuf,int *keyBufLen); int MakeBodyInitX(bool flg,unsigned char sessionKey[32],unsigned char iv[8],MYTSEC_CTX *ctx) { //MD5Init(&(ctx->ctx)); DES3_CBCInit(&(ctx->text),sessionKey,iv,flg); return 0; } //parkLen的值必须是8的整数倍,如果不是需要将park补位 int MakeBodyUpdateX(unsigned char *park,int parkLen,unsigned char *out,MYTSEC_CTX *ctx) { if(ctx->text.encrypt <= 0){ //解密 //MD5Update(&(ctx->ctx),park,parkLen); } DES3_CBCUpdate (&(ctx->text), out, park, parkLen); if(ctx->text.originalIV[0]++ % 50 == 0){ //printf("random ...."); unsigned char buf[200]; FUNC_GenerateRandom(buf,32); } if(ctx->text.encrypt > 0){ //加密 //MD5Update(&(ctx->ctx),out,parkLen); } return 0; } int MakeBodyFinalX(unsigned char *degist,MYTSEC_CTX *ctx) { //MD5Final(degist,&(ctx->ctx)); DES3_CBCRestart(&(ctx->text)); return 0; } void RandomX(unsigned char buf[],int len) { for(int i = 0;i 150){ rc = 1001; goto ENDFUNC; } //检测KEY是否存在 //校验KEY是否是MYT签发的--利用这个操作获取用户口令以后就不用填了 inFp = outFp = NULL; //加密报文 inFp = fopen(inPath,"rb"); if(inFp == NULL){ rc = 1001; goto ENDFUNC; } fseek(inFp,0,2); fileSize = ftell(inFp); //取得文件的大小 fseek(inFp,0,0); outFp = fopen(outPath,"wb"); if(outFp == NULL){ rc = 1002; goto ENDFUNC; } memcpy(out,MYTCRYPTFILE,8); //不明了 从MYCRYPTFILE 拷贝 8个字节到out 中 rc = fwrite(out,1,8,outFp);//特殊标示 从out 添加8个数据项到outFp 之中 每一次是一个字节 if(outFp == NULL){ rc = 1003; goto ENDFUNC; } //明文的会话密钥送入, //生成随机数 unsigned char sessionKey[300]; RandomX(sessionKey,16);// 此处的作用是生成一个随机数 rc = FUNC_GenerateKey(DES3_TYPE,16,sessionKey,&len);//此处的作用就是生成DES密钥 if(rc != 0){ rc = 1004; goto ENDFUNC; } memcpy(sessionKey+16,sessionKey,8);//不明了 sessionKey+16 是什么作用 //strcpy((char*)sessionKey,"12345678"); len = 32;//目前用DES密钥作会话密钥 memcpy(out,&len,4); rc = fwrite(out,1,4,outFp);//保存密钥长度 if(outFp == NULL){ rc = 1005; goto ENDFUNC; } memcpy(out,"/x00/x00/x00/x00/x00/x00/x00/x00",8); rc = fwrite(out,1,8,outFp);//保存IV if(outFp == NULL){ rc = 1006; goto ENDFUNC; } //templates 难道是用 pubKey 给sessionKey 加密吗?这一点不是很懂 rc = CSP_RSAPublicEncrypt (templates, (unsigned int *)&len,sessionKey,32,pubKey,pubLen); if(rc != 0){ rc = 1007; goto ENDFUNC; } //templates 这里又是什么呢? rc = fwrite(templates,1,128+8,outFp);//保存会话密钥和密钥序列号 if(outFp == NULL){ rc = 1008; goto ENDFUNC; } rc = MakeBodyInitX(true,sessionKey,(unsigned char *)out,&ctx); if(rc != 0){ rc = 1009; goto ENDFUNC; } pos = 0;currentPos = 0; while(!feof(inFp)){//in 在这里是空的,什么也没有 len = fread(in,1,FRAME_SIZE,inFp); if(len < 0){ rc = 1010; goto ENDFUNC; } if(len < FRAME_SIZE || feof(inFp)){//这里的为什么要用循环? rc = 8 - (len % 8); for(int i=0;i= FRAME_SIZE) len = fread(in,1,FRAME_SIZE,inFp); else if(fileSize - ftell(inFp) - olen > 0){ offset = fileSize - ftell(inFp) - olen; len = fread(in,1,offset,inFp); } else break; if(len < 0){ rc = 2006; goto ENDFUNC; } if(len == 0) break; if(retType == 0){ //retType = 1; } else{ rc = fwrite(templates,1,retType,outFp); if(rc != retType){ rc = 2007; goto ENDFUNC; } } if(len % 8 != 0) { rc = 2008; goto ENDFUNC; } rc = MakeBodyUpdateX(in,len,out,&ctx); if(rc != 0){ rc = 2009; goto ENDFUNC; } //if(feof(inFp)) len = len - out[len-1]; retType = out[len-1]; //不明白为什么要减1呢? if(retType > 8 || retType < 0) retType = 8; // retType = 8 的意义何在? rc = fwrite(out,1,len-retType,outFp); if(rc != len-retType){ rc = 2010; goto ENDFUNC; } //memcpy(templates,out + len - retType,retType); 发送消息--展示进度 //rc = ((CProgressCtrl *)ccdlg->prgrsDlg.GetDlgItem(IDC_PROGRESS1))->GetPos( ); //if(rc < 0) rc =0; //((CProgressCtrl *)ccdlg->prgrsDlg.GetDlgItem(IDC_PROGRESS1))->SetPos(rc++); //((CProgressCtrl *)ccdlg->prgrsDlg.GetDlgItem(IDC_PROGRESS1))->StepIt(); /* double temp = FRAME_SIZE * currentPos * 10; temp = temp / fileSize; CString hhhh; hhhh.Format("%.0f%%",temp*10); ccdlg->SetWindowText(hhhh); if(temp > (pos + 1) ){ ((CProgressCtrl *)ccdlg->prgrsDlg.GetDlgItem(IDC_PROGRESS1))->StepIt(); pos+=1; } currentPos++;*/ } rc = MakeBodyFinalX(templates,&ctx); if(rc != 0){ rc = 2011; goto ENDFUNC; } rc = 0; ENDFUNC: if(inFp) fclose(inFp); if(outFp){ fclose(outFp);} return rc; #else return 0; #endif }

你可能感兴趣的:(c语言,openssl,加密,templates,struct,null,vector,buffer)