C语言如何利用DES模块实现加密功能

C语言如何利用DES模块实现加密功能

这篇文章主要讲解了“C语言如何利用DES模块实现加密功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C语言如何利用DES模块实现加密功能”吧!

DES(Data Encryption Standard)

C语言如何利用DES模块实现加密功能

DES一度是电子数据对称加密的主导者。他影响了现代加密学。最早是在IBM于1970年基于更早的Horst Feistel的设计而开发出来的,算法应美国国家标准局(NBSNational_Bureau_of_Standards) National Bureau of Standards)代理人的邀请加入对美国政府敏感电子数据加密的候选方案。在1976年,经过和美国国家安全局(NSA)磋商,NBS最终选择了一个精简版本在1977年发布。

如今在很多应用的加密还是会考虑使用DES。这个主要由于56-byte key size

AES(Advanced Encryption Standard)

是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

编译openssl

wgetftp://ftp.openssl.org/source/openssl-1.0.0c.tar.gztar-zxfopenssl-1.0.0c.tar.gzcdopenssl-1.0.0c/./config--prefix=/usr/local--openssldir=/usr/local/sslmake&&makeinstall./configshared--prefix=/usr/local--openssldir=/usr/local/sslmakecleanmake&&makeinstall

代码示例

DES

include文件

#include<openssl/des.h>#include<openssl/pkcs7.h>#ifndefuchar#defineucharunsignedchar#endif

引入lib

libeay32.lib//forwindows-lcrypto//forlinux

加密代码

intencrypt_data(constchar*_key,constchar*_vt,char*_raw_ptr,size_t_raw_size,char**_dst_buf,size_t*_dst_size){DES_key_scheduleschedule;ucharkey1[8];des_cblock*iv3;intpading;size_ti,vt_size;char*mid_buf;memset(key1,0,8);memcpy(key1,_key,8);DES_set_key_unchecked((const_DES_cblock*)&key1,&schedule);vt_size=strlen(_vt);iv3=(des_cblock*)malloc(vt_size*sizeof(uchar));memcpy(iv3,_vt,vt_size);pading=8-(_raw_size%8);*_dst_size=_raw_size+pading;mid_buf=(char*)malloc(*_dst_size);memcpy(mid_buf,_raw_ptr,_raw_size);for(i=_raw_size;i<*_dst_size;i++){mid_buf[i]=pading;}*_dst_buf=(char*)malloc(*_dst_size);DES_cbc_encrypt((constuchar*)mid_buf,(unsignedchar*)*_dst_buf,*_dst_size,&schedule,iv3,DES_ENCRYPT);free(iv3);free(mid_buf);return1;}

解密代码

intdecrypt_data(constchar*_key,constchar*_vt,char*_raw_ptr,size_t_raw_size,char**_dst_buf,size_t*_dst_size){DES_key_scheduleschedule;ucharkey1[8];des_cblock*iv3;intpading;size_ti,vt_size;char*mid_buf;memset(key1,0,8);memcpy(key1,_key,8);DES_set_key_unchecked((const_DES_cblock*)&key1,&schedule);vt_size=strlen(_vt);iv3=(des_cblock*)malloc(vt_size*sizeof(uchar));memcpy(iv3,_vt,vt_size);*_dst_buf=(char*)malloc(_raw_size);DES_cbc_encrypt((constuchar*)_raw_ptr,*_dst_buf,_raw_size,&schedule,iv3,DES_DECRYPT);free(iv3);return1;}

编译运行

scons脚本SConstruct

importglobenv=Environment()env["CPPPATH"]=['/home/abel/lib/openssl-1.0.2f/include']env['LIBPATH']=['/home/abel/lib/openssl-1.0.2f']env['CPPDEFINES']=['LINUX','_DEBUG']env['CCFLAGS']='-g-std=gnu99'env['LIBS']=['m','crypto','dl','profiler']env.Program(target="./test_des",source=(glob.glob('./*.c')))

测试代码

inttest_fun(intagrn,char*agrv[]){char*_key="jkl;!@#$";char*_vt="asdf!@#$";char*_raw_ptr;size_t_raw_size;char*_dst_buf;size_t_dst_size;char*_final_buf;size_t_final_size;_raw_ptr=(char*)malloc(sizeof(char)*5);memcpy(_raw_ptr,"hello",5);_raw_size=5;encrypt_data(_key,_vt,_raw_ptr,_raw_size,&_dst_buf,&_dst_size);decrypt_data(_key,_vt,_dst_buf,_dst_size,&_final_buf,&_final_size);printf("final:%s\n",_final_buf);free(_dst_buf);return0;}

感谢各位的阅读,以上就是“C语言如何利用DES模块实现加密功能”的内容了,经过本文的学习后,相信大家对C语言如何利用DES模块实现加密功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是恰卡编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

发布于 2022-04-03 22:41:09
收藏
分享
海报
0 条评论
42
上一篇:C语言中的makefile怎么使用 下一篇:C语言中常用的头文件是什么
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码