SM2算法第十四篇:ECDH秘钥交换的C程序
2016-05-19 16:28
1366 查看
参考:ECDH秘钥交换的C程序
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/02/00fd91bb72d5cc8bd41cc18215ff6919)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/02/dacb4715c537bb1b2082435ad179e972)
出错原因在“ 解决error LNK2001 unresolved external symbol”一文中说得很清楚,缺少必要的库,添加库的方法为:工程—>设置—>连接—>对象/模块库—>添加ssleay32.lib
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/02/5204f03fc7a3431e9cafb59bd7c3ae99)
在组建(build),成功了,运行(Ctrl+F5),结果如下
ECDH秘钥交换与SM2秘钥交换类似,下面是我从网上下到的ECDH秘钥交换的C程序
#include <openssl/ssl.h> #define ECDH_SIZE 33 void handleErrors() { printf("Error occurred.\n"); } static void disp(const char *str, const void *pbuf, const int size) { int i=0; if(str != NULL){ printf("%s:\n", str); } if(pbuf != NULL && size > 0){ for(i=0;i<size;i++) printf("%02x ", *((unsigned char *)pbuf+i)); putchar('\n'); } putchar('\n'); } int main() { /* alice */ EC_KEY *ecdh = EC_KEY_new(); EC_POINT *point = NULL; EC_POINT *point2c; EC_GROUP *group; unsigned char pubkey[ECDH_SIZE]; unsigned char shared[ECDH_SIZE]; int len; //Generate Public ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1 EC_KEY_generate_key(ecdh); point = EC_KEY_get0_public_key(ecdh); group = EC_KEY_get0_group(ecdh); if(0 == (len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL))) handleErrors(); printf("len=%d\n",len); /* bob */ EC_KEY *ecdh2 = EC_KEY_new(); EC_POINT *point2 = NULL; EC_POINT *pointc; EC_GROUP *group2; unsigned char pubkey2[ECDH_SIZE]; unsigned char shared2[ECDH_SIZE]; int len2; //Generate Public ecdh2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1 EC_KEY_generate_key(ecdh2); point2 = EC_KEY_get0_public_key(ecdh2); group2 = EC_KEY_get0_group(ecdh2); if(0 == (len2 = EC_POINT_point2oct(group2, point2, POINT_CONVERSION_COMPRESSED, pubkey2, ECDH_SIZE, NULL))) handleErrors(); printf("len2=%d\n",len); /* alice */ //ComputeKey point2c = EC_POINT_new(group); EC_POINT_oct2point(group, point2c, pubkey2, ECDH_SIZE, NULL); if (0 != EC_POINT_cmp(group, point2, point2c, NULL)) handleErrors(); if(0 == (len = ECDH_compute_key(shared, ECDH_SIZE, point2c, ecdh, NULL))) handleErrors(); printf("len=%d\n",len); disp("shared", shared, len); /* bob */ //ComputeKey pointc = EC_POINT_new(group2); EC_POINT_oct2point(group2, pointc, pubkey, ECDH_SIZE, NULL); if (0 != EC_POINT_cmp(group2, point, pointc, NULL)) handleErrors(); if(0 == (len2 = ECDH_compute_key(shared2, ECDH_SIZE, pointc, ecdh2, NULL))) handleErrors(); printf("len2=%d\n",len2); disp("shared2", shared2, len2); /* alice */ EC_POINT_free(pointc); EC_KEY_free(ecdh); /* bob */ EC_POINT_free(point2c); EC_KEY_free(ecdh2); printf("To the end\n"); return 0; }
在VC++ 6中编译(compile),报错信息如下
出错原因在这篇博客“容易混淆的const”一文中说得很清楚,据此解决方案就是将34、35、52、53的右侧表达式进行强制类型转换,经编译没有报错,修改后完整代码如下
#include <openssl/ssl.h> #define ECDH_SIZE 33 void handleErrors() { printf("Error occurred.\n"); } static void disp(const char *str, const void *pbuf, const int size) { int i=0; if(str != NULL){ printf("%s:\n", str); } if(pbuf != NULL && size > 0){ for(i=0;i<size;i++) printf("%02x ", *((unsigned char *)pbuf+i)); putchar('\n'); } putchar('\n'); } int main() { /* alice */ EC_KEY *ecdh = EC_KEY_new(); EC_POINT *point = NULL; EC_POINT *point2c; EC_GROUP *group; unsigned char pubkey[ECDH_SIZE]; unsigned char shared[ECDH_SIZE]; int len; //Generate Public ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1 EC_KEY_generate_key(ecdh); point = (struct ec_point_st *)EC_KEY_get0_public_key(ecdh); group = (struct ec_group_st *)EC_KEY_get0_group(ecdh); if(0 == (len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL))) handleErrors(); printf("len=%d\n",len); /* bob */ EC_KEY *ecdh2 = EC_KEY_new(); EC_POINT *point2 = NULL; EC_POINT *pointc; EC_GROUP *group2; unsigned char pubkey2[ECDH_SIZE]; unsigned char shared2[ECDH_SIZE]; int len2; //Generate Public ecdh2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);//NID_secp521r1 EC_KEY_generate_key(ecdh2); point2 = (struct ec_point_st *)EC_KEY_get0_public_key(ecdh2); group2 = (struct ec_group_st *)EC_KEY_get0_group(ecdh2); if(0 == (len2 = EC_POINT_point2oct(group2, point2, POINT_CONVERSION_COMPRESSED, pubkey2, ECDH_SIZE, NULL))) handleErrors(); printf("len2=%d\n",len); /* alice */ //ComputeKey point2c = EC_POINT_new(group); EC_POINT_oct2point(group, point2c, pubkey2, ECDH_SIZE, NULL); if (0 != EC_POINT_cmp(group, point2, point2c, NULL)) handleErrors(); if(0 == (len = ECDH_compute_key(shared, ECDH_SIZE, point2c, ecdh, NULL))) handleErrors(); printf("len=%d\n",len); disp("shared", shared, len); /* bob */ //ComputeKey pointc = EC_POINT_new(group2); EC_POINT_oct2point(group2, pointc, pubkey, ECDH_SIZE, NULL); if (0 != EC_POINT_cmp(group2, point, pointc, NULL)) handleErrors(); if(0 == (len2 = ECDH_compute_key(shared2, ECDH_SIZE, pointc, ecdh2, NULL))) handleErrors(); printf("len2=%d\n",len2); disp("shared2", shared2, len2); /* alice */ EC_POINT_free(pointc); EC_KEY_free(ecdh); /* bob */ EC_POINT_free(point2c); EC_KEY_free(ecdh2); printf("To the end\n"); return 0; }
组建(build),报错如下
出错原因在“ 解决error LNK2001 unresolved external symbol”一文中说得很清楚,缺少必要的库,添加库的方法为:工程—>设置—>连接—>对象/模块库—>添加ssleay32.lib
libeay32.lib,如下
在组建(build),成功了,运行(Ctrl+F5),结果如下
相关文章推荐
- Linux C函数参考手册(PDF版)
- C# partial关键字说明
- Lua教程(十七):C API简介
- 简单谈谈lua和c的交互
- C#中的委托数据类型简介
- C#编写的艺术字类实例代码
- C#实现打造气泡屏幕保护效果
- 举例讲解C#编程中委托的实例化使用
- 使用C#代码获取存储过程返回值
- C++中explict关键字用法
- C/C++数据对齐详细解析
- 利用C语言来求最大连续子序列乘积的方法
- 字符串的组合算法问题的C语言实现攻略
- C 语言基础教程(我的C之旅开始了)[三]
- 学习C和C++的9点经验总结
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- 最大子矩阵问题实例解析
- C字符串操作函数实现方法小结