主頁 >  其他 > 【安全演算法之DES】DES演算法(支持ECB/CBC模式)的C語言原始碼實作

【安全演算法之DES】DES演算法(支持ECB/CBC模式)的C語言原始碼實作

2021-10-29 09:19:46 其他

【安全演算法之DES】DES演算法(支持ECB/CBC模式)的C語言原始碼實作

  • 概述
  • 頭檔案定義
  • C語言版本的實作原始碼
  • 資料分組模式:ECB模式和CBC模式
  • 測驗用例
  • github倉庫
  • 更多參考鏈接


概述

大家都知道在安全領域,演算法可謂是十分重要,而面對加密要求不算太高的場景,我們都可能會考慮使用對稱演算法,而不是采用非對稱演算法,主要是因為對稱演算法相對簡單,計算復雜度較低,而作為比較前出現的對稱演算法,DES演算法在對稱演算法領域一直占有很有重要的地位,
關于對稱演算法和非對稱演算法的區別,可以參考我之前的文章: 【安全演算法之概述】一文帶你簡要了解常見常用的安全演算法
今天給大家帶來DES演算法的C原始碼版本實作,本原始碼支持ECB模式和CBC模式,歡迎大家深入學習和討論,

頭檔案定義

頭檔案定義如下,主要定義了DES運算的資料塊長度大小,以及3種可能的密鑰長度,還有匯出的2個API,一個用于資料加密,一個用于資料解密:

#ifndef __DES_H__
#define __DES_H__

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stdint.h>

#define DES_BLOCK_LEN 		8       // 8bytes

#define DES_KEY_LEN_8		8		// key-length: 8bytes  -> single DES
#define DES_KEY_LEN_16		16      // key-length: 16bytes -> triple DES (key1=key[0], key2=key[8], key3=key[0])
#define DES_KEY_LEN_24		24		// key-length: 24bytes -> triple DES (key1=key[0], key2=key[8], key3=key[16])

typedef enum _des_mode_e {
	DES_MODE_ECB,
	DES_MODE_CBC
} des_mode_e;

int32_t crypto_des_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *out, const uint8_t *iv,
                           const uint8_t *key, uint32_t key_len, des_mode_e mode);
int32_t crypto_des_decrypt(const uint8_t *data, uint32_t data_len, uint8_t *out, const uint8_t *iv,
                            const uint8_t *key, uint32_t key_len, des_mode_e mode);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /*__DES_H__*/

C語言版本的實作原始碼

下面是DES的C語言版本實作,主要也是圍繞匯出的2個資料加解密API,其中資料的分組模式,支持ECB模式和CBC模式:


#include <stdio.h>
#include <string.h>

#include "des.h"

#define DES_TRUE                        0x01
#define DES_FALSE                       0x00
#define DES_DO_INITIAL_PERMUTATION      0x10
#define DES_ENCRYPTION_MODE             0x20
#define DES_DECRYPTION_MODE             0
#define DES_DO_FINAL_PERMUTATION        0x40
#define DES_ENCRYPT_BLOCK               (DES_DO_INITIAL_PERMUTATION|DES_ENCRYPTION_MODE|DES_DO_FINAL_PERMUTATION)
#define DES_DECRYPT_BLOCK               (DES_DO_INITIAL_PERMUTATION|DES_DECRYPTION_MODE|DES_DO_FINAL_PERMUTATION)

const uint8_t IPP[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
};

const uint8_t IPN[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
}; //Inverse permutation

const uint8_t Choose56[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
}; //Choosing the 56bit key from 64bit

const uint8_t key_round[32] = {
    1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
    0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
}; //shift key left //shift key right

const uint8_t Choose48[48] = {
    31, 0, 1, 2, 3, 4, 3, 4,   5, 6, 7, 8, 7, 8, 9, 10,
    11, 12, 11, 12, 13, 14, 15, 16,  15, 16, 17, 18, 19, 20, 19, 20,
    21, 22, 23, 24, 23, 24, 25, 26,  27, 28, 27, 28, 29, 30, 31, 0
}; //expands the right half text of 32 bits to 48 bits

const uint8_t E[48] = {
    13, 16, 10, 23, 0, 4, 2, 27,  14, 5, 20, 9, 22, 18, 11, 3,
    25, 7, 15, 6, 26, 19, 12, 1,  40, 51, 30, 36, 46, 54, 29, 39,
    50, 44, 32, 47, 43, 48, 38, 55,  33, 52, 45, 41, 49, 35, 28, 31
}; //Compression and permutation for key

const uint8_t PP[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
}; //P-box permutation

const uint8_t S[8][64] = {{
        14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
        0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
        4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
        15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
    },
    {
        15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
        3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
        0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
        13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
    },
    {
        10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
        13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
        13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
        1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
    },
    {
        7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
        13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
        10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
        3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
    },
    {
        2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
        14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
        4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
        11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
    },
    {
        12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
        10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
        9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
        4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
    },
    {
        4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
        13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
        1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
        6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
    },
    {
        13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
        1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
        7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
        2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
    }
};

const uint8_t _bitposition[8] = {
    0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};

static void check_table(uint8_t line, uint8_t *text, uint8_t *lasttext, const uint8_t *IDD)
{
    uint8_t i, j, k, temp, temp2;

    for (j = 0, k = 0; j < line; j++, k += 8) {
        lasttext[j] = 0;
        for (i = 0; i < 8; i++) {
            temp2 = IDD[k + i];
            temp = text[temp2 / 8]; // get the byte
            temp &= _bitposition[temp2 & 0x7]; // get the bit
            if (temp) {
                lasttext[j] |= _bitposition[i];
            }
        }
    }
}

static void single_des_operation(uint8_t *plaintext, uint8_t *key, uint8_t mode)
{
    static uint8_t prevtext[8];
    uint8_t prevkey[8], Ltext[4], Rtext[4];
    char temp, temp1;
    int32_t i = 0;
    int32_t Round = 0;
    uint8_t j = 0;
    
    if (mode & DES_DO_INITIAL_PERMUTATION) {
        check_table(8, plaintext, prevtext, IPP); //Initial permutation
    }

    for (i = 0; i < 4; i++) {
        Ltext[i] = prevtext[i];
        Rtext[i] = prevtext[i + 4];
    }

    check_table(7, key, prevkey, Choose56);

    for (Round = 0; Round < 16; Round++) {
        //rotate both 28bits block to left(encrypt) or right(decrypt)
        if (mode & DES_ENCRYPTION_MODE) { // encrypt
            for (j = 0; j < key_round[Round]; j++) {
                temp = prevkey[3] & 0x08;
                for (i = 7; i > 0; i--) {
                    temp1 = prevkey[i - 1] & 0x80;
                    prevkey[i - 1] <<= 1;
                    if (temp) {
                        prevkey[i - 1] |= 0x01;
                    }
                    temp = temp1;
                }

                if (temp) {
                    prevkey[3] |= 0x10;
                } else {
                    prevkey[3] &= 0xEF;
                }
            }
        } else { // decrypt
            for (j = 0; j < key_round[Round + 16]; j++) {
                temp = prevkey[3] & 0x10;
                for (i = 0; i < 7; i++) {
                    temp1 = prevkey[i] & 0x01;
                    prevkey[i] >>= 1;
                    if (temp) {
                        prevkey[i] |= 0x80;
                    }
                    temp = temp1;
                }

                if (temp) {
                    prevkey[3] |= 0x08;
                } else {
                    prevkey[3] &= 0xF7;
                }
            }
        }

        check_table(6, prevkey, plaintext, E); //Compression permutation
        check_table(6, Rtext, prevtext, Choose48); //Expansion permutation

        //Expanded right half xor_operation with the subkey
        prevtext[0] ^= plaintext[0];
        prevtext[1] ^= plaintext[1];
        prevtext[2] ^= plaintext[2];
        prevtext[3] ^= plaintext[3];
        prevtext[4] ^= plaintext[4];
        prevtext[5] ^= plaintext[5];

        for (j = 6, i = 8; j > 0; j -= 3, i -= 4) { //S-Box Substitution
            plaintext[i - 1] = prevtext[j - 1];
            plaintext[i - 2] = ((prevtext[j - 1] >> 6) & 0x03) | (prevtext[j - 2] << 2);
            plaintext[i - 3] = ((prevtext[j - 2] >> 4) & 0x0f) | (prevtext[j - 3] << 4);
            plaintext[i - 4] = prevtext[j - 3] >> 2;
        }

        for (i = 0; i < 8; i++) { //Get the S-Box location
            temp = plaintext[i] & 0x21;
            if (temp & 0x01) {
                temp = (temp & 0x20) | 0x10;
            }
            plaintext[i] = temp | ((plaintext[i] >> 1) & 0x0F);
        }

        //Get S-Box output
        plaintext[0] = S[0][plaintext[0]];
        plaintext[1] = S[1][plaintext[1]];
        plaintext[2] = S[2][plaintext[2]];
        plaintext[3] = S[3][plaintext[3]];
        plaintext[4] = S[4][plaintext[4]];
        plaintext[5] = S[5][plaintext[5]];
        plaintext[6] = S[6][plaintext[6]];
        plaintext[7] = S[7][plaintext[7]];

        //Combine 4-bit block to form 32-bit block
        plaintext[0] = (plaintext[0] << 4) | plaintext[1];
        plaintext[1] = (plaintext[2] << 4) | plaintext[3];
        plaintext[2] = (plaintext[4] << 4) | plaintext[5];
        plaintext[3] = (plaintext[6] << 4) | plaintext[7];

        check_table(4, plaintext, prevtext, PP);

        for (i = 0; i < 4; i++) {
            prevtext[i] ^= Ltext[i];
            Ltext[i] = Rtext[i];
            Rtext[i] = prevtext[i];
        }
    }

    for (i = 0; i < 4; i++) {
        prevtext[i] = Rtext[i];
        prevtext[i + 4] = Ltext[i];
    }

    if (mode & DES_DO_FINAL_PERMUTATION) {
        check_table(8, prevtext, plaintext, IPN); //Final permutation
    }
}

static int32_t xor_operation(uint8_t *out, const uint8_t *data1, const uint8_t *data2, uint32_t dwLen)
{
    int32_t ret = DES_TRUE;
    uint32_t i = 0;

    if ((dwLen != 0) && ((data1 == NULL) || (data2 == NULL) || (out == NULL))) {
        ret = DES_FALSE;
    } else {
        for (i = 0; i < dwLen; i++) {
            out[i] = data1[i] ^ data2[i];
        }
    }
    return ret;
}

int32_t crypto_des_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *out, const uint8_t *iv,
                           const uint8_t *key, uint32_t key_len, des_mode_e mode)
{
    uint32_t i = 0;

    if ((data_len % DES_BLOCK_LEN != 0) || \
            ((key_len != DES_KEY_LEN_8) && \
            (key_len != DES_KEY_LEN_16) && \
            (key_len != DES_KEY_LEN_24))) {
        return -1;
    }

    for (i = 0; i < data_len / DES_BLOCK_LEN; i++) {
        if (mode == DES_MODE_CBC) {
            if (i > 0) {
                xor_operation(&out[i * DES_BLOCK_LEN], &data[i * DES_BLOCK_LEN], \
                    &out[(i - 1) * DES_BLOCK_LEN], DES_BLOCK_LEN);
            } else {
                xor_operation(&out[i], &data[i], iv, DES_BLOCK_LEN);
            }
        } else {
            memcpy(&out[i * DES_BLOCK_LEN], &data[i * DES_BLOCK_LEN], DES_BLOCK_LEN);
        }

        if (key_len == DES_KEY_LEN_8) { //Single DES
            single_des_operation(&out[i * DES_BLOCK_LEN], (uint8_t *)&key[0], DES_ENCRYPT_BLOCK);
        } else { //Trip DES
            uint8_t *key1 = (uint8_t *)&key[0];
            uint8_t *key2 = (uint8_t *)&key[DES_KEY_LEN_8];
            uint8_t *key3 = (uint8_t *)&key[0];

            if (key_len == DES_KEY_LEN_24) {
                key3 = (uint8_t *)&key[DES_KEY_LEN_16];
            }
            single_des_operation(&out[i * DES_BLOCK_LEN], key1, DES_ENCRYPT_BLOCK);
            single_des_operation(&out[i * DES_BLOCK_LEN], key2, DES_DECRYPT_BLOCK);
            single_des_operation(&out[i * DES_BLOCK_LEN], key3, DES_ENCRYPT_BLOCK);
        }
    }

    return 0;
}

int32_t crypto_des_decrypt(const uint8_t *data, uint32_t data_len, uint8_t *out, const uint8_t *iv,
                            const uint8_t *key, uint32_t key_len, des_mode_e mode)
{
    int32_t i = 0;

    if ((data_len % DES_BLOCK_LEN != 0) || \
            ((key_len != DES_KEY_LEN_8) && \
            (key_len != DES_KEY_LEN_16) && \
            (key_len != DES_KEY_LEN_24))) {
        return -1;
    }

    for (i = data_len / DES_BLOCK_LEN - 1; i >= 0; i--) {
        memcpy(&out[i * DES_BLOCK_LEN], &data[i * DES_BLOCK_LEN], DES_BLOCK_LEN);
        if (key_len == DES_KEY_LEN_8) { //Single DES            
            single_des_operation(&out[i * DES_BLOCK_LEN], (uint8_t *)&key[0], DES_DECRYPT_BLOCK);
        } else { //Trip DES
            uint8_t *key1 = (uint8_t *)&key[0];
            uint8_t *key2 = (uint8_t *)&key[DES_KEY_LEN_8];
            uint8_t *key3 = (uint8_t *)&key[0];

            if (key_len == DES_KEY_LEN_24) {
                key3 = (uint8_t *)&key[DES_KEY_LEN_16];
            }
            single_des_operation(&out[i * DES_BLOCK_LEN], key3, DES_DECRYPT_BLOCK);
            single_des_operation(&out[i * DES_BLOCK_LEN], key2, DES_ENCRYPT_BLOCK);
            single_des_operation(&out[i * DES_BLOCK_LEN], key1, DES_DECRYPT_BLOCK);
        }

        if (mode == DES_MODE_CBC) {
            if (i > 0) {
                xor_operation(&out[i * DES_BLOCK_LEN], &out[i * DES_BLOCK_LEN], \
                    &data[(i - 1) * DES_BLOCK_LEN], DES_BLOCK_LEN);
            } else {
                xor_operation(&out[i], &out[i], iv, DES_BLOCK_LEN);
            }
        }
    }

    return 0;
}

資料分組模式:ECB模式和CBC模式

找了2張相關的圖,描述得比較清晰,有助于大家理解:
在這里插入圖片描述
簡單來說,ECB分組模式就把資料劃分為等長的資料塊,在DES運算里面,這個資料塊是8位元組,然后對每個資料塊進行加密解密操作,最后將所有的結果再次按序拼接在一起;每個資料塊之間互不影響,
在這里插入圖片描述
而CBC分組模式,它引入一個初始向量IV,它的運算流程如下:

加密流程

  • 將原文資料劃分成N組等長的資料塊,在DES運算里面,這個資料塊是8位元組;
  • 將IV向量與第一組資料塊明文進行異或運算,然后把異或的結果傳入DES加密流程,得到第一組密文;
  • 將第一組密文與第二組資料塊明文進行異或運算,然后把異或的結果傳入DES加密流程,得到第二組密文;
  • 以此類推,直到完成所有資料塊的加密操作;
  • 把所有的密文按照原來的順序拼接起來,得到輸出的密文,

解密流程

  • 將密文資料劃分成N組等長的資料塊,在DES運算里面,這個資料塊是8位元組;
  • 一般從尾部資料塊開始解密,把最后一組密文資料塊傳入DES解密流程,得到明文資料塊后與倒數第二組密文資料塊做異或運算,得到最后一組資料的明文;
  • 把倒數第二組密文資料塊傳入DES解密流程,得到明文資料塊后與倒數第三組密文資料塊做異或運算,得到倒數第二組資料的明文;
  • 以此類推,直到完成所有資料塊的解密操作;
  • 當解密完第一組資料密文后,要與初始向量IV做異或才得到第一組資料明文;
  • 把所有的明文按照原來的順序拼接起來,得到輸出的明文,

CBC分組模式的一個特點就是,有IV向量的參與,不同的輸入IV會有不同的輸出結果,并且之間直接的資料塊是會有關聯的,

測驗用例

針對DES匯出的兩個加解密介面,以及ECB和CBC陣列分組模式,我撰寫了以下測驗用例:


#include <stdio.h>
#include <string.h>

#include "des.h"
#include "convert.h"

int log_hexdump(const char *title, const unsigned char *data, int len)
{
    char str[160], octet[10];
    int ofs, i, k, d;
    const unsigned char *buf = (const unsigned char *)data;
    const char dimm[] = "+------------------------------------------------------------------------------+";

    printf("%s (%d bytes):\r\n", title, len);
    printf("%s\r\n", dimm);
    printf("| Offset  : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   0123456789ABCDEF |\r\n");
    printf("%s\r\n", dimm);

    for (ofs = 0; ofs < (int)len; ofs += 16) {
        d = snprintf( str, sizeof(str), "| %08X: ", ofs );

        for (i = 0; i < 16; i++) {
            if ((i + ofs) < (int)len) {
                snprintf( octet, sizeof(octet), "%02X ", buf[ofs + i] );
            } else {
                snprintf( octet, sizeof(octet), "   " );
            }

            d += snprintf( &str[d], sizeof(str) - d, "%s", octet );
        }
        d += snprintf( &str[d], sizeof(str) - d, "  " );
        k = d;

        for (i = 0; i < 16; i++) {
            if ((i + ofs) < (int)len) {
                str[k++] = (0x20 <= (buf[ofs + i]) &&  (buf[ofs + i]) <= 0x7E) ? buf[ofs + i] : '.';
            } else {
                str[k++] = ' ';
            }
        }

        str[k] = '\0';
        printf("%s |\r\n", str);
    }

    printf("%s\r\n", dimm);

    return 0;
}

typedef struct _des_test_data_t {
    const char *name;
    const char *data;
    const char *cipher_exp;
    const char *key;
    const char *iv;
    int         mode;
} des_test_data_t;

static des_test_data_t g_des_test_data[] = 
{
    {
        "DES_8-ECB-ENC-DEC",
        "486328584376857876876876DFED458693696994369982784325876837265867",
        "5D9D966FB7BD4C63C2452ADD63C75283D58253705EA8C9D2E2E69E4774C38DFF",
        "C1D0F8FB4958670D",
        "",
        DES_MODE_ECB,
    },
    {
        "DES_16-ECB-ENC-DEC",
        "486328584376857876876876DFED458693696994369982784325876837265867",
        "537B058DE0469097B657AB3639057FDE3A8AFF5D872BD02352C9785CCE9EDCA6",
        "C1D0F8FB4958670DBA40AB1F3752EF0D",
        "",
        DES_MODE_ECB,
    }, 
    {
        "DES_24-ECB-ENC-DEC",
        "486328584376857876876876DFED458693696994369982784325876837265867",
        "D7444666014085E167F20E9E6B592622CF74FB7C1F8AB3E05290C5AD81EA3140",
        "C1D0F8FB4958670DBA40AB1F3752EF0D453D784F3784E389",
        "",
        DES_MODE_ECB,
    }, 
    {
        "DES_8-CBC-ENC-DEC",
        "486328584376857876876876DFED458693696994369982784325876837265867",
        "799BFA00F4C3B2125DA1BA9185AB9B6C72940A92D7ABBDA1436F4C2C898B4D55",
        "C1D0F8FB4958670D",
        "876876DFED458693",
        DES_MODE_CBC,
    }, 
    {
        "DES_16-CBC-ENC-DEC",
        "486328584376857876876876DFED458693696994369982784325876837265867",
        "DE88B9AB8380320870E18130C306A2C953BAF27B2210EF0456B2CF90F6E2C1D3",
        "C1D0F8FB4958670DBA40AB1F3752EF0D",
        "876876DFED458693",
        DES_MODE_CBC,
    }, 
    {
        "DES_24-CBC-ENC-DEC",
        "486328584376857876876876DFED458693696994369982784325876837265867",
        "3788F49F44CFF611D1B638B0D0E71A2DB55304B3066EA2A5A864C7AC8FC111E5",
        "C1D0F8FB4958670DBA40AB1F3752EF0D453D784F3784E389",
        "876876DFED458693",
        DES_MODE_CBC,
    }, 
};

#define ARRAY_SIZE(n)   (sizeof(n)/sizeof(n[0]))

static int des_test(des_test_data_t *data)
{
    uint8_t data_hex[128];
    uint16_t data_len;
    uint8_t data_exp_hex[128];
    uint16_t data_exp_len;
    uint8_t key[24];
    uint16_t key_len;
    uint8_t iv[24];
    uint16_t iv_len;
    uint8_t data_calc[128];
    uint8_t data_calc2[128];
    int32_t ret;

    utils_hex_string_2_bytes(data->data, data_hex, &data_len);
    utils_hex_string_2_bytes(data->cipher_exp, data_exp_hex, &data_exp_len);
    utils_hex_string_2_bytes(data->key, key, &key_len);
    utils_hex_string_2_bytes(data->iv, iv, &iv_len);

    ret = crypto_des_encrypt(data_hex, data_len, data_calc, iv, key, key_len, data->mode);
    printf("DES encrypt ret: %d\n", ret);
    log_hexdump("data_hex", data_hex, data_len);
    log_hexdump("data_calc", data_calc, data_len);
    log_hexdump("iv", iv, 8);
    log_hexdump("key", key, key_len);

    if (!memcmp(data_calc, data_exp_hex, data_len)) {
        printf("DES encrypt [%s] OK !\n", data->name);
    } else {
        log_hexdump("data_exp_hex", data_exp_hex, data_len);
        log_hexdump("data_calc", data_calc, data_len);
        printf("DES encrypt [%s] FAIL !\n", data->name);
        ret = -1;
        goto exit_entry;
    }

    ret = crypto_des_decrypt(data_calc, data_len, data_calc2, iv, key, key_len, data->mode);
    printf("DES decrypt ret: %d\n", ret);
    log_hexdump("data_calc", data_calc, data_len);
    log_hexdump("data_calc2", data_calc2, data_len);
    log_hexdump("iv", iv, 8);
    log_hexdump("key", key, key_len);

    if (!memcmp(data_calc2, data_hex, data_len)) {
        printf("DES decrypt [%s] OK !\n", data->name);
    } else {
        log_hexdump("data_hex", data_hex, data_len);
        log_hexdump("data_calc2", data_calc2, data_len);
        printf("DES decrypt [%s] FAIL !\n", data->name);
        ret = -1;
        goto exit_entry;
    }

exit_entry:

    return ret;
}

int main(int argc, const char *argv[])
{
    int ret;
    int i;
    int cnt = ARRAY_SIZE(g_des_test_data);

    for (i = 0; i < cnt; i++) {        
        ret = des_test(&g_des_test_data[i]);
        if (ret < 0) {
            break;
        }
    }

	return 0;
}

測驗用例比較簡單,分為6組測驗場景,分別是:
"DES_8-ECB-ENC-DEC",
“DES_16-ECB-ENC-DEC”,
“DES_24-ECB-ENC-DEC”,
“DES_8-CBC-ENC-DEC”,
“DES_16-CBC-ENC-DEC”,
“DES_24-CBC-ENC-DEC”,

資料的原文都是486328584376857876876876DFED458693696994369982784325876837265867但是使用不同的密鑰長度和不同的分組模式,得出的密文是不一樣的,
測驗代碼中,提前預置好了期望的密文值,當計算結果與期望值一致時,表示計算成功,反之就計算失敗,

同時,也歡迎大家設計提供更多的測驗案例代碼,

github倉庫

以上代碼和測驗用例,及編譯運行等,可以參考我的github倉庫,有詳細的流程介紹,歡迎大家交流討論,如果有幫助到你的話,記得幫忙點亮一顆星哦,

更多參考鏈接

[1] 【安全演算法的github倉庫】
[2] 【安全演算法之概述】一文帶你簡要了解常見常用的安全演算法
[3] 【安全演算法之base64】base64加解密的C語言原始碼實作
[4] 【安全演算法之MD5】MD5摘要運算的C語言原始碼實作
[5] 【安全演算法之SHA1】SHA1摘要運算的C語言原始碼實作
[6] 【安全演算法之SHA224】SHA224摘要運算的C語言原始碼實作
[7] 【安全演算法之SHA256】SHA256摘要運算的C語言原始碼實作
[8] 【安全演算法之SHA384】SHA384摘要運算的C語言原始碼實作
[9] 【安全演算法之SHA512】SHA512摘要運算的C語言原始碼實作

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340660.html

標籤:其他

上一篇:BUUCTF Web [極客大挑戰 2019]Knife

下一篇:web安全101之如何理解XXE?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more