DES encryption applications

1) Data Format
plain text by 64-bit packet, the key requirement is that the length of the 64-bit
2) filling mode plaintext
DES encryption algorithm requires the plaintext length must be 8-byte aligned. If for any piece of data before the last encryption requires a
filled to 8 bytes, padding need to delete the decrypted data

There are three modes are filled
ZeroPadding, data length is not aligned, padded with zeros, end with respect to the plaintext / 0, can be distinguished
PKCS7Padding, assuming that the data length n to be filled (> 0 n) byte aligned, then n padding bytes, each byte n-; if the data itself have been aligned, the
filling length of a data block size, block size each byte.
PKCS5Padding, PKCS7Padding subset fixed block size is 8 bytes

Providing a length of code PKCS5Padding filling
 CH =. 8 -. 8% nlen;
 Memset (nlen the src +, CH, CH); // fill padding5
described nlen plaintext length, src + nlen pointer is the last plaintext, nlen% 8 how many words section is not aligned, then 8 - so many nlen% 8 to add a few bytes

3) a vector
in the CBC (not just the DES algorithm) mode, iv generation is a relatively common method of using a random number (or pseudo random) mechanism. iv action is used mainly to produce a first ciphertext block, so that the final ciphertext generated a difference (same plaintext), the password *** makes more difficult, and in addition iv no other uses. The greatest advantage is that even if the same plaintext, the same key can generate different cipher text.


4) Under normal circumstances, after the adoption of DES encryption, you need to be sent hexadecimal code, and some also need to be Base64 encoded, which is to remember!

Call OpenSSL encryption algorithm
where the plaintext length is limited to 4096, mainly inside to save the data length of the Qi Houming text is hard-coded caused
bool EncryptDataByDESOfIV (std :: string & key , std :: string & iv, char * input_data, unsigned char encrypt_data *, data_len The int *)
{
 ! IF (key.size () =. 8) return to false;

 DES_key_schedule ks;
 DES_cblock ivec = { 0 };
 int i = 0;
 int len = 0;
 int nlen = 0;

 char * data = (char *) input_data; / * original plaintext, hexadecimal string * /
 char * Okey = (char *) key.c_str (); / * original key, hexadecimal string * /
 the memcpy (& ivec, iv.c_str (), iv.size ());
 unsigned char CH = '\ 0';
 unsigned char * PTR = NULL;

 unsigned char src[4096] = { 0 };
 unsigned char *dst = nullptr;
 DES_set_key_unchecked((const_DES_cblock*)okey, &ks);

 / * Analysis and filled up the space required Qi Mingwen padding data * /
 nLen = strlen ((char *) Data);
 // len = (nLen / +. 8 (nLen. 8%. 1:? 0)) *. 8;
 len . 8 nLen + = - (nLen. 8%);
 DST = (unsigned char *) calloc (. 1, len);

 memcpy(src, data, len);

 . 8 = CH -. 8% nLen;
 Memset (nLen the src +, CH,. 8 - nLen. 8%); // fill padding7

 //printf("加密之前: ");
 //for (i = 0; i < len; i++) {
 // printf("%.2X", *(src + i));
 //}
 //printf("\n");

 / * Cipher block chained encryption * /
 DES_ncbc_encrypt (the src, DST, len, & KS, & ivec, DES_ENCRYPT);
 the memcpy (encrypt_data, DST, len);
 * data_len The = len;
 // the printf ( "after being encrypted:");
 / / for (I = 0; I <len; I ++) {
 // the printf ( "% 2X.", * (DST + I));
 //}
 // the printf ( "\ n-");

 free(dst);

 return true;
}

Reference
https://www.cnblogs.com/chenggang816/p/10542673.html
https://www.cnblogs.com/azbane/p/10179660.html
http://tool.chacuo.net/cryptdes


Guess you like

Origin blog.51cto.com/fengyuzaitu/2416557