aes算法加密c语言完整程序,AES算法加密C语言完整程序.doc

AES算法加密C语言完整程序

AES算法加密C语言完整程序

#include

#include "aes.h"

#include "commonage.h"

#define byte unsigned char

#define BPOLY 0x1b //!< Lower 8 bits of (x^8+x^4+x^3+x+1), ie. (x^4+x^3+x+1).

#define BLOCKSIZE 16 //!< Block size in number of bytes.

#define KEYBITS 128 //!< Use AES128.

#define ROUNDS 10 //!< Number of rounds.

#define KEYLENGTH 16 //!< Key length in number of bytes.

byte xdata block1[ 256 ]; //!< Workspace 1.

byte xdata block2[ 256 ]; //!< Worksapce 2.

byte xdata * powTbl; //!< Final location of exponentiation lookup table.

byte xdata * logTbl; //!< Final location of logarithm lookup table.

byte xdata * sBox; //!< Final location of s-box.

byte xdata * sBoxInv; //!< Final location of inverse s-box.

byte xdata * expandedKey; //!< Final location of expanded key.

void CalcPowLog( byte * powTbl, byte * logTbl )

{

byte xdata i = 0;

byte xdata t = 1;

do {

// Use 0x03 as root for exponentiation and logarithms.

powTbl[i] = t;

logTbl[t] = i;

i++;

// Muliply t by 3 in GF(2^8).

t ^= (t << 1) ^ (t & 0x80 ? BPOLY : 0);

} while( t != 1 ); // Cyclic properties ensure that i < 255.

powTbl[255] = powTbl[0]; // 255 = '-0', 254 = -1, etc.

}

void CalcSBox( byte * sBox )

{

byte xdata i, rot;

byte xdata temp;

byte xdata result;

// Fill all entries of sBox[].

i = 0;

do {

// Inverse in GF(2^8).

if( i > 0 ) {

temp = powTbl[ 255 - logTbl[i] ];

} else {

temp = 0;

}

// Affine transformation in GF(2).

result = temp ^ 0x63; // Start with adding a vector in GF(2).

for( rot = 0; rot < 4; rot++ ) {

// Rotate left.

temp = (temp<<1) | (temp>>7);

// Add rotated byte in GF(2).

result ^= temp;

}

// Put result in table.

sBox[i] = result;

} while( ++i != 0 );

}

void Calc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值