?? evp.cpp
字號:
goto err;
}
pcert=X509_get_pubkey(x509);
if (pcert == NULL)
{
sprintf(outMsg,"Read Public Key Failed!");
ret=false;
goto err;
}
md = EVP_get_digestbyname(mdname);
if(!md)
{
sprintf(outMsg,"Unknown message digest %s",mdname);
ret=false;
goto err;
}
EVP_MD_CTX_init(&md_ctx);
if(!EVP_VerifyInit_ex(&md_ctx,md,NULL))
{
strcpy(outMsg,"初始化算法結構出錯");
ret=false;
goto err;
}
for(;;)
{
len = fread(inbuf, sizeof(char), 1024, infd);
if(len <= 0) break;
if(!EVP_VerifyUpdate (&md_ctx, inbuf, len))
{
strcpy(outMsg,"中間過程出錯");
ret=false;
goto err;
}
finishLen+=len;
if(DrawProg) DrawProg(finishLen*100/fileLen);
}
sig_len =EVP_PKEY_size(pcert);//這里應該改動
sig_buf=new unsigned char[sig_len];
fread(sig_buf,sizeof(char),sig_len,outfd);
if(!EVP_VerifyFinal (&md_ctx, sig_buf, sig_len, pcert))
{
strcpy(outMsg,"最終過程出錯");
ret=false;
}
err:
fclose(infd);
fclose(outfd);
if(pcert) EVP_PKEY_free (pcert);
if(x509) X509_free(x509);
if(md_ctx.digest) EVP_MD_CTX_cleanup(&md_ctx);
delete [] sig_buf;
EVP_cleanup();
return ret;
}
/*數字信封*/
bool Seal(char * cert/*公鑰*/,int certlen,char * cpname/*算法名稱*/,char * filein/*輸入文件*/,
char * fileout/*輸出文件*/,char * outMsg,PDrawProg DrawProg/*回調函數*/)//寫信
{
//對隨機數播種(seeded)。
unsigned char iv[EVP_MAX_IV_LENGTH]="";
unsigned char *ekey[1];
unsigned char buf[512]="";
unsigned char ebuf[512]="";
int readlen=0,ekeylen=0;
int ebuflen;
EVP_CIPHER_CTX ectx;
memset(&ectx,0,sizeof(ectx));
// int net_ekeylen;
EVP_PKEY *pubKey[1];
X509 * x509=NULL;
const EVP_CIPHER *cipher=NULL;
bool ret=true;
FILE *outfd,*infd;
double fileLen=0;//文件長度
double finishLen=0;//完成長度
if(strlen(filein)==0||strlen(fileout)==0)
{
strcpy(outMsg,"NO specify input or output file");
return false;
}
if ((infd = fopen (filein, "rb")) == NULL)//原文
{
strcpy(outMsg,"open input file error");
return false;
}
if ((outfd = fopen (fileout, "wb")) == NULL)//密文
{
strcpy(outMsg,"open output file error");
fclose(infd);
return false;
}
fileLen=filelength(fileno(infd));//得到文件長度
OpenSSL_add_all_algorithms();
x509=LoadCert(cert,certlen,outMsg);
if (x509 == NULL)
{
ret=false;
goto err;
}
pubKey[0]=X509_get_pubkey(x509);
if (pubKey[0] == NULL)
{
sprintf(outMsg,"Read Public Key Failed!");
ret=false;
goto err;
}
ekey[0] =new unsigned char[EVP_PKEY_size(pubKey[0])];
cipher=EVP_get_cipherbyname(cpname);
if(cipher==NULL)
{
sprintf(outMsg,"Unknown cipher name %s\n",cpname);
ret=false;
goto err;
}
RAND_bytes(iv,EVP_MAX_IV_LENGTH);//產生隨機數種子
//初始化一個加密算法結構EVP_CIPHER_CTX
//允許使用多把 public keys,到時後要 Open 時僅需
// 其中一把 public key 對應的 private key 即可
if(!EVP_SealInit(&ectx,cipher,
ekey,//可以有好幾把隨機密鑰
&ekeylen,//存放所有 ek 長度
iv,//自動生成
pubKey,//加密 ek 用的 public key(s)
1/*共有多少把 public key*/))
{
ret=false;
goto err;
}
// 保存 ekeylen, ekey[0] 與 iv,這些是解密時所需的。
// htonl() 轉換,以避免跨平臺時所遭遇到的 endian 問題。
//net_ekeylen = htonl(ekeylen);
fwrite((char*)&ekeylen,sizeof(char), sizeof(ekeylen),outfd);
fwrite(ekey[0],sizeof(char) ,ekeylen,outfd);
fwrite(iv,sizeof(char),sizeof(iv),outfd);
for(;;)
{
readlen = fread(buf, sizeof(char),sizeof(buf),infd);
if (readlen <= 0)
{
break;
}
if(!EVP_SealUpdate(&ectx, ebuf, &ebuflen, buf, readlen))
{
strcpy(outMsg,"中間過程出錯");
ret=false;
goto err;
}
fwrite(ebuf,sizeof(char),ebuflen,outfd);
finishLen+=readlen;
if(DrawProg) DrawProg(finishLen*100/fileLen);
}
if(!EVP_SealFinal(&ectx, ebuf, &ebuflen))
{
strcpy(outMsg,"中間過程出錯");
ret=false;
goto err;
}
fwrite(ebuf, sizeof(char), ebuflen,outfd);
err:
fclose(infd);
fclose(outfd);
if(pubKey[0]) EVP_PKEY_free(pubKey[0]);
if(ekey[0]) delete [] ekey[0];
if(x509) X509_free(x509);
if(ectx.cipher) EVP_CIPHER_CTX_cleanup(&ectx);
EVP_cleanup();
return ret;
}
/*拆封數字信封*/
bool OpenSeal(char * key/*私鑰*/,int keylen,char * cpname/*算法名稱*/,char * filein/*輸入文件*/,
char * fileout/*輸出文件*/,char * outMsg,PDrawProg DrawProg/*回調函數*/)
{
unsigned char buf[512]="";
unsigned char ebuf[512]="";
unsigned char iv[EVP_MAX_IV_LENGTH]="";
unsigned char *encryptKey=NULL;
unsigned int ekeylen=0;
const EVP_CIPHER *cipher=NULL;
int ebuflen=0,readlen=0;
bool ret=true;
FILE *outfd,*infd;
double fileLen=0;//文件長度
double finishLen=0;//完成長度
EVP_CIPHER_CTX ectx;
memset(&ectx,0,sizeof(ectx));
EVP_PKEY * pkey=NULL;
if(strlen(filein)==0||strlen(fileout)==0)
{
strcpy(outMsg,"NO specify input or output file");
return false;
}
if ((infd = fopen (filein, "rb")) == NULL)//原文
{
strcpy(outMsg,"open input file error");
return false;
}
if ((outfd = fopen (fileout, "wb")) == NULL)//密文
{
strcpy(outMsg,"open output file error");
fclose(infd);
return false;
}
fileLen=filelength(fileno(infd));//得到文件長度
OpenSSL_add_all_algorithms();//digests and ciphers
/* Read private key */
pkey=LoadKey(key,keylen,NULL,outMsg);
if (pkey == NULL)
{
ret=false;
goto err;
}
readlen=fread(&ekeylen, sizeof(char),sizeof(ekeylen),infd);
finishLen+=readlen;
/// ekeylen = ntohl(ekeylen);
if (ekeylen !=(unsigned ) EVP_PKEY_size(pkey))
{
sprintf(outMsg,"keylength mismatch");
ret=false;
goto err;
}
encryptKey =new unsigned char[sizeof(char) * ekeylen];
if (!encryptKey)
{
strcpy(outMsg,"內存分配錯誤");
ret=false;
goto err;
}
readlen=fread(encryptKey,sizeof(char), ekeylen,infd);
finishLen+=readlen;
readlen=fread(iv,sizeof(char),sizeof(iv),infd);
finishLen+=readlen;
cipher=EVP_get_cipherbyname(cpname);
if(cipher==NULL)
{
sprintf(outMsg,"Unknown cipher name %s\n",cpname);
ret=false;
goto err;
}
if(!EVP_OpenInit(&ectx,cipher, encryptKey,ekeylen,iv,pkey))
{
sprintf(outMsg,"初始化錯誤");
ret=false;
goto err;
}
for(;;)
{
readlen = fread(buf, sizeof(char),sizeof(buf),infd);
if (readlen <= 0)
{
break;
}
if(!EVP_OpenUpdate(&ectx, ebuf, &ebuflen, buf, readlen))
{
sprintf(outMsg,"中間過程錯誤");
ret=false;
goto err;
}
fwrite(ebuf,sizeof(char),ebuflen,outfd);
finishLen+=readlen;
if(DrawProg) DrawProg(finishLen*100/fileLen);
}
if(!EVP_OpenFinal(&ectx, ebuf, &ebuflen))
{
sprintf(outMsg,"最終過程錯誤");
ret=false;
goto err;
}
fwrite(ebuf, sizeof(char), ebuflen,outfd);
err:
fclose(infd);
fclose(outfd);
if(pkey) EVP_PKEY_free(pkey);
if((&ectx)->cipher) EVP_CIPHER_CTX_cleanup(&ectx);
delete[] encryptKey;
return ret;
}
//公鑰加密
bool RSAEnc(char * cert/*公鑰*/,int certlen,char * filein/*輸入文件*/,
char * fileout/*輸出文件*/,char * outMsg,PDrawProg DrawProg/*回調函數*/)//Rsa加密
{
int iblock_size, oblock_size;
unsigned char * bufin=NULL,* bufout=NULL;
int inlen=0,outlen=0;
bool ret=true;
double fileLen=0;//文件長度
double finishLen=0;//完成長度
X509 * x509=NULL;
EVP_PKEY *pcert=NULL;
RSA * rsa=NULL;
FILE *outfd,*infd;
if(strlen(filein)==0||strlen(fileout)==0)
{
strcpy(outMsg,"NO specify input or output file");
return false;
}
if ((infd = fopen (filein, "rb")) == NULL)//原文
{
strcpy(outMsg,"open input file error");
return false;
}
if ((outfd = fopen (fileout, "wb")) == NULL)//密文
{
strcpy(outMsg,"open output file error");
fclose(infd);
return false;
}
fileLen=filelength(fileno(infd));//得到文件長度
x509=LoadCert(cert,certlen,outMsg);
if (x509 == NULL)
{
ret=false;
goto err;
}
pcert = X509_get_pubkey(x509);
if(pcert==NULL)
{
sprintf(outMsg,"Get pkey Failed!");
ret=false;
goto err;
}
if (!(rsa = EVP_PKEY_get1_RSA(pcert)))
{
sprintf(outMsg,"Get rsa Failed!");
ret=false;
goto err;
}
oblock_size = BN_num_bytes(rsa->n);//加密后長度,128
iblock_size = BN_num_bytes(rsa->n) - 11;//預加密長度,117
bufin=new unsigned char[iblock_size];
bufout=new unsigned char[oblock_size];
for(;;)
{
inlen=fread(bufin,sizeof(char),iblock_size,infd);
if(!inlen)
break;
outlen=RSA_public_encrypt(inlen,bufin,bufout,rsa,RSA_PKCS1_PADDING);
if (outlen == -1)//加密后資料長度
{
sprintf(outMsg,"unable to do RSA encryption");
ret=false;
goto err;
}
fwrite(bufout,sizeof(char),outlen,outfd);
memset(bufout,0,outlen);
finishLen+=inlen;
if(DrawProg) DrawProg(finishLen*100/fileLen);
}
err:
fclose(infd);
fclose(outfd);
if(pcert) EVP_PKEY_free(pcert);
if(x509) X509_free(x509);
if(rsa) RSA_free(rsa);
delete [] bufin;
delete [] bufout;
return true;
}
//私鑰解密
int RSADec(char * key,int keylen,char * filein/*輸入文件*/,
char * fileout/*輸出文件*/,char * outMsg,PDrawProg DrawProg/*回調函數*/)
{
unsigned char * bufin=NULL,* bufout=NULL;
int iblock_size=0,oblock_size=0,outlen=0,inlen=0;
bool ret=true;
double fileLen=0;//文件長度
double finishLen=0;//完成長度
RSA * rsa=NULL;
EVP_PKEY * pkey=NULL;
FILE *outfd,*infd;
if(strlen(filein)==0||strlen(fileout)==0)
{
strcpy(outMsg,"NO specify input or output file");
return false;
}
if ((infd = fopen (filein, "rb")) == NULL)//原文
{
strcpy(outMsg,"open input file error");
return false;
}
if ((outfd = fopen (fileout, "wb")) == NULL)//密文
{
strcpy(outMsg,"open output file error");
fclose(infd);
return false;
}
fileLen=filelength(fileno(infd));//得到文件長度
pkey=LoadKey(key,keylen,NULL,outMsg);
if (pkey == NULL)
{
ret=false;
goto err;
}
if (!(rsa = EVP_PKEY_get1_RSA(pkey)))
{
sprintf(outMsg,"Get rsa Failed!");
ret=false;
goto err;
}
iblock_size = BN_num_bytes(rsa->n);//預接密長度 128
oblock_size = BN_num_bytes(rsa->n) - 11;//杰密后長度 117
bufin=new unsigned char[iblock_size];
bufout=new unsigned char[oblock_size];
for(;;)
{
inlen=fread(bufin,sizeof(char),iblock_size,infd);
if(!inlen)
break;//117,128
outlen=RSA_private_decrypt(inlen,bufin,bufout,rsa,RSA_PKCS1_PADDING);
if (outlen == -1)//加密后資料長度
{
sprintf(outMsg,"unable to do RSA encryption");
ret=false;
goto err;
}
fwrite(bufout,sizeof(char),outlen,outfd);
memset(bufout,0,oblock_size);
finishLen+=inlen;
if(DrawProg) DrawProg(finishLen*100/fileLen);
}
err:
if(pkey) EVP_PKEY_free(pkey);
fclose(infd);
fclose(outfd);
if(rsa) RSA_free(rsa);
delete [] bufin;
delete [] bufout;
return ret;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -