//
//
#if !defined(AFX_EC_CLASS_H__360A35F7_DB6B_4390_810E_FFEF59954245__INCLUDED_)
#define AFX_EC_CLASS_H__360A35F7_DB6B_4390_810E_FFEF59954245__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
class EC_Class
{
public:
//**********从文件中读取一个EC公钥***********//
int ReadPubEC(char *FP);
//***********向文件中写入一个EC公钥并编码**************//
int WritePubEC(char *mode, char *FP, char *name);
//*********转换EC秘钥为同一秘钥匙格式**********//
EVP_PKEY *GetEvpKeyPoint();
//**********从文件中读取一个EC私钥***********//
int ReadEC(char *FP);
//**********向文件中写入一个EC私钥并编码**************//
int WriteEC(char *mode,char *FP,char *name);
//***********生成EC秘钥***********//
int CreateEC(int keylen);
EC_Class();
virtual ~EC_Class();
private:
EC_KEY *m_ec;//定义一个EC结构体指针
};
#endif // !defined(AFX_EC_CLASS_H__360A35F7_DB6B_4390_810E_FFEF59954245__INCLUDED_)
// EC_Class.cpp: implementation of the EC_Class class.
//
//
#include "stdafx.h"
#include "MYCA.h"
#include "EC_Class.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include <string.h>
//
// Construction/Destruction
//
EC_Class::EC_Class()
{
m_ec=NULL;
}
EC_Class::~EC_Class()
{
if(m_ec!=NULL)
{
EC_KEY_free(m_ec);
}
}
int EC_Class::CreateEC(int keylen)
{
//**********初始化曲线结构体并选择曲线方程*********//
if(m_ec==NULL)
{
if(1024 == keylen)
{
if ((m_ec = EC_KEY_new_by_curve_name(NID_secp160r1)) == NULL)//NID_secp160r1--709,好像是160位,NID_secp224r1 是224位
{
return 0;
}
}
if(2048 == keylen)
{
if ((m_ec = EC_KEY_new_by_curve_name(NID_secp224r1)) == NULL)//NID_secp160r1--709,好像是160位,NID_secp224r1 是224位
{
return 0;
}
}
}
//*************生成秘钥*************//
if (!EC_KEY_generate_key(m_ec))
{
return 0;
}
return 1;
}
int EC_Class::WriteEC(char *mode, char *FP, char *name)
{
BIO * bkey;
CString path;
path=FP;
path=path+"\\"+name+"PriKey."+mode;
if((bkey = BIO_new_file(path, "w"))== NULL)
{
AfxMessageBox("open CAPriKey.der fail");
return 0;
}
if(strcmp(mode,"der")==0)
{
if (!i2d_ECPrivateKey_bio(bkey,m_ec))
{
AfxMessageBox("ECPrivateKey DER write bio fail");
return 0;
}
}
else
{
if (!PEM_write_bio_ECPrivateKey(bkey,m_ec,NULL,NULL, 6, 0, NULL))
{
AfxMessageBox("ECPrivateKey PEM write bio fail");
return 0;
}
}
BIO_free(bkey);
return 1;
}
int EC_Class::ReadEC(char *FP)
{
BIO * bkey;
int pri=0;
CString path;
//***********结构体指针初始化**********//
if(m_ec==NULL)
{
if ((m_ec = EC_KEY_new_by_curve_name(NID_secp160r1)) == NULL)
{
return 0;
}
}
//************尝试以PEM编码格式读取的私钥*************//
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
BIO_free(bkey);
return 0;
}
if (PEM_read_bio_ECPrivateKey(bkey,&m_ec,NULL,NULL))
{
pri=1;
}
BIO_free(bkey);
}
//**********如果PEM编码格式读取失败,则尝试以DER编码方式读取**********//
if(!pri)
{
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
BIO_free(bkey);
return 0;
}
if (d2i_ECPrivateKey_bio(bkey,&m_ec))
{
pri=1;
}
BIO_free(bkey);
}
}
return pri;
}
EVP_PKEY *EC_Class::GetEvpKeyPoint()
{
EVP_PKEY *EvpKey;
if ((EvpKey=EVP_PKEY_new()) == NULL)
{
return NULL;
}
int ret = EVP_PKEY_assign_EC_KEY(EvpKey,m_ec);//RSA结构转换成EVP_KEY结构
if(ret != 1)
return NULL;
return EvpKey;
}
int EC_Class::WritePubEC(char *mode, char *FP, char *name)
{
BIO * bkey;
CString path;
path=FP;
path=path+"\\"+name+"PubKey."+mode;
if((bkey = BIO_new_file(path, "w"))== NULL)
{
AfxMessageBox("open CAPriKey.der fail");
return 0;
}
if(strcmp(mode,"der")==0)
{
if (!i2d_EC_PUBKEY_bio(bkey,m_ec))
{
AfxMessageBox("ECPrivateKey DER write bio fail");
return 0;
}
}
else
{
if (!PEM_write_bio_EC_PUBKEY(bkey,m_ec))
{
AfxMessageBox("ECPrivateKey PEM write bio fail");
return 0;
}
}
BIO_free(bkey);
return 1;
}
int EC_Class::ReadPubEC(char *FP)
{
BIO * bkey;
int pri=0;
CString path;
if(m_ec==NULL)
{
if ((m_ec = EC_KEY_new_by_curve_name(NID_secp160r1)) == NULL)
{
return 0;
}
}
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
BIO_free(bkey);
return 0;
}
if (PEM_read_bio_EC_PUBKEY(bkey,&m_ec,NULL,NULL))
{
pri=1;
}
BIO_free(bkey);
}
if(!pri)
{
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
BIO_free(bkey);
return 0;
}
if (d2i_EC_PUBKEY_bio(bkey,&m_ec))
{
pri=1;
}
BIO_free(bkey);
}
}
return pri;
}