openssl
所属分类:加密解密
开发工具:Visual C++
文件大小:622KB
下载次数:198
上传日期:2013-03-15 14:56:15
上 传 者:
xiaoxue53
说明: openssl常用功能练习源码,包括des、3des、rsa加解密算法,数字签名操作,pem和der格式转换,以及证书操作等。适用于初学者。
(Openssl function practice commonly used source code, including des, 3 des, rsa encryption algorithm, digital signature, pem and der format conversion, as well as certificate of operation and so on. Suitable for beginners. )
文件列表:
openssl\createcrt\createcrt.c (3783, 2012-11-30)
openssl\createcrt\createcrt.vcproj (3993, 2012-11-30)
openssl\createcrt\createcrt.vcproj.CHENGWB.Administrator.user (1411, 2013-01-28)
openssl\createcrt\Debug\BuildLog.htm (4392, 2013-01-24)
openssl\createcrt\Debug\createcrt.exe.embed.manifest (403, 2012-11-30)
openssl\createcrt\Debug\createcrt.exe.embed.manifest.res (468, 2012-11-30)
openssl\createcrt\Debug\createcrt.exe.intermediate.manifest (385, 2012-12-12)
openssl\createcrt\Debug\createcrt.obj (25594, 2012-12-12)
openssl\createcrt\Debug\mt.dep (67, 2012-12-12)
openssl\createcrt\Debug\vc80.idb (84992, 2013-01-24)
openssl\createcrt\Debug\vc80.pdb (77824, 2013-01-24)
openssl\debug\createcrt.exe (45056, 2012-12-12)
openssl\debug\createcrt.exp (574, 2012-11-30)
openssl\debug\createcrt.ilk (441160, 2012-12-12)
openssl\debug\createcrt.lib (1762, 2012-11-30)
openssl\debug\createcrt.pdb (363520, 2012-12-12)
openssl\debug\openssl.exe (81920, 2013-01-24)
openssl\debug\openssl.exp (570, 2013-01-24)
openssl\debug\openssl.ilk (649360, 2013-01-24)
openssl\debug\openssl.lib (1738, 2013-01-24)
openssl\debug\openssl.pdb (478208, 2013-01-24)
openssl\debug\reqcrt.exe (40960, 2012-12-12)
openssl\debug\reqcrt.exp (569, 2012-11-30)
openssl\debug\reqcrt.ilk (411376, 2012-12-12)
openssl\debug\reqcrt.lib (1726, 2012-11-30)
openssl\debug\reqcrt.pdb (355328, 2012-12-12)
openssl\openssl\cacert.crt (936, 2012-11-21)
openssl\openssl\cacert.der (759, 2012-12-07)
openssl\openssl\cert.crt (3018, 2012-11-22)
openssl\openssl\cert2.crt (798, 2012-11-29)
openssl\openssl\Debug\BuildLog.htm (11980, 2013-01-24)
openssl\openssl\Debug\des.obj (26767, 2013-01-24)
openssl\openssl\Debug\file.obj (8559, 2013-01-24)
openssl\openssl\Debug\main.obj (25019, 2013-01-24)
openssl\openssl\Debug\mt.dep (67, 2013-01-24)
openssl\openssl\Debug\openssl.exe.embed.manifest (403, 2013-01-09)
openssl\openssl\Debug\openssl.exe.embed.manifest.res (468, 2013-01-09)
openssl\openssl\Debug\openssl.exe.intermediate.manifest (385, 2013-01-24)
openssl\openssl\Debug\pkcs7test.obj (8676, 2013-01-24)
openssl\openssl\Debug\rsa.obj (83459, 2013-01-24)
... ...
看了你的练习,基本上openssl的调用方式也比较熟悉了,功能都可以完成。
但是编码细节上问题还是很多,推荐一本《C陷阱与缺陷》,连同《C安全编码》,一定要看,才能提高编码能力。
存在以下问题,请思考和测试修改(标黄色的是你需要反馈给我的问题,周一下午确认全部解决或有答案了后邮件回复给我,其它的问题是我希望在下次编码中看到改善):
1.已经说过了,封装接口方式不对,不利于将来的使用。
4.目前接口中有错误直接return,主程序无法获知失败原因,应该返回错误码。
2.不要在接口中使用sizeof(data),思考原因和正确做法
DESEncrypt(data, enout, sizeof(data), dkey)
DESDecrypt(enout, deout, sizeof(enout), dkey);
Sizeof不是实际的数据长度。应该另外设置一个变量len,将sizeof(data)的值赋给len,
DESEncrypt(data, enout,len, dkey)
DESDecrypt(enout, deout, len, dkey);
3.目前在一个接口(如UseDes())中可以用原文和解密后的明文比较来判断加解密是否成功,实际使用中不会在一个接口里,如何判断解密成功?
在加密时先计算出原文的一个摘要,然后将该摘要与加密后的数据放在一起;解密时不操作摘要,在解密后计算解密数据的摘要然后同之前的摘要比较,一样则正确。如果需要保证安全性,那么就只有使用私钥签名了。
经过测试,修改加密过后的数据解密照样可以进行,当然解密后的数据与原数据是不同的。所以这些加密解密的函数只负责加解密,并不负责判断解密是否正确。
5.DataSign()参数中的ctx有何用,为何传来传去?
EVP_MD_CTX是摘要的载体,用来存放数据和数据的摘要。
Init是初始化此结构,update是计算数据的摘要并存入此结构,final是将摘要拷贝给用户。
这个参数可以直接封装在DataSign和DataVerify方法里面,当时没仔细想这个问题。
6.CreateRsa()
if (bits < 512 || bits > 4096) 为什么这样写不好?
密钥长度应该大于512且为2的n次方.
改为:
If(bits < 512 || bits > 4096);先判断范围
If(bits != 512 || bits != 1024 || bits != 2048 || bits != 4096);再判断值
7.char signdata[BITS****/512] = {0}; 这个公式是何含义?
本意是签名数据的长度,但是不易于理解,应改为BITS/8。
8.#define FILENAME_LEN 100
操作系统定义的文件长度是多少?你应该如何定义?
代码中类似的还要哪些地方?
windows系统定义:
#define _MAX_PATH 260 /* max. length of full pathname */
#define _MAX_DIR 256 /* max. length of path component */
#define _MAX_FNAME 256 /* max. length of file name component */
自己的定义
#define MAX_PATH _MAX_PATH /* max. length of full pathname */
#define MAX_DIR _MAX_DIR /* max. length of path component */
#define MAX_FNAME _MAX_FNAME /* max. length of file name component */
Unix下:
文件名长度为255
文件路径长度4096
#include
#include
int main()
{
long len = 0;
char file[10] = "cheng.c";
len = pathconf(file, _PC_NAME_MAX);
printf("NAME = %ld\n",len);
len = pathconf(file, _PC_PATH_MAX);
printf("PATH = %ld\n",len);
return 0;
}
可以用pathconf函数获取值。
_PC_NAME_MAX
returns the maximum length of a filename in the directory path
or filedes. the process is allowed to create. The correspond-
ing macro is _POSIX_NAME_MAX.
_PC_PATH_MAX
returns the maximum length of a relative pathname when path or
filedes is the current working directory. The corresponding
macro is _POSIX_PATH_MAX.
9.ReadCrtToX509()
如果用一个随机文件改名cert.crt,程序运行结果会怎样?
一样会运行成功,但是不是程序的本意。读取证书时应按两套方案,一是按pem格式,如果不对再用der格式.然后,还需要判断读取数据后的x509的结构是否正确。
将之前的PEM_read_X509(fp, &x509, NULL, NULL);
改为x509 = PEM_read_X509(fp, NULL, NULL, NULL);
下面个不需要初始化x509,而上面个必须要初始化x509;上面个如果不成功可能只有判断x509的内容是否正常,下面个没有初始化x509,所以如果失败了,那么x509的值为NULL。
同样x509 = d2i_X509_fp(fp, NULL);
10.打开文件要注意什么?
打开文件时最好都用二进制打开。
11.函数名和变量名要具有可读性,GetNotInfo,betime这些不容易懂
获得有效期GetValidTime,开始时间notbefore
近期下载者:
相关文件:
收藏者: