转自:https://blog.csdn.net/leumber/article/details/78043675
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leumber/article/details/78043675
DES和3DES加密算法C语言实现
记录DES和3DES加密算法最简洁易懂的C语言源码
主要是要用到CBC这部分的算法,后边也有一个工具可以提供验证,因为网上的工具含有CBC的很少,也方便大家吧
#define MBEDTLS_DES_ENCRYPT 1
#define MBEDTLS_DES_DECRYPT 0
#define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0002 /**< The data input has an invalid length. */
#define MBEDTLS_DES_KEY_SIZE 8
#define DES_KEY_SIZE (8)
#define DES3_KEY2_SIZE (16)
#define DES3_KEY3_SIZE (24)
typedef struct
{
uint32_t sk[32]; /*!< DES subkeys */
}des_context;
/**
* rief Triple-DES context structure
*/
typedef struct
{
uint32_t sk[96]; /*!< 3DES subkeys */
}des3_context;
#define DES_C
#if defined(DES_C)
#include "des.h"
#include <string.h>
#include <stdlib.h>
#if !defined(DES_ALT)
/* Implementation that should never be optimized out by the compiler */
static void zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
}
/*
* 32-bit integer manipulation macros (big endian)
*/
#ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i)
{
(n) = ( (uint32_t) (b)[(i) ] << 24 )
| ( (uint32_t) (b)[(i) + 1] << 16 )
| ( (uint32_t) (b)[(i) + 2] << 8 )
| ( (uint32_t) (b)[(i) + 3] );
}
#endif
#ifndef PUT_UINT32_BE
#define PUT_UINT32_BE(n,b,i)
{
(b)[(i) ] = (unsigned char) ( (n) >> 24 );
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 );
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 );
(b)[(i) + 3] = (unsigned char) ( (n) );
}
#endif
/*
* Expanded DES S-boxes
*/
static const uint32_t SB1[64] =
{
0x01010400, 0x00000000, 0x00010000, 0x01010404,
0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
0x00000404, 0x01000400, 0x01000400, 0x00010400,
0x00010400, 0x01010000, 0x01010000, 0x01000404,
0x00010004, 0x01000004, 0x01000004, 0x00010004,
0x00000000, 0x00000404, 0x00010404, 0x01000000,
0x00010000, 0x01010404, 0x00000004, 0x01010000,
0x01010400, 0x01000000, 0x01000000, 0x00000400,
0x01010004, 0x00010000, 0x00010400, 0x01000004,
0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
0x01000004, 0x00000404, 0x00010404, 0x01010400,
0x00000404, 0x01000400, 0x01000400, 0x00000000,
0x00010004, 0x00010400, 0x00000000, 0x01010004
};
static const uint32_t SB2[64] =
{
0x80108020, 0x80008000, 0x00008000, 0x00108020,
0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
0x00108000, 0x00100020, 0x80008020, 0x00000000,
0x80000000, 0x00008000, 0x00108020, 0x80100000,
0x00100020, 0x80000020, 0x00000000, 0x00108000,
0x00008020, 0x80108000, 0x80100000, 0x00008020,
0x00000000, 0x00108020, 0x80100020, 0x00100000,
0x80008020, 0x80100000, 0x80108000, 0x00008000,
0x80100000, 0x80008000, 0x00000020, 0x80108020,
0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
0x00100020, 0x80008020, 0x80000020, 0x00100020,
0x00108000, 0x00000000, 0x80008000, 0x00008020,
0x80000000, 0x80100020, 0x80108020, 0x00108000
};
static const uint32_t SB3[64] =
{
0x00000208, 0x08020200, 0x00000000, 0x08020008,
0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
0x08000000, 0x00000008, 0x08020200, 0x00000200,
0x00020200, 0x08020000, 0x08020008, 0x00020208,
0x08000208, 0x00020200, 0x00020000, 0x08000208,
0x00000008, 0x08020208, 0x00000200, 0x08000000,
0x08020200, 0x08000000, 0x00020008, 0x00000208,
0x00020000, 0x08020200, 0x08000200, 0x00000000,
0x00000200, 0x00020008, 0x08020208, 0x08000200,
0x08000008, 0x00000200, 0x00000000,