#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include "openssl/sha.h"
#include "openssl/sha.h"
#include <cstring>
#include "rsa_codec.h"
using namespace std;
using namespace unp::crypto;
const char* CRSACodec::GetErrMsg(){
return m_sErrMsg.c_str();
}
int CRSACodec::Pubilc_Decode
(
const std::string& sKeyFileName,
const std::string& sSrcData,
std::string& sResult,
int iPadding
)
{
FILE* pFile = NULL;
RSA* pRsa = NULL;
unsigned char* pSigbuf = NULL;
sResult = "";
bool bIfSuccess = false;
do
{
char szErrMsg[1024] = {0};
// 检查密钥文件是否存在
pFile = fopen(sKeyFileName.c_str(), "r");
if(pFile == NULL){
snprintf(szErrMsg, sizeof(szErrMsg), "Key File[%s] Not Exist!!!", sKeyFileName.c_str() );
m_sErrMsg = szErrMsg;
break;
}
// 取出密钥
pRsa = PEM_read_RSA_PUBKEY(pFile, NULL, NULL, NULL);
if (pRsa == NULL){
m_sErrMsg = "PEM_read_RSA_PUBKEY FAIL:Key is NULL!!!";
break;
}
int iSiglen = RSA_size(pRsa);
pSigbuf = (unsigned char *) malloc (iSiglen + 1);
int iRet = RSA_public_decrypt(sSrcData.size(),
(unsigned char *)sSrcData.c_str(),
pSigbuf,
pRsa,
iPadding);
if (iRet <= 0){
// 解密失败
ERR_load_ERR_strings();
ERR_load_crypto_strings();
unsigned long ulErr = ERR_get_error();
char szRSAErrMsg[1024] = {0};
char *pTmp = NULL;
pTmp = ERR_error_string(ulErr,szRSAErrMsg);
snprintf(szErrMsg, sizeof(szErrMsg), "RSA_public_decrypt FAIL!!!iRet:[%d],errMsg:[%s]", iRet,szRSAErrMsg );
m_sErrMsg = szErrMsg;
break;
}
// 解密成功,赋值
sResult = std::string((char*)pSigbuf,iRet);
bIfSuccess = true;
}while(0);
if(pFile != NULL){
fclose(pFile);
pFile = NULL;
}
if(pSigbuf != NULL){
free(pSigbuf);
pSigbuf = NULL;
}
if(pRsa != NULL){
RSA_free(pRsa);
pRsa = NULL;
}
if(!bIfSuccess){
return -1;
}
return 0;
}
int CRSACodec::Private_Encode
(
const std::string& sKeyFileName,
const std::string& sSrcData,
std::string& sResult,
int iPadding
)
{
FILE* pFile = NULL;
RSA* pRsa = NULL;
unsigned char* pSigbuf = NULL;
sResult = "";
bool bIfSuccess = false;
do
{
char szErrMsg[1024] = {0};
// 检查密钥文件是否存在
pFile = fopen(sKeyFileName.c_str(), "r");
if(pFile == NULL)
{
snprintf(szErrMsg, sizeof(szErrMsg), "Key File[%s] Not Exist!!!", sKeyFileName.c_str() );
m_sErrMsg = szErrMsg;
break;
}
// 取出密钥 -----BEGIN RSA PRIVATE KEY-----
pRsa = PEM_read_RSAPrivateKey(pFile, NULL, NULL, NULL);
if (pRsa == NULL)
{
m_sErrMsg = "PEM_read_RSAPrivateKey FAIL:Key is NULL!!!";
break;
}
int iSiglen = RSA_size(pRsa);
pSigbuf = (unsigned char *) malloc (iSiglen + 1);
int iRet = RSA_private_encrypt(sSrcData.size(),
(unsigned char *)sSrcData.c_str(),
pSigbuf,
pRsa,
iPadding);
if(iRet <= 0)
{
// 加密失败
ERR_load_ERR_strings();
ERR_load_crypto_strings();
unsigned long ulErr = ERR_get_error();
char szRSAErrMsg[1024] = {0};
char *pTmp = NULL;
pTmp = ERR_error_string(ulErr,szRSAErrMsg);
snprintf(szErrMsg, sizeof(szErrMsg), "RSA_private_encrypt FAIL!!!iRet:[%d],errMsg:[%s]", iRet, szRSAErrMsg );
m_sErrMsg = szErrMsg;
break;
}
// 加密成功,赋值
sResult = std::string((char*)pSigbuf, iRet);
bIfSuccess = true;
} while(0);
if(pFile != NULL)
{
fclose(pFile);
pFile = NULL;
}
if(pSigbuf != NULL)
{
free(pSigbuf);
pSigbuf = NULL;
}
if(pRsa != NULL)
{
RSA_free(pRsa);
pRsa = NULL;
}
if(!bIfSuccess)
{
return -1;
}
return 0;
}
// ---- sha256摘要哈希 ---- //
void CRSACodec::sha256
(
const std::string &srcStr,
std::string &encodedStr,
std::string &encodedHexStr
)
{
// 调用sha256哈希
unsigned char mdStr[33] = {0};
SHA256((const unsigned char *)srcStr.c_str(), srcStr.length(), mdStr);
// 哈希后的字符串
encodedStr = std::string((const char *)mdStr);
// 哈希后的十六进制串 64字节
char buf[65] = {0};
char tmp[3] = {0};
for (int i = 0; i < 32; i++)
{
sprintf(tmp, "%02x", mdStr[i]);
strcat(buf, tmp);
}
buf[64] = '\0'; // 后面都是0,从64字节截断
encodedHexStr = std::string(buf);
}