AES 加密算法的 C 语言实现

      这个AES 的C 语言实现,是从linux 中port 而来,放在这里方便自己日后使用。 很容易把这个code 转为为C++ 的封装。

      aes.h
a#ifndef __AES_H__
#define  __AES_H__
#define  AES_MIN_KEY_SIZE    16
#define  AES_MAX_KEY_SIZE    32

#define  AES_BLOCK_SIZE        16

typedef unsigned 
char     u8;
typedef signed 
char     s8;
typedef signed 
short     s16;
typedef unsigned 
short     u16;
typedef    signed 
int     s32;
typedef unsigned 
int     u32;
typedef signed 
long   long     s64;
typedef unsigned 
long   long     u64;
typedef u16        __le16;
typedef u32        __le32;

#define  E_KEY    (&ctx->buf[0])
#define  D_KEY    (&ctx->buf[60])
#define  le32_to_cpu
#define  cpu_to_le32

struct  aes_ctx
{
    
int  key_length;
    u32 buf[
120 ];
};


void  gen_tabs ( void );
int  aes_set_key( struct  aes_ctx  *  ctx,  const  u8  * in_key, unsigned  int  key_len);
void  aes_encrypt( struct  aes_ctx  *  ctx, u8  * out const  u8  * in );
void  aes_decrypt( struct  aes_ctx  *  ctx, u8  * out const  u8  * in );
#endif


      aes.c    
#include  < stdio.h >
#include 
" aes.h "

static  u8 pow_tab[ 256 ];
static  u8 log_tab[ 256 ];
static  u8 sbx_tab[ 256 ];
static  u8 isb_tab[ 256 ];

static  u32 rco_tab[ 10 ];
static  u32 ft_tab[ 4 ][ 256 ];
static  u32 it_tab[ 4 ][ 256 ];
static  u32 fl_tab[ 4 ][ 256 ];
static  u32 il_tab[ 4 ][ 256 ];

static  inline u8  byte ( const  u32 x , const  unsigned n)
{
    
return  x  >>  (n  <<   3 );
}
static  inline u32 rol32(u32 word, unsigned  int  shift)
{
    
return  (word  <<  shift)  |  (word  >>  ( 32   -  shift));
}
static  inline u32 ror32(u32 word, unsigned  int  shift)
{
    
return  (word  >>  shift)  |  (word  <<  ( 32   -  shift));
}
static  inline u8 f_mult(u8 a , u8 b )
{
    u8 aa 
=  log_tab[a];
    u8 cc 
=  aa  +  log_tab[b];
    
return  pow_tab[cc  +  (cc  <  aa  ?   1  :  0  )];
}
#define  ff_mult(a,b) (a && b ? f_mult(a,b) : 0 )

#define  f_rn(bo, bi, n, k)                    
    bo[n] 
=   ft_tab[ 0 ][ byte (bi[n], 0 )]  ^                 
ft_tab[
1 ][ byte (bi[(n  +   1 &   3 ], 1 )]  ^         
ft_tab[
2 ][ byte (bi[(n  +   2 &   3 ], 2 )]  ^         
ft_tab[
3 ][ byte (bi[(n  +   3 &   3 ], 3 )]  ^   * (k  +  n)

#define  i_rn(bo, bi, n, k)                    
    bo[n] 
=   it_tab[ 0 ][ byte (bi[n], 0 )]  ^                 
it_tab[
1 ][ byte (bi[(n  +   3 &   3 ], 1 )]  ^         
it_tab[
2 ][ byte (bi[(n  +   2 &   3 ], 2 )]  ^         
it_tab[
3 ][ byte (bi[(n  +   1 &   3 ], 3 )]  ^   * (k  +  n)

#define  ls_box(x)                
    ( fl_tab[
0 ][ byte (x,  0 )]  ^             
      fl_tab[
1 ][ byte (x,  1 )]  ^             
      fl_tab[
2 ][ byte (x,  2 )]  ^             
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值