C语言使用openSSL库DES模块实现加密功能详解
本文实例讲述了C语言使用openSSL库DES模块实现加密功能。分享给大家供大家参考,具体如下:
在通讯过程中为了防止普通的玩家截取协议修改内容并且发送,我们是有必要对协议进行加密的。当前这样的加密手段都已经是变成世界里面的基础设施了。我们只需要将其引入到工程中就好。本文将会基于OpenSSL来编写一个加密、解密的实例。时下流行的加密解密方式有DES/AES。先我们来聊聊历史吧。
历史介绍
DES(DataEncryptionStandard)
DES一度是电子数据对称加密的主导者。他影响了现代加密学。最早是在IBM于1970年基于更早的HorstFeistel的设计而开发出来的,算法应美国国家标准局(NBSNational_Bureau_of_Standards)NationalBureauofStandards)代理人的邀请加入对美国政府敏感电子数据加密的候选方案。在1976年,经过和美国国家安全局(NSA)磋商,NBS最终选择了一个精简版本在1977年发布。
如今在很多应用的加密还是会考虑使用DES。这个主要由于56-bytekeysize
AES(AdvancedEncryptionStandard)
是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPSPUB197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
编译openssl
wgetftp://ftp.openssl.org/source/openssl-1.0.0c.tar.gz tar-zxfopenssl-1.0.0c.tar.gz cdopenssl-1.0.0c/ ./config--prefix=/usr/local--openssldir=/usr/local/ssl make&&makeinstall ./configshared--prefix=/usr/local--openssldir=/usr/local/ssl makeclean make&&makeinstall
代码示例
DES
include文件
#include#include #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
importglob env=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; }
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password
在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
希望本文所述对大家C语言程序设计有所帮助。