//加密机制初始化
g_HDMgr.init();
//对方的public key
BIGNUM* peerPubKey = NULL;
peerPubKey = BN_bin2bn((unsigned char*)pRegist->DH_PubKey, pRegist->nDH_PubKey_Len, NULL);
if (peerPubKey != NULL)
{
//计算密钥
unsigned char* ClientSecret = NULL;
int nSecretSize = g_HDMgr.ComputeSecret(g_HDMgr.GetdhPriv(), peerPubKey, &ClientSecret); if (nSecretSize <= )
{
return false;
}
SHA256(ClientSecret, nSecretSize, key32);
strHex = DPC::PrintHexMsg((char*)g_HDMgr.ServerSecret, g_HDMgr.nServerSecret_Len);
gLog.LogText(LOG_INFO, "[%s][%s] \n", ENKEY, strHex.c_str());
}
/*---------------------------------------------------------------------------
Copyright (C) 2013 Bonson Info
文件 : HiredisConn.h
版本 : V1.0
时间 : 2018-08-30
作者 : lial@bonsoninfo.com
描述 : HD加密 控制者
---------------------------------------------------------------------------*/ #pragma once
#include <boost/serialization/singleton.hpp>
#include <openssl/dh.h>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/ossl_typ.h> #define USE_PREDEF_PARAMETERS 1
#define BUFFER_SIZE 4096
#define ADD 1
#define DES 0 #define g_HDMgr CDHMgr::get_mutable_instance()
class CDHMgr : public boost::serialization::singleton<CDHMgr>
{
public:
CDHMgr();
~CDHMgr();
public:
DH *dhPriv;
unsigned char* ServerSecret; // SiteCenter端密码摘要
int nServerSecret_Len; // SiteCenter端密码摘要长度
public:
void init();
int ComputeSecret(DH* dh, BIGNUM* peerPubKey, unsigned char** secret);
int PKCS7Pending(unsigned char* plaintext, int nDataLen, int nBlockSize);
inline unsigned char* GetServerSecret(){ return ServerSecret; };
inline DH * GetdhPriv(){ return dhPriv; };
};
#include "DHMrg.h"
#include "../PluginComm/clog.h" //*************************************************************************
// 函数名: InitDHParameters
// 参数: DH** dh
// 返回值: int
// 描述: 初始化Diffie-Hellman参数p 和 g
//*************************************************************************
int InitDHParameters(DH** dh)
{
#if USE_PREDEF_PARAMETERS
//使用预定义参数 p 和 g
const unsigned char p[] = {
0xcc, 0x0b, 0xea, 0x16, 0x42, 0xc6, 0x55, 0x9c, 0xef, 0x5d,
0x73, 0x51, 0xd8, 0x03, 0xe9, 0x90, 0x53, 0xdc, 0x27, 0x8e,
0x97, 0x22, 0x22, 0x67, 0xdf, 0xe1, 0xf3, 0x99, 0xcc, 0x55,
0xe0, 0xd0, 0x75, 0x6d, 0x51, 0x5e, 0x00, 0x25, 0xca, 0x1d,
0x5d, 0x01, 0xec, 0x54, 0xae, 0xb2, 0xe9, 0xca, 0xc3, 0xae,
0xb0, 0xc5, 0xa0, 0x2c, 0x53, 0xc5, 0x10, 0xe6, 0x4d, 0xe7,
0xba, 0xe8, 0x81, 0xb7, 0x76, 0x79, 0x80, 0xb0, 0x3c, 0xdd,
0xbf, 0x1a, 0x94, 0x04, 0x7a, 0x51, 0xca, 0xed, 0x89, 0x77,
0x88, 0xb1, 0xa3, 0x6c, 0xf6, 0x98, 0xba, 0x39, 0xf4, 0x7c,
0xa2, 0xd9, 0x7a, 0x94, 0x2a, 0x46, 0xcc, 0x9e, 0x7f, 0xd2,
0x27, 0x1e, 0xe6, 0x02, 0x5a, 0xd3, 0xa6, 0x38, 0x5c, 0x63,
0x7a, 0xab, 0xc0, 0x69, 0x74, 0x16, 0x92, 0x29, 0x94, 0xbe,
0x6b, 0xf3, 0x34, 0xd2, 0x36, 0x80, 0xde, 0xdc, 0x46, 0x17,
0xd0, 0xd0, 0x1a, 0x40, 0x51, 0xce, 0x00, 0x89, 0x93, 0x17,
0xc8, 0x4f, 0x11, 0x03, 0xc2, 0x67, 0x47, 0x01, 0x26, 0x63,
0xdd, 0xce, 0xb1, 0xba, 0x49, 0x8a, 0x50, 0x23, 0xd0, 0x3c,
0x62, 0x18, 0xb6, 0x0b, 0x5a, 0xe4, 0xc8, 0x4b, 0x96, 0xb0,
0xce, 0x5f, 0x7a, 0xbe, 0xcd, 0xe9, 0xc6, 0x08, 0x5b, 0x9a,
0x29, 0xd0, 0x7a, 0x44, 0xc9, 0x29, 0x0c, 0x55, 0x7e, 0xc1,
0x34, 0xc0, 0xdc, 0x1b, 0x64, 0x81, 0xd3, 0xa7, 0x36, 0x7a,
0x5b, 0x1e, 0xe9, 0x89, 0x5b, 0x26, 0x6b, 0xa8, 0x58, 0x86,
0x89, 0x3f, 0xc5, 0x87, 0x11, 0xac, 0x21, 0xdd, 0xb6, 0xc0,
0x7a, 0xbc, 0x4d, 0x2b, 0x86, 0xe8, 0x08, 0xc7, 0x3c, 0x34,
0xe6, 0xc3, 0xd7, 0x09, 0x6e, 0xed, 0x2c, 0xb0, 0x9e, 0x9b,
0x0f, 0xd8, 0xdc, 0x56, 0x6c, 0x7a, 0x80, 0x84, 0xc4, 0x17,
0x73, 0x25, 0xb4, 0xb6, 0x20, 0x2b
};
const unsigned char g[] = { };
*dh = DH_new(); BIGNUM *pb, *gb;
pb = BN_bin2bn(p, sizeof(p), NULL);
gb = BN_bin2bn(g, sizeof(g), NULL);
if (pb == NULL || gb == NULL)
{
goto __ERROR;
}
DH_set0_pqg(*dh, pb, NULL, gb); #else
//p和g可以通过客户端发送过来
#endif
return ; __ERROR:
DH_free(*dh);
*dh = NULL;
return -;
} CDHMgr::CDHMgr()
{
dhPriv = NULL;
ServerSecret = NULL;
} CDHMgr::~CDHMgr()
{ } //*************************************************************************
// 函数名: Init
// 返回值: void
// 描述: 初始化
//*************************************************************************
void CDHMgr::init()
{
//初始化参数
if ( != InitDHParameters(&dhPriv))
{
gLog.LogText(LOG_ERROR,
"%s: %s", __FUNCTION__, "DH InitDHParameters Fail");
_exit();
} // 生成随机数
if (DH_generate_key(dhPriv) <= )
{
gLog.LogText(LOG_ERROR,
"%s: %s\n", __FUNCTION__, "DH DH_generate_key Fail");
_exit();
} // 计算自己的公共密钥
const BIGNUM *pubKey, *PriKey;
DH_get0_key(dhPriv, &pubKey, &PriKey); ServerSecret = (unsigned char*)OPENSSL_malloc(BN_num_bytes(pubKey));
nServerSecret_Len = BN_bn2bin(pubKey, ServerSecret);
if (nServerSecret_Len <= )
{
gLog.LogText(LOG_ERROR,
"%s: %s", __FUNCTION__, "DH BN_bn2bin Fail");
_exit();
}
} //************************************
// Method: ComputeSecret
// FullName: ComputeSecret
// Access: public
// Returns: int
// Qualifier:
// Parameter: DH * dh -- 已经初始化过的DH指针
// Parameter: BIGNUM * peerPubKey -- 对方的public key
// Parameter: unsigned char * * secret -- 计算出来的密钥
//************************************
int CDHMgr::ComputeSecret(DH* dh, BIGNUM* peerPubKey, unsigned char** secret)
{
/* Compute the shared secret */
*secret = (unsigned char *)OPENSSL_malloc(DH_size(dh));
int nSecretSize = DH_compute_key(*secret, peerPubKey, dh);
if (nSecretSize < )
{
OPENSSL_free(*secret);
*secret = NULL;
}
return nSecretSize;
} /**********************************************************
函数名:PKCS7Pending
参数:unsigned char* plaintext --输入
参数:int nDataLen --字符长度
参数:int BlockSize --加密向量
返回值:int --0失败 1成功
说明:内容的长度
***********************************************************/
int CDHMgr::PKCS7Pending(unsigned char* plaintext, int nDataLen, int FLAG)
{
int len = -;
if (ADD == FLAG)
{
char T = (AES_BLOCK_SIZE - (nDataLen % AES_BLOCK_SIZE)); for (char i = ; i < T; ++i)
{
plaintext[nDataLen + i] = T;
}
len = (nDataLen + T);
}
else
{
if (AES_BLOCK_SIZE >= plaintext[nDataLen - ])
{
len = (nDataLen - plaintext[nDataLen - ]);
}
} return len;
}

openssl之DH(Diffie–Hellman)加密的更多相关文章

  1. 深入浅出Diffie–Hellman

    一.作者 这个密钥交换方法,由惠特菲尔德·迪菲(Bailey Whitfield Diffie).马丁·赫尔曼(Martin Edward Hellman)于1976年发表. 二.说明 它是一种安全协 ...

  2. 浅析Diffie–Hellman

    一.作者 这个密钥交换方法,由惠特菲尔德·迪菲(Bailey Whitfield Diffie).马丁·赫尔曼(Martin Edward Hellman)于1976年发表. 二.说明 它是一种安全协 ...

  3. DH密钥交换非对称加密

    迪菲-赫尔曼密钥交换(Diffie–Hellman key exchange,简称"D–H") 是一种安全协议. 它可以让双方在完全没有对方任何预先信息的条件下通过不安全信道建立起 ...

  4. 使用openssl库实现des,3des加密

    原文地址: 使用openssl库实现des,3des加密 主要是调整了一下格式,以及一些变量的类型,以解决在VC2008下无法编译通过的问题. #include <stdio.h> #in ...

  5. Diffie–Hellman key exchange

    General overview[edit]   Illustration of the idea behind Diffie–Hellman key exchange Diffie–Hellman ...

  6. openssl生成CA签署 及 加密解密基础

    openssl  生成私有CA 及签署证书 openssl 配置文件: /etc/pki/tls/openssl.cnf 1. 在openssl CA 服务器端生成私钥 cd /etc/pki/CA/ ...

  7. RSA非对称加密,使用OpenSSL生成证书,iOS加密,java解密

    最近换了一份工作,工作了大概一个多月了吧.差不多得有两个月没有更新博客了吧.在新公司自己写了一个iOS的比较通用的可以架构一个中型应用的不算是框架的一个结构,并已经投入使用.哈哈 说说文章标题的相关的 ...

  8. php使用openssl来实现RSA(非对称加密)

    使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和PHP的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...

  9. openssl与cryptoAPI交互AES加密解密

    继上次只有CryptoAPI的加密后,这次要实现openssl的了 动机:利用CryptoAPI制作windows的IE,火狐和chrome加密控件后,这次得加上与android的加密信息交互 先前有 ...

随机推荐

  1. response中文乱码问题

    1.要确定I代码的编码格式为UTF-8 2.乱码原因:浏览器和服务器的编码格式不同: 服务器的默认编码为:ISO-8859-1,如果浏览器的编码不是ISO-8859-1,就会出现乱码: public ...

  2. 关于W3Cschool定义的设计模式-常用的9种设计模式的介绍

    一.设计模式 tip:每种设计模式,其实都是为了更高效的,更方便的解决在面对对象编程中所遇到的问题. 什么是设计模式:     是一套经过反复使用.多人知晓的.经过分类的.代码设计经验的总结   为什 ...

  3. 11-常用SQL总结

    1.设置表的列不能为nullalter table run.dbo.T1 alter column Col1 int not null 2.给表添加主键alter table run.dbo.T1 a ...

  4. Linux初识之Centos7中terminal光标位置偏移问题的解决

    新安装的centos7打开terminal发现光标位置向右偏移,使用起来影响感官,经查询后找到类似情况并顺利解决问题,特记录解决过程以作参考. 1.未解决时光标向右偏移显示: 2.打开设置(Setti ...

  5. Spring入门(十四):Spring MVC控制器的2种测试方法

    作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...

  6. 程序员接触新语言————hello world ^-^,web3种样式表

    我的第一个网页 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  7. hbase、pig、hive配置与应用

    ------------------HBASE---------- [root@iClient~]#sudo yum install hbase #iClient安装Hbase客户端 [root@cM ...

  8. frp 端口映射

    简介 frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp 协议,为 http 和 https 应用协议提供了额外的能力,且尝试性支持了点对点穿透. 场景 利用处于内网或防火墙后 ...

  9. 【SQL server初级】数据库性能优化二:数据库表优化

    数据库优化包含以下三部分,数据库自身的优化,数据库表优化,程序操作优化.此文为第二部分 数据库性能优化二:数据库表优化 优化①:设计规范化表,消除数据冗余 数据库范式是确保数据库结构合理,满足各种查询 ...

  10. restapi(7)- 谈谈函数式编程的思维模式和习惯

    国庆前,参与了一个c# .net 项目,真正重新体验了一把搬砖感觉:在一个多月时间好像不加任何思考,不断敲键盘加代码.我想,这也许是行业内大部分中小型公司程序猿的真实写照:都是坐在电脑前的搬砖工人.不 ...