EC_Class

8 篇文章 0 订阅
// EC_Class.h: interface for the EC_Class class.
//
//


#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
转c#写法:#!/bin/sh time_stamp=`date +%s` function CheckStop() { if [ $? -ne 0 ]; then echo "execute fail, error on line_no:"$1" exit!!!" exit fi } function GenEcdsaKey() { ec_param_file_path="/tmp/ec_param.pem."$time_stamp openssl ecparam -out $ec_param_file_path -name prime256v1 -genkey CheckStop $LINENO openssl genpkey -paramfile $ec_param_file_path -out $1 CheckStop $LINENO openssl pkey -in $1 -inform PEM -out $2 -outform PEM -pubout CheckStop $LINENO rm $ec_param_file_path echo "gen_ecdsa_key succ prikey_path:"$1" pubkey_path:"$2 } function GenEcdsaSign() { ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl pkeyutl -sign -in $ec_sign_info_sha256 -out $ec_binary_sign_file -inkey $3 -keyform PEM CheckStop $LINENO openssl base64 -e -in $ec_binary_sign_file -out $4 CheckStop $LINENO rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file echo "gen_ecdsa_sign succ sign_file_path:"$4 } function VerifyEcdsaSign() { ec_sign_info_file="/tmp/ec_sign_info_file."$time_stamp ec_sign_info_sha256="/tmp/ec_sign_info_sha256."$time_stamp ec_binary_sign_file="/tmp/ec_binary_sign_file."$time_stamp echo -n "$1"_"$2" > $ec_sign_info_file openssl dgst -sha256 -binary -out $ec_sign_info_sha256 $ec_sign_info_file CheckStop $LINENO openssl base64 -d -in $4 -out $ec_binary_sign_file CheckStop $LINENO openssl pkeyutl -verify -in $ec_sign_info_sha256 -sigfile $ec_binary_sign_file -pubin -inkey $3 -keyform PEM rm $ec_sign_info_file $ec_sign_info_sha256 $ec_binary_sign_file } function Usage() { echo "Usage:" echo "mmiot_ecdsa_sign.sh gen_ecdsa_key <private_key_file_path> <public_key_file_path>" echo "mmiot_ecdsa_sign.sh gen_ecdsa_sign <product_id> <sn> <private_
最新发布
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值