实例介绍
安全性高
有研究表示160位的椭圆密钥与1024位的RSA密钥安全性相同。
处理速度快
在私钥的加密解密速度上,ecc算法比RSA、DSA速度更快。
-
存储空间占用小。
-
带宽要求低。 [1]
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ecc_en.h"
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib,"ecc_d_x64.lib")
#else
#pragma comment(lib,"ecc_x64.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib,"ecc_d.lib")
#else
#pragma comment(lib,"ecc.lib")
#endif
#endif
int processor(int iteration) {
int nRet =0;
int tlen;
int olen;
char *hex_pub = NULL, *hex_priv = NULL;
unsigned char *text = NULL, *copy = NULL, *original = NULL;
char*en_buf = NULL;
int nen_len =0;
// Generate random size for the block of data were going to encrypt.
//Use a min value of 1 KB and a max of 1 MB.
do {
tlen = (rand() % (1024 * 1024));
} while (tlen < 1024);
if (!(text = (unsigned char*)malloc(tlen 1)) || !(copy = (unsigned char *)malloc(tlen 1))) {
printf("Memory error.\n");
return -1;
}
// Wipe and then fill the data blocks with random data.
memset(copy, 0, tlen 1);
memset(text, 0, tlen 1);
for (int j = 0; j < tlen; j ) {
*(copy j) = *(text j) = (rand() % 255);
}
if(!ecc_get_private_public_key(&hex_priv,&hex_pub))
{
printf("ecc_get_private_public_key in fail:\n");
return -1;
}
printf("prv:%s\n",hex_priv);
printf("pub:%s\n",hex_pub);
do
{
if(!ecc_encrypt_buf(hex_pub,(char*)text,tlen,&en_buf,&nen_len))
{
printf("The encryption process failed!\n");
nRet =-1;
break;
}
if(!ecc_decrypt_buf(hex_priv,en_buf,nen_len,(char**)&original,&olen))
{
printf("The decryption process failed!\n");
nRet = -1;
break;
}
if (olen != tlen || memcmp(original, copy, tlen)) {
printf("Comparison failure.\n");
nRet = -1;
break;
}
nRet =1;
} while (0);
if(hex_pub)
free(hex_pub);
if(hex_priv)
free(hex_priv);
if(copy)
free(copy);
if(text)
free(text);
if(en_buf)
free(en_buf);
if(original)
free(original);
if(!nRet)
getchar();
printf(" ... %i ... %i\n", iteration 1, nRet);
return nRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
size_t nMaxCurveCount =0;
if(-1==ecc_init(0,&nMaxCurveCount))
{
printf("ecc_init in fail! (nid > nMaxCurveCount\n");
return -1;
}
for (int i = 0; i < 100; i ) {
if (!processor(i)) {
getchar();
return 1;
}
}
getchar();
return 0;
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论