国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言使用openSSL庫DES模塊實現加密功能詳解

2020-01-26 14:07:49
字體:
來源:轉載
供稿:網友

本文實例講述了C語言使用openSSL庫DES模塊實現加密功能。分享給大家供大家參考,具體如下:

在通訊過程中為了防止普通的玩家截取協議修改內容并且發送,我們是有必要對協議進行加密的。當前這樣的加密手段都已經是變成世界里面的基礎設施了。我們只需要將其引入到工程中就好。本文將會基于OpenSSL來編寫一個加密、解密的實例。時下流行的加密解密方式有DES/AES。先我們來聊聊歷史吧。

歷史介紹

DES(Data Encryption Standard)

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

wget ftp://ftp.openssl.org/source/openssl-1.0.0c.tar.gztar -zxf openssl-1.0.0c.tar.gzcd openssl-1.0.0c/./config --prefix=/usr/local --openssldir=/usr/local/sslmake && make install./config shared --prefix=/usr/local --openssldir=/usr/local/sslmake cleanmake && make install

代碼示例

DES

include文件

#include <openssl/des.h>#include <openssl/pkcs7.h>#ifndef uchar#define uchar unsigned char#endif

引入lib

libeay32.lib    // for windows-lcrypto      // for linux

加密代碼

int encrypt_data(const char *_key, const char *_vt,char *_raw_ptr,size_t _raw_size  , char **_dst_buf, size_t *_dst_size) {  DES_key_schedule schedule;  uchar key1[8];  des_cblock *iv3;  int pading ;  size_t i, 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( (const uchar*)mid_buf, (unsigned char *)*_dst_buf, *_dst_size, &schedule, iv3, DES_ENCRYPT);  free(iv3);  free(mid_buf);  return 1;}

解密代碼

int decrypt_data(const char *_key, const char *_vt,char *_raw_ptr,size_t _raw_size    , char **_dst_buf, size_t *_dst_size ) {  DES_key_schedule schedule;  uchar key1[8];  des_cblock *iv3;  int pading ;  size_t i, 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( (const uchar*)_raw_ptr, *_dst_buf, _raw_size, &schedule, iv3, DES_DECRYPT);  free(iv3);  return 1;}

編譯運行

scons腳本SConstruct

import globenv = 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' ) ) )

測試代碼

int test_fun( int agrn, 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);  return 0;}

PS:關于加密解密感興趣的朋友還可以參考本站在線工具:

文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.VeVB.COm/password/txt_encode

MD5在線加密工具:
http://tools.VeVB.COm/password/CreateMD5Password

在線散列/哈希算法加密工具:
http://tools.VeVB.COm/password/hash_encrypt

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.VeVB.COm/password/hash_md5_sha

在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.VeVB.COm/password/sha_encode

希望本文所述對大家C語言程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 谷城县| 上高县| 文山县| 衡阳市| 临城县| 蛟河市| 临汾市| 汾西县| 六安市| 云阳县| 商城县| 彭州市| 项城市| 新巴尔虎右旗| 满城县| 双柏县| 沐川县| 株洲县| 泰和县| 牙克石市| 永吉县| 鹿邑县| 五指山市| 太原市| 永善县| 潮州市| 乌拉特中旗| 保定市| 印江| 南康市| 桃源县| 郧西县| 金湖县| 沈阳市| 珠海市| 拜城县| 交口县| 平和县| 鹿邑县| 枣庄市| 诸暨市|