Implementing Des,3des encryption using the OpenSSL library

Source: Internet
Author: User
Tags openssl openssl library
Finishing reprint from: http://blog.csdn.net/xuplus/article/details/2607254
The main is to adjust the format, as well as some types of variables to solve in the VC2008 can not compile the problem.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h>
/************************************************************************
* * This example uses:
* * 3DES-ECB encryption method;
* * 24-bit key, less than 24-bit right complement 0x00;
* * Encrypted content 8-bit, the completion of the way is: less than 1 to fill a 0x01, less 2 to fill two 0x02,...
* * itself is 8-bit aligned, followed by eight 0x08.
************************************************************************/
int main (void)
{
int docontinue = 1;
char *data = "Hello world!"; /* Clear * *
int Data_len;
int data_rest;
unsigned char ch;
unsigned char *src = NULL; * * After the clear text/
unsigned char *dst = NULL; * * After the decryption of clear text * *
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 24
unsigned char Key[len_of_key]; * * After the completion of the key/
unsigned char block_key[9];
Des_key_schedule KS,KS2,KS3;
/* The key after completion of the construction * *
Key_len = strlen (k);
memcpy (key, K, Key_len);
memset (key + Key_len, 0x00, Len_of_key-key_len);
/* Analysis of the space required to complete the clear text and fill the data * *
Data_len = strlen (data);
Data_rest = data_len% 8;
Len = Data_len + (8-data_rest);
ch = 8-data_rest;
src = (unsigned char *) malloc (len);
DST = (unsigned char *) 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, &AMP;KS2);
memcpy (Block_key, key + 16, 8);
Des_set_key_unchecked ((const_des_cblock*) Block_key, &AMP;KS3);
printf ("Before encrypt:\n");
for (i = 0; i < len; i++)
{
printf ("0x%.2x", * (src + i));
}
printf ("\ n");
/* Loop encryption/decryption, once per 8 bytes * *
Count = LEN/8;
for (i = 0; i < count; i++)
{
memset (TMP, 0, 8);
memset (in, 0, 8);
memset (out, 0, 8);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.