欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言使用openSSL庫DES模塊實現(xiàn)加密功能詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

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

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

歷史介紹

DES(Data Encryption Standard)

DES一度是電子數(shù)據(jù)對稱加密的主導(dǎo)者。他影響了現(xiàn)代加密學(xué)。最早是在IBM于1970年基于更早的Horst Feistel的設(shè)計而開發(fā)出來的,算法應(yīng)美國國家標(biāo)準(zhǔn)局(NBSNational_Bureau_of_Standards) National Bureau of Standards)代理人的邀請加入對美國政府敏感電子數(shù)據(jù)加密的候選方案。在1976年,經(jīng)過和美國國家安全局(NSA)磋商,NBS最終選擇了一個精簡版本在1977年發(fā)布。

如今在很多應(yīng)用的加密還是會考慮使用DES。這個主要由于56-byte key size

AES(Advanced Encryption Standard)

是美國聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。經(jīng)過五年的甄選流程,高級加密標(biāo)準(zhǔn)由美國國家標(biāo)準(zhǔn)與技術(shù)研究院(NIST)于2001年11月26日發(fā)布于FIPS PUB 197,并在2002年5月26日成為有效的標(biāo)準(zhǔn)。2006年,高級加密標(biāo)準(zhǔn)已然成為對稱密鑰加密中最流行的算法之一。

編譯openssl

wget ftp://ftp.openssl.org/source/openssl-1.0.0c.tar.gz
tar -zxf openssl-1.0.0c.tar.gz
cd openssl-1.0.0c/
./config --prefix=/usr/local --openssldir=/usr/local/ssl
make && make install
./config shared --prefix=/usr/local --openssldir=/usr/local/ssl
make clean
make && 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;
}

編譯運(yùn)行

scons腳本SConstruct

import glob
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' ) ) )

測試代碼

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:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:

文字在線加密解密工具(包含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語言程序設(shè)計有所幫助。

上一篇:C++ 中placement new 操作符使用方法

欄    目:C語言

下一篇:求解旋轉(zhuǎn)數(shù)組的最小數(shù)字

本文標(biāo)題:C語言使用openSSL庫DES模塊實現(xiàn)加密功能詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1528.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有