Go to: http://blog.csdn.net/lyjinger/article/details/1722570
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/des.h>/*********
* * This example uses: * * 3DES-ECB encryption mode, * * 24-bit key, less than 24-bit right to fill the 0x00;
* * Encrypted content 8-bit, the completion of the way is: less than 1 bit to fill a 0x01, less 2 two 0x02,... * * itself has 8-bit aligned, followed by eight 0x08.
/int main (void) {int docontinue = 1; unsigned char *data = "Hello world!";
/* plaintext */int data_len;
int data_rest;
unsigned char ch; unsigned char *src = NULL; * * After the clear text/unsigned char *dst = NULL;
* * decrypted plaintext/int len;
unsigned char tmp[8];
unsigned char in[8];
unsigned char out[8]; Char *k = "01234567899876543210";
/* Original key */int key_len; #define Len_of_key unsigned char key[len_of_key];
/* After completion of the key * * unsigned char block_key[9];
Des_key_schedule KS,KS2,KS3;
* The key after completion of the/* Structure Key_len = strlen (k); memcpy (Key, K, Key_len);
memset (key + Key_len, 0x00, Len_of_key-key_len);
/* Analysis of the required space to complete the clear text and fill the filling data * * * Data_len = strlen (data);
Data_rest = data_len% 8;
Len = Data_len + (8-data_rest);
ch = 8-data_rest;
src = malloc (len);
DST = malloc (len); if (NULL = src | |
NULL = DST) {docontinue = 0;
} if (docontinue) {int count;
int i;
/* Construction After the completion of the encrypted content * * memset (src, 0, Len);
memcpy (SRC, data, data_len);
memset (src + data_len, ch, 8-data_rest);
/* Key Replacement */memset (block_key, 0, sizeof (block_key));
memcpy (Block_key, key + 0, 8);
Des_set_key_unchecked ((const_des_cblock*) Block_key, &ks);
memcpy (Block_key, key + 8, 8);
Des_set_key_unchecked ((const_des_cblock*) Block_key, &KS2);
memcpy (Block_key, key + 16, 8);
Des_set_key_unchecked ((const_des_cblock*) Block_key, &KS3);
printf ("Before encrypt:"); for (i = 0; I &Lt Len
i++) {printf ("0x%.2x", * (src + i));
printf ("n");
/* Loop encryption/decryption, every 8 bytes per * * Count = LEN/8;
for (i = 0; i < count; i++) {memset (TMP, 0, 8);
memset (in, 0, 8);
memset (out, 0, 8);
memcpy (TMP, SRC + 8 * I, 8);
* * Encryption/Des_ecb3_encrypt ((const_des_cblock*) TMP, (des_cblock*) in, &ks, &ks2, &KS3, Des_encrypt); /* Decrypt/* DES_ECB3_ENCRYPT (const_des_cblock*) in, (des_cblock*) out, &ks, &ks2, &KS3, DE
S_decrypt);
* * Copy the decrypted content to the decrypted plaintext/memcpy (DST + 8 * I, out, 8);
printf ("After decrypt:");
for (i = 0; i < len; i++) {printf ("0x%.2x", * (DST + i));
printf ("n");
} if (NULL!= src) {free (SRC);
src = NULL;
} if (NULL!= DST) {free (DST);
DST = NULL;
} return 0; }