資源簡介
ECDH 密鑰交換算法程序,程序內含有調用大數(shù)求公鑰,私鑰求取,私鑰計算。

代碼片段和文件信息
//?ECDH.cpp?:?定義控制臺應用程序的入口點。
//
#include?“stdafx.h“
#include
#include????
#include???
#include????
#include???
#include???
#include
#define?ECDH_SIZE?67??
#define?MALLOC_SIZE?0x400u
#pragma?warning(disable:4996)
using?namespace?std;
/*hex轉bin*/
int?String2Buffer(unsigned?char*?src?int?srclen?unsigned?char*?dest)?{
int?i?=?0;
if?(srclen?%?2?!=?0)?return?0;
for?(i?=?0;?i? {
char?tmp[3];
tmp[0]?=?*(src?+?2?*?i);
tmp[1]?=?*(src?+?2?*?i?+?1);
tmp[2]?=?0;
int?out?=?0;
sscanf(tmp?“%x“?&out);
unsigned?char?ch?=?(unsigned?char)out;
*(dest?+?i)?=?ch;
}
return?i;
}
/*bin轉hex*/
int?Buffer2String(unsigned?char*?src?int?srclen?unsigned?char*?dest)?{
int?i;
for?(i?=?0;?i? {
char?tmp[3]?=?{?0?};
sprintf(tmp?“%x“?*(src?+?i));
if?(strlen(tmp)?==?1)?{
strcat((char*)dest?“0“);
strncat((char*)dest?tmp?1);
}
else?if?(strlen(tmp)?==?2)?{
strncat((char*)dest?tmp?2);
}
else?{
strcat((char*)dest?“00“);
}
}
return?i?*?2;
}
/*16進制展示數(shù)據(jù)*/
static?void?display(const?char?*tripstr?const?char?*src?const?int?src_size)
{
int?i?=?0;
if?(tripstr?!=?NULL)?{
printf(“%s“?tripstr);
}
unsigned?char*tmp?=?(unsigned?char*)malloc(MALLOC_SIZE);
memset(tmp?0?MALLOC_SIZE);
Buffer2String((unsigned?char*)src?src_size?tmp);
cout?< free(tmp);
}
static?int?Gen_ECDH_Key(string?*s1?string?*publickey?string?*privatekey?string?*sharedkey)?{
string?str1?=?*s1;
string?str2?=?*publickey;
string?str3?=?*privatekey;
bool?isNeed23?=?false;//是否需要第2?3個參數(shù)
if?(str1.length()?==?0)?{
//第一個參數(shù)不能空
printf(“第一個參數(shù)不能空\n“);
return?-0x10;
}
if?(isNeed23)?{
//檢測第23個參數(shù)
if?(str2.length()?==?0)?{
printf(“str2?null\n“);
return?-0x12;
}
if?(str3.length()?==?0)?{
printf(“str3?null\n“);
return?-0x13;
}
}
EC_KEY?*eckey?=?EC_KEY_new();
eckey?=?EC_KEY_new_by_curve_name(NID_secp192k1);//711
if?(!eckey)?{
printf(“eckey?null\n“);
return?-0x7;
}
if?(str3.length()?>?0)?{
//str3不為空
printf(“private?key!=null?gen?sharekey\n“);
BIGNUM?*big?=?BN_new();
unsigned?char?*sout3?=?(unsigned?char*)malloc(MALLOC_SIZE);
memset(sout3?0?MALLOC_SIZE);
int?len3?=?String2Buffer((unsigned?char*)str3.c_str()?str3.length()?sout3);
BN_mpi2bn(sout3?len3?big);
if?(!big)?{
printf(“big?null“);
return?-0x5;
}
char?*p?=?BN_bn2hex(big);
if?(p)
{
printf(“set?prikey?is?0x%s\n“?p);
OPENSSL_free(p);//釋放p
free(sout3);//釋放?sout3
}
int?r?=?EC_KEY_set_private_key(eckey?big);
printf(“r=%d\n“?r);
BN_free(big);//釋放
if?(r?!=?1)?{
printf(“EC_KEY_set_private_key?Failed~\n“);
return?-0x1;
}
const?EC_GROUP?*group?=?EC_KEY_get0_group(eckey);
EC_POINT?*point?=?EC_POINT_new(group);
if?(str2.length()?>?0)?{
unsigned?char?*st
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????60928??2018-06-21?08:29??ECDH\ECDH\Debug\ECDH.exe
?????文件?????693644??2018-06-21?08:29??ECDH\ECDH\Debug\ECDH.ilk
?????文件????1027072??2018-06-21?08:29??ECDH\ECDH\Debug\ECDH.pdb
?????文件????1114112??2011-03-15?19:46??ECDH\ECDH\Debug\libeay32.dll
?????文件???????2906??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\cl.command.1.tlog
?????文件??????43100??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\CL.read.1.tlog
?????文件???????1624??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\CL.write.1.tlog
?????文件????????406??2017-11-16?11:26??ECDH\ECDH\ECDH\Debug\ECDH.exe.em
?????文件????????472??2018-05-03?15:22??ECDH\ECDH\ECDH\Debug\ECDH.exe.em
?????文件????????381??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\ECDH.exe.intermediate.manifest
?????文件?????????91??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\ECDH.lastbuildstate
?????文件????????946??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\ECDH.log
?????文件?????157107??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\ECDH.obj
?????文件????1179648??2018-05-03?15:22??ECDH\ECDH\ECDH\Debug\ECDH.pch
?????文件????????198??2017-11-16?11:26??ECDH\ECDH\ECDH\Debug\ECDH_manifest.rc
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
?????文件??????????2??2018-06-21?08:29??ECDH\ECDH\ECDH\Debug\li
............此處省略37個文件信息
- 上一篇:基于STM32設計的無線遙控器
- 下一篇:一個仿豆瓣的網站
評論
共有 條評論