Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the encryption function by using DES Module in C language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "C language how to use DES module to achieve encryption function", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "C language how to use DES module to achieve encryption function" bar!

DES (Data Encryption Standard)

DES was once the leader of symmetrical encryption of electronic data. He influenced modern cryptography. First developed by IBM in 1970 based on the earlier design of Horst Feistel, the algorithm joined the candidate scheme for encrypting sensitive electronic data to the US government at the invitation of the NBSNational_Bureau_of_Standards National Bureau of Standards agent. In 1976, after consultation with the National Security Agency (NSA), NBS finally chose a stripped-down version to be released in 1977.

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

AES (Advanced Encryption Standard)

Is a block encryption standard adopted by the federal government of the United States. This standard, which is used to replace the original DES, has been analyzed by many parties and is widely used all over the world. After a five-year selection process, the Advanced encryption Standard was issued by the National Institute of Standards and Technology (NIST) in FIPS PUB 197 on November 26th, 2001, and became a valid standard on May 26th, 2002. In 2006, the advanced encryption standard has become one of the most popular algorithms in symmetric key encryption.

Compile 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

Code example

DES

Include file

# include # include # ifndef uchar#define uchar unsigned char#endif

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; iTunes +) {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 and run

Scons script 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'] = [' masked, 'crypto',' dl' 'profiler'] env.Program (target = ". / test_des", source = (glob.glob ('. / * .c')

Test code

Int test_fun (int agrn, char * agrv []) {char * _ key = "JKL * _ char"; char * _ vt = "asdf shipping costs $"; 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;} Thank you for your reading, this is the content of "how C language uses DES module to achieve encryption function". After the study of this article, I believe you have a deeper understanding of how C language uses DES module to achieve encryption function, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report