精华内容
下载资源
问答
  • 3DES加密算法的设计与实现
    2021-05-24 04:30:17

    3DES Encryption algorithm Design and Implementation

    1、Hohai University

    Abstract:The Data Encryption Standard DES is released in 1977 with the American National Standards Bureau, for the past 20 years, it has been playing a very important role in the data encryption that could put up the thinking of designing careful and precise. Although with the development of the decryption technology , DES security has been threaten, the improvement of 3DES algorithm is a great extent to make up for the disadvantages of the DES algorithm, so it gets a very wide range of applications in the data encryption.

    The issue is the analysis and research of 3DES algorithm, and on this basis, using C program of the algorithm, the final design of a packet of data can go into effect file encryption software, providing the users an method to insure data security measures.

    更多相关内容
  • 3DES加密算法C语言实现代码,很好很实用.亲测可用.
  • 3DES加密算法C语言实现

    热门讨论 2013-09-26 18:45:04
    利用C语言编写的3DES加密算法,能够对任意长度的字符数组进行加解密。通过编写相应的JNI接口文件能够很容易移植到Android平台,从而解决不同平台DES加解密结果不一致的问题。
  • C语言实现DES、三重DES3DES)的加解密,支持ECB、CBC模式。 ECB支持64位密钥; CBC支持128和192位密钥
  • 而我们本篇文章讨论的是DES加密算法。希望大家能够将这两个名词区别开来,很多时候我们说的DES都是在指DES算法,而不是DES数据加密标准。DES算法是一种典型的分组密码,即将固定长度的明文通过一系列复杂的操作...
  • DES,DES加密算法,DES算法源码。用C写的DES加密算法。 DES,DES加密算法,DES算法源码。 DES加密算法c语言实现) (本程序可以直接使用)
  • 3DES加密和解密算法, 用C语言实现的代码实例 。// printf("Original data: %s\n", data); des(data, key1, len); //产生密钥,并调整数组byte,输出密文。 /*---再解次密---*/ printf("Please input a key2:\n...
  • DES/3DES算法C语言实现

    2018-06-11 20:52:27
    1、完整的DES/3DES算法,C语言实现,可用于金融POS安全认证; 2、DES-ECB加密/解密、DES-CBC加密/解密 33DES-ECB加密/解密、3DES-CBC加密/解密 4、内含测试程序,在Linux环境下...5、3DES,即TDES,三重数据加密算法
  • DES和3DES加密算法C语言实现

    万次阅读 热门讨论 2017-09-20 17:58:45
    DES和3DES加密算法C语言实现 记录DES和3DES加密算法最简洁易懂的C语言源码 typedef struct { uint32_t sk[32]; /*!< DES subkeys */ } des_context;/** * \brief Triple-DES context structure */ ...

    DES和3DES加密算法C语言实现
    记录DES和3DES加密算法最简洁易懂的C语言源码
    主要是要用到CBC这部分的算法,后边也有一个工具可以提供验证,因为网上的工具含有CBC的很少,也方便大家吧
    之前代码缺少一个头文件,有失严谨,这次更新一下,去掉头文件,将头文件部分内容加入到c文件头部

    #include <stddef.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define DES_ENCRYPT     1
    #define DES_DECRYPT     0
    
    #define ERR_DES_INVALID_INPUT_LENGTH              -0x0032  /**< The data input has an invalid length. */
    
    #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;
    
    /**
     * \brief          Triple-DES context structure
     */
    typedef struct
    {
        uint32_t sk[96];            /*!<  3DES subkeys      */
    }des3_context;
    
    #define DES_C
    
    #if defined(DES_C)
    
    
    
    #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, 0x08020008,
        0x08000208, 0x00020000, 0x08000000, 0x08020208,
        0x00000008, 0x00020208, 0x00020200, 0x08000008,
        0x08020000, 0x08000208, 0x00000208, 0x08020000,
        0x00020208, 0x00000008, 0x08020008, 0x00020200
    };
    
    static const uint32_t SB4[64] =
    {
        0x00802001, 0x00002081, 0x00002081, 0x00000080,
        0x00802080, 0x00800081, 0x00800001, 0x00002001,
        0x00000000, 0x00802000, 0x00802000, 0x00802081,
        0x00000081, 0x00000000, 0x00800080, 0x00800001,
        0x00000001, 0x00002000, 0x00800000, 0x00802001,
        0x00000080, 0x00800000, 0x00002001, 0x00002080,
        0x00800081, 0x00000001, 0x00002080, 0x00800080,
        0x00002000, 0x00802080, 0x00802081, 0x00000081,
        0x00800080, 0x00800001, 0x00802000, 0x00802081,
        0x00000081, 0x00000000, 0x00000000, 0x00802000,
        0x00002080, 0x00800080, 0x00800081, 0x00000001,
        0x00802001, 0x00002081, 0x00002081, 0x00000080,
        0x00802081, 0x00000081, 0x00000001, 0x00002000,
        0x00800001, 0x00002001, 0x00802080, 0x00800081,
        0x00002001, 0x00002080, 0x00800000, 0x00802001,
        0x00000080, 0x00800000, 0x00002000, 0x00802080
    };
    
    static const uint32_t SB5[64] =
    {
        0x00000100, 0x02080100, 0x02080000, 0x42000100,
        0x00080000, 0x00000100, 0x40000000, 0x02080000,
        0x40080100, 0x00080000, 0x02000100, 0x40080100,
        0x42000100, 0x42080000, 0x00080100, 0x40000000,
        0x02000000, 0x40080000, 0x40080000, 0x00000000,
        0x40000100, 0x42080100, 0x42080100, 0x02000100,
        0x42080000, 0x40000100, 0x00000000, 0x42000000,
        0x02080100, 0x02000000, 0x42000000, 0x00080100,
        0x00080000, 0x42000100, 0x00000100, 0x02000000,
        0x40000000, 0x02080000, 0x42000100, 0x40080100,
        0x02000100, 0x40000000, 0x42080000, 0x02080100,
        0x40080100, 0x00000100, 0x02000000, 0x42080000,
        0x42080100, 0x00080100, 0x42000000, 0x42080100,
        0x02080000, 0x00000000, 0x40080000, 0x42000000,
        0x00080100, 0x02000100, 0x40000100, 0x00080000,
        0x00000000, 0x40080000, 0x02080100, 0x40000100
    };
    
    static const uint32_t SB6[64] =
    {
        0x20000010, 0x20400000, 0x00004000, 0x20404010,
        0x20400000, 0x00000010, 0x20404010, 0x00400000,
        0x20004000, 0x00404010, 0x00400000, 0x20000010,
        0x00400010, 0x20004000, 0x20000000, 0x00004010,
        0x00000000, 0x00400010, 0x20004010, 0x00004000,
        0x00404000, 0x20004010, 0x00000010, 0x20400010,
        0x20400010, 0x00000000, 0x00404010, 0x20404000,
        0x00004010, 0x00404000, 0x20404000, 0x20000000,
        0x20004000, 0x00000010, 0x20400010, 0x00404000,
        0x20404010, 0x00400000, 0x00004010, 0x20000010,
        0x00400000, 0x20004000, 0x20000000, 0x00004010,
        0x20000010, 0x20404010, 0x00404000, 0x20400000,
        0x00404010, 0x20404000, 0x00000000, 0x20400010,
        0x00000010, 0x00004000, 0x20400000, 0x00404010,
        0x00004000, 0x00400010, 0x20004010, 0x00000000,
        0x20404000, 0x20000000, 0x00400010, 0x20004010
    };
    
    static const uint32_t SB7[64] =
    {
        0x00200000, 0x04200002, 0x04000802, 0x00000000,
        0x00000800, 0x04000802, 0x00200802, 0x04200800,
        0x04200802, 0x00200000, 0x00000000, 0x04000002,
        0x00000002, 0x04000000, 0x04200002, 0x00000802,
        0x04000800, 0x00200802, 0x00200002, 0x04000800,
        0x04000002, 0x04200000, 0x04200800, 0x00200002,
        0x04200000, 0x00000800, 0x00000802, 0x04200802,
        0x00200800, 0x00000002, 0x04000000, 0x00200800,
        0x04000000, 0x00200800, 0x00200000, 0x04000802,
        0x04000802, 0x04200002, 0x04200002, 0x00000002,
        0x00200002, 0x04000000, 0x04000800, 0x00200000,
        0x04200800, 0x00000802, 0x00200802, 0x04200800,
        0x00000802, 0x04000002, 0x04200802, 0x04200000,
        0x00200800, 0x00000000, 0x00000002, 0x04200802,
        0x00000000, 0x00200802, 0x04200000, 0x00000800,
        0x04000002, 0x04000800, 0x00000800, 0x00200002
    };
    
    static const uint32_t SB8[64] =
    {
        0x10001040, 0x00001000, 0x00040000, 0x10041040,
        0x10000000, 0x10001040, 0x00000040, 0x10000000,
        0x00040040, 0x10040000, 0x10041040, 0x00041000,
        0x10041000, 0x00041040, 0x00001000, 0x00000040,
        0x10040000, 0x10000040, 0x10001000, 0x00001040,
        0x00041000, 0x00040040, 0x10040040, 0x10041000,
        0x00001040, 0x00000000, 0x00000000, 0x10040040,
        0x10000040, 0x10001000, 0x00041040, 0x00040000,
        0x00041040, 0x00040000, 0x10041000, 0x00001000,
        0x00000040, 0x10040040, 0x00001000, 0x00041040,
        0x10001000, 0x00000040, 0x10000040, 0x10040000,
        0x10040040, 0x10000000, 0x00040000, 0x10001040,
        0x00000000, 0x10041040, 0x00040040, 0x10000040,
        0x10040000, 0x10001000, 0x10001040, 0x00000000,
        0x10041040, 0x00041000, 0x00041000, 0x00001040,
        0x00001040, 0x00040040, 0x10000000, 0x10041000
    };
    
    /*
     * PC1: left and right halves bit-swap
     */
    static const uint32_t LHs[16] =
    {
        0x00000000, 0x00000001, 0x00000100, 0x00000101,
        0x00010000, 0x00010001, 0x00010100, 0x00010101,
        0x01000000, 0x01000001, 0x01000100, 0x01000101,
        0x01010000, 0x01010001, 0x01010100, 0x01010101
    };
    
    static const uint32_t RHs[16] =
    {
        0x00000000, 0x01000000, 0x00010000, 0x01010000,
        0x00000100, 0x01000100, 0x00010100, 0x01010100,
        0x00000001, 0x01000001, 0x00010001, 0x01010001,
        0x00000101, 0x01000101, 0x00010101, 0x01010101,
    };
    
    /*
     * Initial Permutation macro
     */
    #define DES_IP(X,Y)                                             \
    {                                                               \
        T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
        T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
        T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
        T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
        Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF;                    \
        T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T;                   \
        X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF;                    \
    }
    
    /*
     * Final Permutation macro
     */
    #define DES_FP(X,Y)                                             \
    {                                                               \
        X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF;                    \
        T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T;                   \
        Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF;                    \
        T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
        T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
        T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
        T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
    }
    
    /*
     * DES round macro
     */
    #define DES_ROUND(X,Y)                          \
    {                                               \
        T = *SK++ ^ X;                              \
        Y ^= SB8[ (T      ) & 0x3F ] ^              \
             SB6[ (T >>  8) & 0x3F ] ^              \
             SB4[ (T >> 16) & 0x3F ] ^              \
             SB2[ (T >> 24) & 0x3F ];               \
                                                    \
        T = *SK++ ^ ((X << 28) | (X >> 4));         \
        Y ^= SB7[ (T      ) & 0x3F ] ^              \
             SB5[ (T >>  8) & 0x3F ] ^              \
             SB3[ (T >> 16) & 0x3F ] ^              \
             SB1[ (T >> 24) & 0x3F ];               \
    }
    
    #define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
    
    void des_init( des_context *ctx )
    {
        memset( ctx, 0, sizeof( des_context ) );
    }
    
    void des_free( des_context *ctx )
    {
        if( ctx == NULL )
            return;
    
        zeroize( ctx, sizeof( des_context ) );
    }
    
    void des3_init( des3_context *ctx )
    {
        memset( ctx, 0, sizeof( des3_context ) );
    }
    
    void des3_free( des3_context *ctx )
    {
        if( ctx == NULL )
            return;
    
        zeroize( ctx, sizeof( des3_context ) );
    }
    
    static const unsigned char odd_parity_table[128] = { 1,  2,  4,  7,  8,
            11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
            47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
            82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
            115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
            143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
            171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
            199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
            227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
            254 };
    
    void des_key_set_parity( unsigned char key[DES_KEY_SIZE] )
    {
        int i;
    
        for( i = 0; i < DES_KEY_SIZE; i++ )
            key[i] = odd_parity_table[key[i] / 2];
    }
    
    /*
     * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
     */
    int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] )
    {
        int i;
    
        for( i = 0; i < DES_KEY_SIZE; i++ )
            if( key[i] != odd_parity_table[key[i] / 2] )
                return( 1 );
    
        return( 0 );
    }
    
    /*
     * Table of weak and semi-weak keys
     *
     * Source: http://en.wikipedia.org/wiki/Weak_key
     *
     * Weak:
     * Alternating ones + zeros (0x0101010101010101)
     * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
     * '0xE0E0E0E0F1F1F1F1'
     * '0x1F1F1F1F0E0E0E0E'
     *
     * Semi-weak:
     * 0x011F011F010E010E and 0x1F011F010E010E01
     * 0x01E001E001F101F1 and 0xE001E001F101F101
     * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
     * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
     * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
     * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
     *
     */
    
    #define WEAK_KEY_COUNT 16
    
    static const unsigned char weak_key_table[WEAK_KEY_COUNT][DES_KEY_SIZE] =
    {
        { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
        { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
        { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
        { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
    
        { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
        { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
        { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
        { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
        { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
        { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
        { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
        { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
        { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
        { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
        { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
        { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
    };
    
    int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] )
    {
        int i;
    
        for( i = 0; i < WEAK_KEY_COUNT; i++ )
            if( memcmp( weak_key_table[i], key, DES_KEY_SIZE) == 0 )
                return( 1 );
    
        return( 0 );
    }
    
    
    void des_setkey( uint32_t SK[32], const unsigned char key[DES_KEY_SIZE] )
    {
        int i;
        uint32_t X, Y, T;
    
        GET_UINT32_BE( X, key, 0 );
        GET_UINT32_BE( Y, key, 4 );
    
        /*
         * Permuted Choice 1
         */
        T =  ((Y >>  4) ^ X) & 0x0F0F0F0F;  X ^= T; Y ^= (T <<  4);
        T =  ((Y      ) ^ X) & 0x10101010;  X ^= T; Y ^= (T      );
    
        X =   (LHs[ (X      ) & 0xF] << 3) | (LHs[ (X >>  8) & 0xF ] << 2)
            | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ]     )
            | (LHs[ (X >>  5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
            | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
    
        Y =   (RHs[ (Y >>  1) & 0xF] << 3) | (RHs[ (Y >>  9) & 0xF ] << 2)
            | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ]     )
            | (RHs[ (Y >>  4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
            | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
    
        X &= 0x0FFFFFFF;
        Y &= 0x0FFFFFFF;
    
        /*
         * calculate subkeys
         */
        for( i = 0; i < 16; i++ )
        {
            if( i < 2 || i == 8 || i == 15 )
            {
                X = ((X <<  1) | (X >> 27)) & 0x0FFFFFFF;
                Y = ((Y <<  1) | (Y >> 27)) & 0x0FFFFFFF;
            }
            else
            {
                X = ((X <<  2) | (X >> 26)) & 0x0FFFFFFF;
                Y = ((Y <<  2) | (Y >> 26)) & 0x0FFFFFFF;
            }
    
            *SK++ =   ((X <<  4) & 0x24000000) | ((X << 28) & 0x10000000)
                    | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
                    | ((X <<  6) & 0x01000000) | ((X <<  9) & 0x00200000)
                    | ((X >>  1) & 0x00100000) | ((X << 10) & 0x00040000)
                    | ((X <<  2) & 0x00020000) | ((X >> 10) & 0x00010000)
                    | ((Y >> 13) & 0x00002000) | ((Y >>  4) & 0x00001000)
                    | ((Y <<  6) & 0x00000800) | ((Y >>  1) & 0x00000400)
                    | ((Y >> 14) & 0x00000200) | ((Y      ) & 0x00000100)
                    | ((Y >>  5) & 0x00000020) | ((Y >> 10) & 0x00000010)
                    | ((Y >>  3) & 0x00000008) | ((Y >> 18) & 0x00000004)
                    | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
    
            *SK++ =   ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
                    | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
                    | ((X >>  2) & 0x02000000) | ((X <<  1) & 0x01000000)
                    | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
                    | ((X <<  3) & 0x00080000) | ((X >>  6) & 0x00040000)
                    | ((X << 15) & 0x00020000) | ((X >>  4) & 0x00010000)
                    | ((Y >>  2) & 0x00002000) | ((Y <<  8) & 0x00001000)
                    | ((Y >> 14) & 0x00000808) | ((Y >>  9) & 0x00000400)
                    | ((Y      ) & 0x00000200) | ((Y <<  7) & 0x00000100)
                    | ((Y >>  7) & 0x00000020) | ((Y >>  3) & 0x00000011)
                    | ((Y <<  2) & 0x00000004) | ((Y >> 21) & 0x00000002);
        }
    }
    
    
    /*
     * DES key schedule (56-bit, encryption)
     */
    int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] )
    {
        des_setkey( ctx->sk, key );
    
        return( 0 );
    }
    
    /*
     * DES key schedule (56-bit, decryption)
     */
    int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] )
    {
        int i;
    
        des_setkey( ctx->sk, key );
    
        for( i = 0; i < 16; i += 2 )
        {
            SWAP( ctx->sk[i    ], ctx->sk[30 - i] );
            SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
        }
    
        return( 0 );
    }
    
    static void des3_set2key( uint32_t esk[96],
                              uint32_t dsk[96],
                              const unsigned char key[DES_KEY_SIZE*2] )
    {
        int i;
    
        des_setkey( esk, key );
        des_setkey( dsk + 32, key + 8 );
    
        for( i = 0; i < 32; i += 2 )
        {
            dsk[i     ] = esk[30 - i];
            dsk[i +  1] = esk[31 - i];
    
            esk[i + 32] = dsk[62 - i];
            esk[i + 33] = dsk[63 - i];
    
            esk[i + 64] = esk[i    ];
            esk[i + 65] = esk[i + 1];
    
            dsk[i + 64] = dsk[i    ];
            dsk[i + 65] = dsk[i + 1];
        }
    }
    
    /*
     * Triple-DES key schedule (112-bit, encryption)
     */
    int des3_set2key_enc( des3_context *ctx,
                          const unsigned char key[DES_KEY_SIZE * 2] )
    {
        uint32_t sk[96];
    
        des3_set2key( ctx->sk, sk, key );
        zeroize( sk,  sizeof( sk ) );
    
        return( 0 );
    }
    
    /*
     * Triple-DES key schedule (112-bit, decryption)
     */
    int des3_set2key_dec( des3_context *ctx,
                          const unsigned char key[DES_KEY_SIZE * 2] )
    {
        uint32_t sk[96];
    
        des3_set2key( sk, ctx->sk, key );
        zeroize( sk,  sizeof( sk ) );
    
        return( 0 );
    }
    
    static void des3_set3key( uint32_t esk[96],
                              uint32_t dsk[96],
                              const unsigned char key[24] )
    {
        int i;
    
        des_setkey( esk, key );
        des_setkey( dsk + 32, key +  8 );
        des_setkey( esk + 64, key + 16 );
    
        for( i = 0; i < 32; i += 2 )
        {
            dsk[i     ] = esk[94 - i];
            dsk[i +  1] = esk[95 - i];
    
            esk[i + 32] = dsk[62 - i];
            esk[i + 33] = dsk[63 - i];
    
            dsk[i + 64] = esk[30 - i];
            dsk[i + 65] = esk[31 - i];
        }
    }
    
    /*
     * Triple-DES key schedule (168-bit, encryption)
     */
    int des3_set3key_enc( des3_context *ctx,
                          const unsigned char key[DES_KEY_SIZE * 3] )
    {
        uint32_t sk[96];
    
        des3_set3key( ctx->sk, sk, key );
        zeroize( sk,  sizeof( sk ) );
    
        return( 0 );
    }
    
    /*
     * Triple-DES key schedule (168-bit, decryption)
     */
    int des3_set3key_dec( des3_context *ctx,
                          const unsigned char key[DES_KEY_SIZE * 3] )
    {
        uint32_t sk[96];
    
        des3_set3key( sk, ctx->sk, key );
        zeroize( sk,  sizeof( sk ) );
    
        return( 0 );
    }
    
    /*
     * DES-ECB block encryption/decryption
     */
    
    int des_crypt_ecb( des_context *ctx,
                        const unsigned char input[8],
                        unsigned char output[8] )
    {
        int i;
        uint32_t X, Y, T, *SK;
    
        SK = ctx->sk;
    
        GET_UINT32_BE( X, input, 0 );
        GET_UINT32_BE( Y, input, 4 );
    
        DES_IP( X, Y );
    
        for( i = 0; i < 8; i++ )
        {
            DES_ROUND( Y, X );
            DES_ROUND( X, Y );
        }
    
        DES_FP( Y, X );
    
        PUT_UINT32_BE( Y, output, 0 );
        PUT_UINT32_BE( X, output, 4 );
    
        return( 0 );
    }
    
    
    
    /*
     * DES-CBC buffer encryption/decryption
     */
    int des_crypt_cbc( des_context *ctx,
                        int mode,
                        size_t length,
                        unsigned char iv[8],
                        const unsigned char *input,
                        unsigned char *output )
    {
        int i;
        unsigned char temp[8];
    
        if( length % 8 )
            return( ERR_DES_INVALID_INPUT_LENGTH );
    
        if( mode == DES_ENCRYPT )
        {
            while( length > 0 )
            {
                for( i = 0; i < 8; i++ )
                    output[i] = (unsigned char)( input[i] ^ iv[i] );
    
                des_crypt_ecb( ctx, output, output );
                memcpy( iv, output, 8 );
    
                input  += 8;
                output += 8;
                length -= 8;
            }
        }
        else /* DES_DECRYPT */
        {
            while( length > 0 )
            {
                memcpy( temp, input, 8 );
                des_crypt_ecb( ctx, input, output );
    
                for( i = 0; i < 8; i++ )
                    output[i] = (unsigned char)( output[i] ^ iv[i] );
    
                memcpy( iv, temp, 8 );
    
                input  += 8;
                output += 8;
                length -= 8;
            }
        }
    
        return( 0 );
    }
    
    
    /*
     * 3DES-ECB block encryption/decryption
     */
    
    int des3_crypt_ecb( des3_context *ctx,
                         const unsigned char input[8],
                         unsigned char output[8] )
    {
        int i;
        uint32_t X, Y, T, *SK;
    
        SK = ctx->sk;
    
        GET_UINT32_BE( X, input, 0 );
        GET_UINT32_BE( Y, input, 4 );
    
        DES_IP( X, Y );
    
        for( i = 0; i < 8; i++ )
        {
            DES_ROUND( Y, X );
            DES_ROUND( X, Y );
        }
    
        for( i = 0; i < 8; i++ )
        {
            DES_ROUND( X, Y );
            DES_ROUND( Y, X );
        }
    
        for( i = 0; i < 8; i++ )
        {
            DES_ROUND( Y, X );
            DES_ROUND( X, Y );
        }
    
        DES_FP( Y, X );
    
        PUT_UINT32_BE( Y, output, 0 );
        PUT_UINT32_BE( X, output, 4 );
    
        return( 0 );
    }
    
    
    
    /*
     * 3DES-CBC buffer encryption/decryption
     */
    int des3_crypt_cbc( des3_context *ctx,
                         int mode,
                         size_t length,
                         unsigned char iv[8],
                         const unsigned char *input,
                         unsigned char *output )
    {
        int i;
        unsigned char temp[8];
    
        if( length % 8 )
            return( ERR_DES_INVALID_INPUT_LENGTH );
    
        if( mode == DES_ENCRYPT )
        {
            while( length > 0 )
            {
                for( i = 0; i < 8; i++ )
                    output[i] = (unsigned char)( input[i] ^ iv[i] );
    
                des3_crypt_ecb( ctx, output, output );
                memcpy( iv, output, 8 );
    
                input  += 8;
                output += 8;
                length -= 8;
            }
        }
        else /* DES_DECRYPT */
        {
            while( length > 0 )
            {
                memcpy( temp, input, 8 );
                des3_crypt_ecb( ctx, input, output );
    
                for( i = 0; i < 8; i++ )
                    output[i] = (unsigned char)( output[i] ^ iv[i] );
    
                memcpy( iv, temp, 8 );
    
                input  += 8;
                output += 8;
                length -= 8;
            }
        }
    
        return( 0 );
    }
    
    
    #endif /* !DES_ALT */
    
    #endif /* DES_C */
    
    /*
     * DES-ECB buffer encryption API
     */
    unsigned int des_ecb_encrypt(unsigned char *pout,
    							 unsigned char *pdata,
    							 unsigned int nlen,
    							 unsigned char *pkey)
    {
        unsigned char *tmp;
        unsigned int len,i;
        unsigned char ch = '\0';
        des_context ctx;
    
        des_setkey_enc( &ctx, pkey );
    
        len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
    
    	//ch = 8 - nlen % 8;
        for(i = 0;i < nlen;i += 8)
        {
            des_crypt_ecb( &ctx, (pdata + i), (pout + i) );
        }
        if(len > nlen)
        {
    		tmp = (unsigned char *)malloc(len);
            i -= 8;
            memcpy(tmp,pdata + i,nlen - i);
            memset(tmp + nlen % 8, ch, (8 - nlen % 8) % 8);
            des_crypt_ecb( &ctx, tmp, (pout + i));
    		free(tmp);
        }
    	
        des_free( &ctx );
        return len;
    
    
    }
    /*
     * DES-ECB buffer decryption API
     */
    unsigned int des_ecb_decrypt(unsigned char *pout,
    							 unsigned char *pdata,
    							 unsigned int nlen,
    							 unsigned char *pkey)
    {
    
        unsigned int i;
        des_context ctx;
    
    	if(nlen % 8)
            return 1;
    
        des_setkey_dec( &ctx, pkey );
    
    
        for(i = 0;i < nlen;i += 8)
        {
            des_crypt_ecb(&ctx, (pdata + i), (pout + i));
        }
        des_free( &ctx );
        return 0;
    
    }
    
    /*
     * DES-CBC buffer encryption API
     */
    unsigned int des_cbc_encrypt(unsigned char *pout,
    							 unsigned char *pdata,
    							 unsigned int nlen,
    							 unsigned char *pkey,
    							 unsigned char *piv)
    {
        des_context ctx;
        unsigned char iv[8] = {0};
        unsigned char *pivb;
    
        if(piv == NULL)
    		pivb = iv;
        else
        	pivb = piv;
    
        des_setkey_enc( &ctx, pkey );
    
        des_crypt_cbc( &ctx, 1, nlen, pivb, pdata, (pout));
    	
        des_free( &ctx );
        
        return nlen;
    
    
    }
    /*
     * DES-CBC buffer decryption API
     */
    unsigned int des_cbc_decrypt(unsigned char *pout,
    							 unsigned char *pdata,
    							 unsigned int nlen,
    							 unsigned char *pkey,
    							 unsigned char *piv)
    {
    
        des_context ctx;
        unsigned char iv[8] = {0};
        unsigned char *pivb;
    
        if(piv == NULL)
    		pivb = iv;
        else
        	pivb = piv;
    
        des_setkey_dec( &ctx, pkey );
    
        des_crypt_cbc( &ctx, 0, nlen, pivb, pdata, (pout));
    	
        des_free( &ctx );
        
        return 0;
    
    }
    /*
     * 3DES-ECB buffer encryption API
     */
    unsigned int des3_ecb_encrypt(unsigned char *pout,
    							  unsigned char *pdata,
    							  unsigned int nlen,
    							  unsigned char *pkey,
    							  unsigned int klen)
    {
        unsigned char *tmp;
        unsigned int len,i;
        unsigned char ch = '\0';
        des3_context ctx3;
    
    	if(klen == DES3_KEY2_SIZE)//16字节
    		des3_set2key_enc( &ctx3, pkey );//根据长度设置key
    	else if(klen == DES3_KEY3_SIZE)//24字节
    		des3_set3key_enc( &ctx3, pkey );
    
        len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
    
    	//ch = 8 - nlen % 8;//可以设置补齐内容,常用0或0xFF
        for(i = 0;i < nlen;i += 8)
        {
            des3_crypt_ecb( &ctx3, (pdata + i), (pout + i) );
        }
        if(len > nlen)//不足8字节补齐
        {
    		tmp = (unsigned char *)malloc(len);
            i -= 8;
            memcpy(tmp,pdata + i,nlen - i);
            memset(tmp + nlen % 8, ch, (8 - nlen % 8) % 8);
            des3_crypt_ecb( &ctx3, tmp, (pout + i));
    		free(tmp);
        }
    	
        des3_free( &ctx3 );
        return len;
    
    
    }
    /*
     * 3DES-ECB buffer decryption API
     */
    unsigned int des3_ecb_decrypt(unsigned char *pout,
    							  unsigned char *pdata,
    							  unsigned int nlen,
    							  unsigned char *pkey,
    							  unsigned int klen)
    {
    
        unsigned int i;
        des3_context ctx3;
    
    	if(nlen % 8)
            return 1;
    
        if(klen == DES3_KEY2_SIZE)
    		des3_set2key_dec( &ctx3, pkey );
    	else if(klen == DES3_KEY3_SIZE)
    		des3_set3key_dec( &ctx3, pkey );
    
    
        for(i = 0;i < nlen;i += 8)
        {
            des3_crypt_ecb(&ctx3, (pdata + i), (pout + i));
        }
        des3_free( &ctx3 );
        return 0;
    
    }
    /*
     * 3DES-CBC buffer encryption API
     */
    unsigned int des3_cbc_encrypt(unsigned char *pout,
    							  unsigned char *pdata,
    							  unsigned int nlen,
    							  unsigned char *pkey,
    							  unsigned int klen,
    							  unsigned char *piv)
    {
        des3_context ctx;
        unsigned char iv[8] = {0};
        unsigned char *pivb;
        unsigned char *tmp;
        unsigned int len;
    
        if(piv == NULL)
    		pivb = iv;
        else
        	pivb = piv;
    
        if(klen == DES3_KEY2_SIZE)
    		des3_set2key_enc( &ctx, pkey );
    	else if(klen == DES3_KEY3_SIZE)
    		des3_set3key_enc( &ctx, pkey );
    
        if(nlen % 8)
    	{
    		len = nlen + 8 - nlen % 8;
    		tmp = (unsigned char *)calloc(1, len);
    		memcpy(tmp, pdata, nlen);
    		des3_crypt_cbc( &ctx, 1, len, pivb, tmp, (pout));
    		free(tmp);
    	}
    	else
    	{
    		des3_crypt_cbc( &ctx, 1, nlen, pivb, pdata, (pout));
    	}
    	
        des3_free( &ctx );
        
        return nlen;
    
    
    }
    /*
     * 3DES-CBC buffer decryption API
     */
    unsigned int des3_cbc_decrypt(unsigned char *pout,
    							  unsigned char *pdata,
    							  unsigned int nlen,
    							  unsigned char *pkey,
    							  unsigned int klen,
    							  unsigned char *piv)
    {
    
        des3_context ctx;
        unsigned char iv[8] = {0};
        unsigned char *pivb;
    
    	if(nlen % 8)
    		return 1;
    		
        if(piv == NULL)
    		pivb = iv;
        else
        	pivb = piv;
    
    
        if(klen == DES3_KEY2_SIZE)
    		des3_set2key_dec( &ctx, pkey );
    	else if(klen == DES3_KEY3_SIZE)
    		des3_set3key_dec( &ctx, pkey );
    
        des3_crypt_cbc( &ctx, 0, nlen, pivb, pdata, (pout));
    	
        des3_free( &ctx );
        
        return 0;
    
    }
    

    这些函数接口可以直接调用,很方便,代码内容也比较清晰,结合DES的算法原理很容易理解

    //main函数测试
    int des_test_self()
    {
    	unsigned char buff[1024] = {0};
    	unsigned char data[1024] = {0x3F,0x12,0xE7,0xC0,0x2D,0x66,0x5A,0xB0,0xC4,0x2E,0x58,0xF1};
    	int ret,len,i;
    	
    //	len = MyStrToHex("3F12E7C02D665AB0C42E58F1", data);//不满8字节
    	len = strlen((char*)data);
    	for(i = 0;i < len;i++)
        {
            printf("%02X",data[i]);
        }
        printf("\r\n");
    
    	unsigned char key[16] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
    	//DES ECB 加密
    	printf("DES ECB ENC::\r\n");
    	ret = des_ecb_encrypt(buff,data,len,key);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",buff[i]);
        }
        printf("\r\n");
    	//DES ECB 解密
    	printf("DES ECB DEC::\r\n");
    	memset(data,0,sizeof(data));
    	des_ecb_decrypt(data,buff,ret,key);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",data[i]);
        }
        printf("\r\n");
    
    	//DES CBC 加密
    	printf("DES CBC ENC::\r\n");
    	memset(buff,0,sizeof(buff));
    	des_cbc_encrypt(buff,data,ret,key,NULL);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",buff[i]);
        }
        printf("\r\n");
    
    	//DES CBC 解密
    	printf("DES CBC DEC::\r\n");
    	memset(data,0,sizeof(data));
    	des_cbc_decrypt(data,buff,ret,key,NULL);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",data[i]);
        }
        printf("\r\n");
    	printf("\r\n");
    
    
    	//3DES ECB 加密
    	printf("3DES ECB ENC::\r\n");
    	ret = des3_ecb_encrypt(buff,data,len,key,16);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",buff[i]);
        }
        printf("\r\n");
    	//3DES ECB 解密
    	printf("3DES ECB DEC::\r\n");
    	memset(data,0,sizeof(data));
    	des3_ecb_decrypt(data,buff,ret,key,16);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",data[i]);
        }
        printf("\r\n");
    
    	//3DES CBC 加密
    	printf("3DES CBC ENC::\r\n");
    	memset(buff,0,sizeof(buff));
    	des3_cbc_encrypt(buff,data,ret,key,16,NULL);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",buff[i]);
        }
        printf("\r\n");
    
    	//3DES CBC 解密
    	printf("3DES CBC DEC::\r\n");
    	memset(data,0,sizeof(data));
    	des3_cbc_decrypt(data,buff,ret,key,16,NULL);
    	for(i = 0;i < ret;i++)
        {
            printf("%02X",data[i]);
        }
        printf("\r\n");
    
    
    	return 0;
    }
    

    具体验证使用这个工具,内含DES和3DES的CBC算法的计算,和后边的博客中提到的其他算法的工具合集
    加密解密算法工具集

    展开全文
  • 3des加密C语言实现

    2013-12-20 14:26:58
    3des加密C语言实现,里面是用C语言实现3DES加密
  • 转自:... 版权声明:本文为博主原创文章,未经博主允许不得转载。... DES和3DES加密算法C语言实现 记录DES和3DES加密算法最简洁易懂的C语言源码 主要是要用到C...

    转自: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; /** * \brief 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, 

    转载于:https://www.cnblogs.com/sky-heaven/p/9772867.html

    展开全文
  • 基于STM32的软件加解密算法,包括DES3DES的ECB,CBC模式。但是验证时CBC模式的初始向量为0时,数据的加解密正确,但是初始向量不为0时,则加解数据有错误。 注意:经测试DES3DES的CBC模式初始向量不为0时,加...
  • 用vs2012写的,可以直接运行想要代码的话,都在一个cpp文件中
  • 3des加密C语言算法

    2014-11-12 10:54:15
    3des加密算法C语言实现,很好很实用
  • 用C语言实现的128位AES加密算法,可以运行在JAVA的JNI 中AIS加密算法c语言实现代码 nt cnt for(ent =0: cnt< 8: cnt++) BvtcToBit(*(ch+cnt), bit+(ent<<3)) return /将二进制位串转为长度为8的字符串水 ...
  • 基于FPGA的DES加密算法实现、电子技术,开发板制作交流
  • //3DES算法 unsigned char D3DES_Encrypt(unsigned char *plainBuffer, unsigned char *keyBuffer, unsigned char *cipherBuffer);//加密数据 unsigned char D3DES_Decrypt(unsigned char *cipherBuffer, unsigned ...
  • 已编译通过能直接运行,可实现DES加密解密。zip内有函数调用大概流程图,较为简单好懂的C语言实现方法。
  • #include /*DES VERSION0.4 CREATED BY SIMPLE SOURCE AT 2007.3 *//*COMPETENCE :* P4 3.0G 512M*3000000 TIMES ENCRYPTION WITH CREATEDSUBKEYS*26.784 SECONDS(ABOUT 0.85M/S)*/static __int64 g_arrayMas...

    #include

    /* DES VERSION 0.4 CREATED BY SIMPLE SOURCE AT 2007.3 */

    /* COMPETENCE :

    * P4 3.0G 512M

    * 3000000 TIMES ENCRYPTION WITH CREATED SUBKEYS

    * 26.784 SECONDS (ABOUT 0.85M/S)*/

    static __int64 g_arrayMask[64] =

    {

    0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x0000000000000008,

    0x0000000000000010, 0x0000000000000020, 0x0000000000000040, 0x0000000000000080,

    0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x0000000000000800,

    0x0000000000001000, 0x0000000000002000, 0x0000000000004000, 0x0000000000008000,

    0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x0000000000080000,

    0x0000000000100000, 0x0000000000200000, 0x0000000000400000, 0x0000000000800000,

    0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x0000000008000000,

    0x0000000010000000, 0x0000000020000000, 0x0000000040000000, 0x0000000080000000,

    0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x0000000800000000,

    0x0000001000000000, 0x0000002000000000, 0x0000004000000000, 0x0000008000000000,

    0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x0000080000000000,

    0x0000100000000000, 0x0000200000000000, 0x0000400000000000, 0x0000800000000000,

    0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x0008000000000000,

    0x0010000000000000, 0x0020000000000000, 0x0040000000000000, 0x0080000000000000,

    0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x0800000000000000,

    0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000

    };

    static int g_arrayIP[64] =

    {

    57, 49, 41, 33, 25, 17,  9,  1,

    59, 51, 43, 35, 27, 19, 11,  3,

    61, 53, 45, 37, 29, 21, 13,  5,

    63, 55, 47, 39, 31, 23, 15,  7,

    56, 48, 40, 32, 24, 16,  8,  0,

    58, 50, 42, 34, 26, 18, 10,  2,

    60, 52, 44, 36, 28, 20, 12,  4,

    62, 54, 46, 38, 30, 22, 14,  6

    };

    static int g_arrayE[64] =

    {

    31,  0,  1,  2,  3,  4, -1, -1,

    3,  4,  5,  6,  7,  8, -1, -1,

    7,  8,  9, 10, 11, 12, -1, -1,

    11, 12, 13, 14, 15, 16, -1, -1,

    15, 16, 17, 18, 19, 20, -1, -1,

    19, 20, 21, 22, 23, 24, -1, -1,

    23, 24, 25, 26, 27, 28, -1, -1,

    27, 28, 29, 30, 31, 30, -1, -1

    };

    static char g_matrixNSBox[8][64] =

    {

    {

    14,  4,  3, 15,  2, 13,  5,  3,

    13, 14,  6,  9, 11,  2,  0,  5,

    4,  1, 10, 12, 15,  6,  9, 10,

    1,  8, 12,  7,  8, 11,  7,  0,

    0, 15, 10,  5, 14,  4,  9, 10,

    7,  8, 12,  3, 13,  1,  3,  6,

    15, 12,  6, 11,  2,  9,  5,  0,

    4,  2, 11, 14,  1,  7,  8, 13

    },

    {

    15,  0,  9,  5,  6, 10, 12,  9,

    8,  7,  2, 12,  3, 13,  5,  2,

    1, 14,  7,  8, 11,  4,  0,  3,

    14, 11, 13,  6,  4,  1, 10, 15,

    3, 13, 12, 11, 15,  3,  6,  0,

    4, 10,  1,  7,  8,  4, 11, 14,

    13,  8,  0,  6,  2, 15,  9,  5,

    7,  1, 10, 12, 14,  2,  5,  9

    },

    {

    10, 13,  1, 11,  6,  8, 11,  5,

    9,  4, 12,  2, 15,  3,  2, 14,

    0,  6, 13,  1,  3, 15,  4, 10,

    14,  9,  7, 12,  5,  0,  8,  7,

    13,  1,  2,  4,  3,  6, 12, 11,

    0, 13,  5, 14,  6,  8, 15,  2,

    7, 10,  8, 15,  4,  9, 11,  5,

    9,  0, 14,  3, 10,  7,  1, 12

    },

    {

    7, 10,  1, 15,  0, 12, 11,  5,

    14,  9,  8,  3,  9,  7,  4,  8,

    13,  6,  2,  1,  6, 11, 12,  2,

    3,  0,  5, 14, 10, 13, 15,  4,

    13,  3,  4,  9,  6, 10,  1, 12,

    11,  0,  2,  5,  0, 13, 14,  2,

    8, 15,  7,  4, 15,  1, 10,  7,

    5,  6, 12, 11,  3,  8,  9, 14

    },

    {

    2,  4,  8, 15,  7, 10, 13,  6,

    4,  1,  3, 12, 11,  7, 14,  0,

    12,  2,  5,  9, 10, 13,  0,  3,

    1, 11, 15,  5,  6,  8,  9, 14,

    14, 11,  5,  6,  4,  1,  3, 10,

    2, 12, 15,  0, 13,  2,  8,  5,

    11,  8,  0, 15,  7, 14,  9,  4,

    12,  7, 10,  9,  1, 13,  6,  3

    },

    {

    12,  9,  0,  7,  9,  2, 14,  1,

    10, 15,  3,  4,  6, 12,  5, 11,

    1, 14, 13,  0,  2,  8,  7, 13,

    15,  5,  4, 10,  8,  3, 11,  6,

    10,  4,  6, 11,  7,  9,  0,  6,

    4,  2, 13,  1,  9, 15,  3,  8,

    15,  3,  1, 14, 12,  5, 11,  0,

    2, 12, 14,  7,  5, 10,  8, 13

    },

    {

    4,  1,  3, 10, 15, 12,  5,  0,

    2, 11,  9,  6,  8,  7,  6,  9,

    11,  4, 12, 15,  0,  3, 10,  5,

    14, 13,  7,  8, 13, 14,  1,  2,

    13,  6, 14,  9,  4,  1,  2, 14,

    11, 13,  5,  0,  1, 10,  8,  3,

    0, 11,  3,  5,  9,  4, 15,  2,

    7,  8, 12, 15, 10,  7,  6, 12

    },

    {

    13,  7, 10,  0,  6,  9,  5, 15,

    8,  4,  3, 10, 11, 14, 12,  5,

    2, 11,  9,  6, 15, 12,  0,  3,

    4,  1, 14, 13,  1,  2,  7,  8,

    1,  2, 12, 15, 10,  4,  0,  3,

    13, 14,  6,  9,  7,  8,  9,  6,

    15,  1,  5, 12,  3, 10, 14,  5,

    8,  7, 11,  0,  4, 13,  2, 11

    },

    };

    static int g_arrayP[32] =

    {

    15,  6, 19, 20, 28, 11, 27, 16,

    0, 14, 22, 25,  4, 17, 30,  9,

    1,  7, 23, 13, 31, 26,  2,  8,

    18, 12, 29,  5, 21, 10,  3, 24

    };

    static int g_arrayIP_1[64] =

    {

    39,  7, 47, 15, 55, 23, 63, 31,

    38,  6, 46, 14, 54, 22, 62, 30,

    37,  5, 45, 13, 53, 21, 61, 29,

    36,  4, 44, 12, 52, 20, 60, 28,

    35,  3, 43, 11, 51, 19, 59, 27,

    34,  2, 42, 10, 50, 18, 58, 26,

    33,  1, 41,  9, 49, 17, 57, 25,

    32,  0, 40,  8, 48, 16, 56, 24

    };

    static int g_arrayPC_1[56] =

    {

    56, 48, 40, 32, 24, 16,  8,

    0, 57, 49, 41, 33, 25, 17,

    9,  1, 58, 50, 42, 34, 26,

    18, 10,  2, 59, 51, 43, 35,

    62, 54, 46, 38, 30, 22, 14,

    6, 61, 53, 45, 37, 29, 21,

    13,  5, 60, 52, 44, 36, 28,

    20, 12,  4, 27, 19, 11,  3

    };

    static int g_arrayPC_2[64] =

    {

    13, 16, 10, 23,  0,  4, -1, -1,

    2, 27, 14,  5, 20,  9, -1, -1,

    22, 18, 11,  3, 25,  7, -1, -1,

    15,  6, 26, 19, 12,  1, -1, -1,

    40, 51, 30, 36, 46, 54, -1, -1,

    29, 39, 50, 44, 32, 47, -1, -1,

    43, 48, 38, 55, 33, 52, -1, -1,

    45, 41, 49, 35, 28, 31, -1, -1

    };

    static int g_arrayLs[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};

    static __int64 g_arrayLsMask[3] =

    {

    0x0000000000000000,

    0x0000000000100001,

    0x0000000000300003

    };

    #define BitTransform(array, len, source, dest) /

    {/

    __int64 bts = source;/

    int bti;/

    dest = 0;/

    for(bti = 0; bti < len; bti++)/

    {/

    if(array[bti] >= 0 && (bts & g_arrayMask[array[bti]]))/

    {/

    dest |= g_arrayMask[bti];/

    }/

    }/

    }

    #define DES_MODE_ENCRYPT        0

    #define DES_MODE_DECRYPT        1

    void DESSubKeys(__int64 key, __int64 K[16], int mode);

    __int64 DES64(__int64 subkeys[16], __int64 data);

    void DESSubKeys(__int64 key, __int64 K[16], int mode)

    {

    __int64 temp;

    int j;

    /* PC-1变换 */

    BitTransform(g_arrayPC_1, 56, key, temp);

    for(j = 0; j < 16; j++)

    {

    /* 循环左移 */

    {

    __int64 source = temp;

    temp = ((source & g_arrayLsMask[g_arrayLs[j]]) << (28 - g_arrayLs[j])) | ((source & ~g_arrayLsMask[g_arrayLs[j]]) >> g_arrayLs[j]);

    }

    /* PC-2变换 */

    BitTransform(g_arrayPC_2, 64, temp, K[j]);

    }

    if(mode == DES_MODE_DECRYPT) /* 如果解密则反转子密钥顺序 */

    {

    __int64 t;

    for(j = 0; j < 8; j++)

    {

    t = K[j];

    K[j] = K[15 - j];

    K[15 - j] = t;

    }

    }

    }

    __int64 DES64(__int64 subkeys[16], __int64 data)

    {

    static __int64 out;

    static __int64 source;

    static __int64 L, R;

    static __int32 * pSource;

    static char * pR;

    static int i;

    static __int32 SOut;

    static __int32 t;

    static int sbi;

    pSource = (__int32 *)&out;

    pR = (char *)&R;

    /* IP变换 */

    {

    BitTransform(g_arrayIP, 64, data, out);

    }

    /* 主迭代 */

    {

    //source = out;

    for(i = 0; i < 16; i++)

    {

    R = pSource[1];

    /* F变换开始 */

    {

    /* E变换 */

    {

    BitTransform(g_arrayE, 64, R, R);

    }

    /* 与子密钥异或 */

    R ^= subkeys[i];

    /* S盒变换 */

    {

    SOut = 0;

    for(sbi = 7; sbi >= 0; sbi--)

    {

    SOut <<= 4;

    SOut |= g_matrixNSBox[sbi][pR[sbi]];

    }

    R = SOut;

    }

    /* P变换 */

    {

    BitTransform(g_arrayP, 32, R, R);

    }

    }

    /* f变换完成 */

    L = pSource[0];

    pSource[0] = pSource[1];

    pSource[1] = (__int32)(L ^ R);

    }

    /* 交换高低32位 */

    {

    t = pSource[0];

    pSource[0] = pSource[1];

    pSource[1] = t;

    }

    }

    /* IP-1变换 */    {        BitTransform(g_arrayIP_1, 64, out, out);    }    return out;}

    展开全文
  • 采用C语言实现DES加密算法,支持文件的加解密,其中包含源代码及可执行程序。
  • 它的出现主要是为了取代DES加密算法的,因为我们都知道DES算法的密钥长度是56Bit,因此算法的理论安全强度是2的56次方。 AES密码与分组密码Rijndael基本上完全一致,Rijndael分组大小和密钥大小都可以为128位、192...
  • DES-加密解密语言算法C语言实现文档带有完整的源代码,可加密字符串或者文件,已通过测试使用。
  • C语言版本的3DES加密解密算法(用DEV C++编译和运行过代码),纯粹的C语言版本,我自己也编译通过,利用DEV C++运行过.希望给大家以帮助!
  • 3DES加密算法

    2021-05-24 05:42:56
    它以DES为基本模块,通过组合分组方法设计出分组加密算法,其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密表,这样, 3DES加密过程为:C=Ek3(Dk2(Ek1(P))) ...
  • 1 AES加密、解密算法原理和AVR实现AES是分组密钥,算法输入128位数据,密钥长度也是128位。用Nr表示对一个数据分组加密的轮数(加密轮数与密钥长度的关系如表1所列)。每一轮都需要一个与输入分组具有相同长度的扩展...
  • DES加密算法C语言实现

    2021-05-20 05:29:25
    DES加密算法C语言实现DES加密算法C语言实现更多更新更免费论文网,专业毕业设计论文,课程设计源代码,上机实习报告,实验报告请点击www.751com.cnDES加密算法C实现mac.c#include #include /** 程序描述: 加/解密公用...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,073
精华内容 1,229
关键字:

3des加密算法c语言实现