crypto aes ecb 加密解密算法c语言实现

crypto_aes.c

//******************************************************************************
#include "crypto_aes.h"
#include "crypto_common.h"

#define AES_ENCRYPT     0
#define AES_DECRYPT     1

static MS_U8 Key[0x10]={ add key yourself}

/**
 * \brief          AES context structure
 */
typedef struct
{
    int nr;                     /*!<  number of rounds  */
    unsigned long *rk;          /*!<  AES round keys    */
    unsigned long buf[68];      /*!<  unaligned data    */
}
aes_context;

/*
 * 32-bit integer manipulation macros (little endian)
 */
#ifndef GET_ULONG_LE
#define GET_ULONG_LE(n,b,i)                             \
{                                                       \
    (n) = ( (unsigned long) (b)[(i)    ]       )        \
        | ( (unsigned long) (b)[(i) + 1] <<  8 )        \
        | ( (unsigned long) (b)[(i) + 2] << 16 )        \
        | ( (unsigned long) (b)[(i) + 3] << 24 );       \
}
#endif

#ifndef PUT_ULONG_LE
#define PUT_ULONG_LE(n,b,i)                             \
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n)       );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 3] = (unsigned char) ( (n) >> 24 );       \
}
#endif

/*
 * Forward S-box & tables
 */
static unsigned char FSb[256];
static unsigned long FT0[256];
static unsigned long FT1[256];
static unsigned long FT2[256];
static unsigned long FT3[256];

/*
 * Reverse S-box & tables
 */
static unsigned char RSb[256];
static unsigned long RT0[256];
static unsigned long RT1[256];
static unsigned long RT2[256];
static unsigned long RT3[256];

/*
 * Round constants
 */
static unsigned long RCON[10];

/*
 * Tables generation code
 */
#define ROTL8(x) ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
#define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
#define MUL(x,y) ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )

static int aes_init_done = 0;

static void aes_gen_tables( void )
{
    int i, x, y, z;
    int pow[256];
    int log[256];

    /*
     * compute pow and log tables over GF(2^8)
     */
    for ( i = 0, x = 1; i < 256; i++ ) {
        pow[i] = x;
        log[x] = i;
        x = ( x ^ XTIME( x ) ) & 0xFF;
    }

    /*
     * calculate the round constants
     */
    for ( i = 0, x = 1; i < 10; i++ ) {
        RCON[i] = (unsigned long) x;
        x = XTIME( x ) & 0xFF;
    }

    /*
     * generate the forward and reverse S-boxes
     */

    FSb[0x00] = 0x63;
    RSb[0x63] = 0x00;

    for ( i = 1; i < 256; i++ ) {
        x = pow[255 - log[i]];

        y  = x; y = ( (y << 1) | (y >> 7) ) & 0xFF;
        x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
        x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
        x ^=

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 AES 加密解密算法 ECB 模式的 C 语言实现。这个实现假设输入数据已经被填充到 AES 块大小的倍数(例如,128 位块大小的 AES 需要填充为 16 字节的倍数)。 ```c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include "aes.h" void aes_encrypt_ecb(const uint8_t *key, const uint8_t *input, uint8_t *output, size_t length) { aes_context ctx; aes_setkey_enc(&ctx, key, 128); for (size_t i = 0; i < length; i += 16) { aes_encrypt(&ctx, input + i, output + i); } } void aes_decrypt_ecb(const uint8_t *key, const uint8_t *input, uint8_t *output, size_t length) { aes_context ctx; aes_setkey_dec(&ctx, key, 128); for (size_t i = 0; i < length; i += 16) { aes_decrypt(&ctx, input + i, output + i); } } int main() { // Example usage uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; uint8_t input[32] = {0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 0x31, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34}; uint8_t output[32]; aes_encrypt_ecb(key, input, output, 32); aes_decrypt_ecb(key, output, input, 32); printf("Input: "); for (int i = 0; i < 32; ++i) { printf("%02x ", input[i]); } printf("\n"); printf("Output: "); for (int i = 0; i < 32; ++i) { printf("%02x ", output[i]); } printf("\n"); return 0; } ``` 这个实现使用了 Crypto++ 中的 AES 实现。在 `aes.h` 头文件中定义了 `aes_context` 结构体,用于保存 AES 加密算法的上下文信息。`aes_setkey_enc()` 和 `aes_setkey_dec()` 函数分别用于设置加密和解密密钥。`aes_encrypt()` 和 `aes_decrypt()` 函数分别用于加密和解密数据块。在 `aes_encrypt_ecb()` 和 `aes_decrypt_ecb()` 函数中,使用了 `for` 循环来对输入数据进行分块并进行加密或解密操作。最后,`main()` 函数演示了如何使用这些函数来加密和解密数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值