Here, insert a bar of encryption, using the OpenSSL library for encryption.
Using MD5 encryption
Let's take a string as an example, create a new file filename.txt, write hello in the file, and then use the command md5sum filename.txt to calculate the MD5 value under Linux ==> b1946ac92492d2347c6235b4d2611184. Although the 5 characters of the Hello are written, we can see that there will be a 0x0a return at the end of the file after we use the command xxd filename.txt. So there is \ n in the code below.
1//Open/usr/include/openssl/md5.h This file we can see some functions2//Initialize MD5 Contex, successfully returned 1, failed to return 03int Md5_init (MD5_CTX *c);4//Loop call this function, you can add different data together to calculate MD5, successfully returned 1, failed to return 05int Md5_update (Md5_ctx *c,Constvoid *Data, size_t Len);6//Output MD5 result data, successfully returned 1, failed to return 0 7 int md5_final (unsigned Char *md, md5_ctx *c); 8 // Md5_init,md5_update, Md5_final a combination of three functions to calculate the value of MD5 directly 9 unsigned char *md5 (const unsigned char *d, size_t N, unsigned char *MD); 10 // intrinsic function, no calls to 11 void md5_transform (md5_ctx *c, Const unsigned char *b);
Create a new CPP file to calculate the MD5 value
1 #include <openssl/md5.h>2 #include <String.h>3 #include <stdio.h>45IntMain ()6{7Md5_ctx CTX;8 unsignedChar outmd[16];9int i=0;10memset (OUTMD,0,sizeof(OUTMD));Md5_init (&CTX);Md5_update (&ctx,"Hel",3);Md5_update (&ctx,"lo\n",3);Md5_final (outmd,&CTX);16 for (I=0;i<16;i<i++) 17 {18 printf (" %02x ",outmd[i]); 19 }20 printf (" \n21 return 0 ; 22}
The compilation options are: g++ Md5test.cpp-lssl-o md5test
The result after operation is: b1946ac92492d2347c6235b4d2611184
Note that the OpenSSL library is used here and can be installed by running Yum install OpenSSL and yum install Openssl-devel.
The following code is a MD5 calculation of the file.
1 #include <openssl/md5.h>2 #include <String.h>3 #include <stdio.h>45IntMain ()6{7Md5_ctx CTX;8 unsignedChar outmd[16];9Char buffer[1024];10Char filename[32];11int len=0;12IntIFILE * fp=NULL;memset (OUTMD,0,sizeof(OUTMD));memset (filename,0,sizeof(filename));memset (Buffer,0,sizeof(buffer));printf ("Please enter a file name for calculating the MD5 value:");scanf ("%s", filename);Fp=fopen (filename,"Rb");20if (fp==NULL)21st{printf ("Can ' t open file\n");23Return0;24}25Md5_init (&CTX);27while (Len=fread (buffer,1,1024,FP)) >0)28{Md5_update (&Ctx,buffer,len);memset (Buffer,0,sizeof(buffer));31}Md5_final (outmd,&CTX);33 34 for (I=0;i<16;i<i++) 35 {36 printf (" Span style= "COLOR: #800000" >%02x ",outmd[i]); 37 }38 printf ( "\n" ); 39 return 0; 40}
After the results are run, we can use the md5sum command to verify.
Using SHA1 encryption
Several functions in OpenSSL are explained
1//The SHA1 algorithm is an upgrade to the MD5 algorithm that evaluates to 20 bytes (160 bits), using the following method:2//Open/usr/include/openssl/sha.h This file we can see some functions3//Initialization of SHA Contex, successfully returned 1, failed to return 04int Sha_init (SHA_CTX *c);5//Loop call this function, you can add different data together to calculate SHA1, successfully returned 1, failed to return 06int Sha_update (Sha_ctx *c,Constvoid *Data, size_t Len);7//Output SHA1 result data, successfully returned 1, failed to return 08int sha_final (unsignedChar *MD, Sha_ctx *c);9// sha_init,sha_update,sha_final a combination of three functions that directly calculates the value of SHA1 Span style= "COLOR: #008080" >10 unsigned char *sha (const unsigned char *d, size_t N, unsigned char *MD); 11 // intrinsic function, no calls to 12 void sha_transform (sha_ctx *c, Const unsigned char *data); 14 //
We're making changes to the above program.
1 #include <openssl/sha.h>2 #include <String.h>3 #include <stdio.h>45IntMain ()6{7Sha_ctx STX;8 unsignedChar outmd[20];Note that the number of characters here is 20.9Char buffer[1024];10Char filename[32];11int len=0;12IntIFILE * fp=NULL;memset (OUTMD,0,sizeof(OUTMD));memset (filename,0,sizeof(filename));memset (Buffer,0,sizeof(buffer));printf ("Please enter a file name for calculating the SHA1 value:");scanf ("%s", filename);Fp=fopen (filename,"Rb");20if (fp==NULL)21st{printf ("Can ' t open file\n");23Return0;24}25Sha1_init (&STX);27while (Len=fread (buffer,1,1024,FP)) >0)28{Sha1_update (&Stx,buffer,len);memset (Buffer,0,sizeof(buffer));31}Sha1_final (outmd,&STX);33 34 for (I=0;i<20;i<i++) 35 {36 printf (" Span style= "COLOR: #800000" >%02x ",outmd[i]); 37 }38 printf ( "\n" ); 39 return 0; 40}
The MD5 has 128bit (16 char) *SHA1 with 160bit (20 char) *sha256 with 256bit (32 char) *sha244 with 244bit (28 char) *sha512 with 512bit (64 char). So pay attention to change the size Oh:-O
MD5 encryption algorithms and upgrades