C language USES openSSL library DES module to achieve encryption function

  • 2020-05-19 05:26:11
  • OfStack

This article illustrates an example of how the C language implements encryption using the openSSL library DES module. I will share it with you for your reference as follows:

In the communication process, it is necessary to encrypt the protocol in order to prevent common players from intercepting the modified content of the protocol and sending it. At the moment such encryption is the infrastructure of the world. We just need to introduce it into the project. This article will write an example of encryption and decryption based on OpenSSL. The popular encryption and decryption methods are DES/AES. Let's talk about history first.

History:

DES(Data Encryption Standard)

DES1 degree is the leader of electronic data symmetric encryption. He influenced modern cryptography. It was first developed in 1970 by IBM based on an earlier Horst Feistel design. At the invitation of the agent of the national bureau of standards (NBSNational_Bureau_of_ Standards), National Bureau of Standards, the algorithm was proposed to encrypt sensitive electronic data of the United States government. In 1976, after consultation with the us national security agency (NSA), NBS finally chose a reduced version for release in 1977.

DES is still considered for encryption in many applications today. This is mainly due to 56-byte key size

AES(Advanced Encryption Standard)

Is the United States federal government adopted a block encryption standard. This standard, which replaces the original DES, has been analyzed by multiple parties and is widely used around the world. After a 5-year selection process, the advanced encryption standard was issued by the U.S. national institute of standards and technology (NIST) on FIPS PUB 197 on November 26, 2001, and became an effective standard on May 26, 2002. In 2006, advanced encryption standards became one of the most popular algorithms for symmetric key encryption.

Compile 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

Code sample

DES

include file


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

The introduction of lib


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

Encryption code


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;
}

Decryption code


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;
}

Compile operation

scons script 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' ) ) )

The test code


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: about encryption and decryption interested friends can also refer to the website online tools:

Text online encryption and decryption tools (including AES, DES, RC4, etc.) :
http://tools.ofstack.com/password/txt_encode

MD5 online encryption tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / es1064en-160 encryption tools:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode

I hope this article has been helpful to you in the programming of C language.


Related articles: