RSA_Class

来源:互联网 发布:final php 关键字 编辑:程序博客网 时间:2024/05/03 06:45
// RSA_Class.h: interface for the RSA_Class class.
//
//////////////////////////////////////////////////////////////////////
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
 
#if !defined(AFX_RSA_CLASS_H__87231EB9_9932_4721_8EC0_FB8A955D4713__INCLUDED_)
#define AFX_RSA_CLASS_H__87231EB9_9932_4721_8EC0_FB8A955D4713__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class RSA_Class  
{
public:
//**********从文件中读取一个RSA公钥***********//
int ReadPubRSA(char *FP/*文件名*/);


//***********向文件中写入一个RSA公钥并编码**************//
int WritePubRSA(char *mode,/*编码方式*/
char *FP,/*文件存放路径*/
char *name);/*文件名*/

//*********转换RSA秘钥为同一秘钥匙格式**********//
EVP_PKEY *GetEvpKeyPoint();


//**********得到RSA结构的拷贝**********//
RSA GetRsa();

//**********得到RSA结构的指针**********//
RSA *GetRsaPoint();


//**********从文件中读取一个RSA私钥***********//
int ReadRSA(char *FP/*文件名*/);


//***********向文件中写入一个RSA公钥并编码**************//
int WriteRSA(char *mode,/*编码方式*/
char *FP,/*文件存放路径*/
char *name);/*文件名*/
RSA_Class();
virtual ~RSA_Class();


//***********生成RSA秘钥***********//
int CreateRSA(int keylen/*文件长度*/);


private:
RSA *m_rsa;
};


#endif // !defined(AFX_RSA_CLASS_H__87231EB9_9932_4721_8EC0_FB8A955D4713__INCLUDED_)




// RSA_Class.cpp: implementation of the RSA_Class class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RSA_Class.h"
#include "MYCA.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


RSA_Class::RSA_Class()
{
m_rsa=NULL;
//m_EvpKey=NULL;
}


RSA_Class::~RSA_Class()
{
RSA_free(m_rsa);


}




//RSA_3
int RSA_Class::CreateRSA(int keylen)
{
//http://blog.sina.com.cn/s/blog_4fcd1ea30100sj08.html
//***********结构体指针初始化**********//
if((m_rsa=RSA_new())==NULL)
{
AfxMessageBox("RSA new fail");
}

//*************产生随即数***********//
BIGNUM *bne;
// if((bne=BN_new())==NULL||!BN_set_word(bne,0x10001))
if((bne=BN_new())==NULL||!BN_set_word(bne,RSA_F4))
{
AfxMessageBox("BN new or BN set word fail");
return 0;
}


//*****************生成秘钥匙***************//
if(!RSA_generate_key_ex(m_rsa,keylen,bne,NULL))
{
AfxMessageBox("RSA generate fail");
return 0;
}
return 1;
}


int RSA_Class::WriteRSA(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)
/*
http://www.newsmth.net/bbsanc.php?path=%2Fgroups%2Fcomp.faq%2FSecurity%2F20050418%2F1025-2048%2FM.1113837112.v%3D
BIO *BIO_new_file(const char *filename, const char *mode);
*/
 {
AfxMessageBox("open CAPriKey.der fail");
return 0;
}
//***********以DER编码格式向文件中写入私钥***********//
if(strcmp(mode,"der")==0)
{
if (!i2d_RSAPrivateKey_bio(bkey,m_rsa))
//***********!i2d_RSAPrivateKey_bio***********//
//  http://bbs.chinaunix.net/thread-1860492-1-1.html
{
AfxMessageBox("RSAPrivateKey DER write bio fail");
return 0;
}
}
//***********以PEM编码格式向文件中写入私钥***********//
else
{
if (!PEM_write_bio_RSAPrivateKey(bkey,m_rsa,NULL,NULL, 6, 0, NULL))
{
AfxMessageBox("RSAPrivateKey PEM write bio fail");
return 0;
}
}
BIO_free(bkey);
return 1;
}




int RSA_Class::WritePubRSA(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 CAPubKey.der fail");//原文:AfxMessageBox("open CAPriKey.der fail");
return 0;
}
//***********以DER编码格式向文件中写入私钥***********//
if(strcmp(mode,"der")==0)
{
if (!i2d_RSAPublicKey_bio(bkey,m_rsa))
{
AfxMessageBox("RSAPublicKey DER write bio fail");
return 0;
}
}
//***********以PEM编码格式向文件中写入私钥***********//
else
{
if (!PEM_write_bio_RSAPublicKey(bkey,m_rsa))
{
AfxMessageBox("RSAPublicKey PEM write bio fail");
return 0;
}
}
BIO_free(bkey);
return 1;
}


int RSA_Class::ReadRSA(char *FP)
{
BIO * bkey;
int pri=0;
CString path;
//***********结构体指针初始化**********//
if(m_rsa==NULL)
{
if((m_rsa=RSA_new())==NULL)
{
AfxMessageBox("RSA new fail");
return 0;
}
}
//************尝试以PEM编码格式读取的私钥*************//
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
return 0;
}
if (PEM_read_bio_RSAPrivateKey(bkey,&m_rsa,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");
return 0;
}
if (d2i_RSAPrivateKey_bio(bkey,&m_rsa))
{
pri=1;
}

BIO_free(bkey);
}
}
return pri;
}


RSA *RSA_Class::GetRsaPoint()
{
RSA *rsa;
if((rsa=RSA_new())==NULL)
{
AfxMessageBox("RSA new fail");
}
*rsa=*m_rsa;
return rsa;
}


RSA RSA_Class::GetRsa()
{
return *m_rsa;
}


EVP_PKEY *RSA_Class::GetEvpKeyPoint()
{
RSA *rsa;
if((rsa=RSA_new())==NULL)
{
AfxMessageBox("RSA new fail");
return NULL;
}
*rsa=*m_rsa;
EVP_PKEY *EvpKey;
if ((EvpKey=EVP_PKEY_new()) == NULL) 
{
return NULL;
}
int ret = EVP_PKEY_assign_RSA(EvpKey,rsa);//RSA结构转换成EVP_KEY结构
if(ret != 1) 
return NULL;
return EvpKey;
}










int RSA_Class::ReadPubRSA(char *FP)
{
BIO * bkey;
int pub=0;
CString path;
if(m_rsa==NULL)
{
if((m_rsa=RSA_new())==NULL)
{
AfxMessageBox("RSA new fail");
return 0;
}
}
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
return 0;
}
if (PEM_read_bio_RSAPublicKey(bkey,&m_rsa,NULL,NULL))
{
pub=1;
}
BIO_free(bkey);
}


if(!pub)
{
if(FP!=NULL)
{
path=FP;
if((bkey = BIO_new_file(path, "r"))== NULL)
{
AfxMessageBox("open "+path+" fail");
return 0;
}
if (d2i_RSAPublicKey_bio(bkey,&m_rsa))
{
pub=1;
}

BIO_free(bkey);
}
}
return pub;
}

原创粉丝点击