下载此文档

AES算法加密C语言程序.doc


文档分类:IT计算机 | 页数:约14页 举报非法文档有奖
1/14
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/14 下载此文档
文档列表 文档介绍
#include <>
#include ""
#include ""
#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 CalcSBoxInv( byte * sBox, byte * sBoxInv )
{
byte xdata i = 0;
byte xdata j = 0;
// Iterate through all elements in sBoxInv using i.
do {
// Search through sBox using j.
cleardog();
do {
// Check if current j is the inverse of current i.
if(

AES算法加密C语言程序 来自淘豆网www.taodocs.com转载请标明出处.

非法内容举报中心
文档信息
  • 页数14
  • 收藏数0 收藏
  • 顶次数0
  • 上传人zxwziyou8
  • 文件大小95 KB
  • 时间2018-06-25