搜索
bottom↓
回复: 6

AES算法的参数怎么看出来,懂加密的帮忙看看

[复制链接]

出0入4汤圆

发表于 2018-8-17 14:40:05 | 显示全部楼层 |阅读模式
在本坛下载的AES算法源码,这个算法的加密模式 填充模式 偏移量等参数 和代码中如何具体对应。上位机不是用C开发 现在两头加密解密怎么也对应不起来 ,懂加密算法的帮忙看看 指点一二  非常感谢!

#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 KEY_COUNT 3

#if KEY_COUNT == 1
  #define KEYBITS 128 //!< Use AES128.
#elif KEY_COUNT == 2
  #define KEYBITS 192 //!< Use AES196.
#elif KEY_COUNT == 3
  #define KEYBITS 256 //!< Use AES256.
#else
  #error Use 1, 2 or 3 keys!
#endif

#if KEYBITS == 128
  #define ROUNDS 10 //!< Number of rounds.
  #define KEYLENGTH 16 //!< Key length in number of bytes.
#elif KEYBITS == 192
  #define ROUNDS 12 //!< Number of rounds.
  #define KEYLENGTH 24 //!< // Key length in number of bytes.
#elif KEYBITS == 256
  #define ROUNDS 14 //!< Number of rounds.
  #define KEYLENGTH 32 //!< Key length in number of bytes.
#else
  #error Key must be 128, 192 or 256 bits!
#endif


#define EXPANDED_KEY_SIZE (BLOCKSIZE * (ROUNDS+1)) //!< 176, 208 or 240 bytes.

unsigned char AES_Key_Table[32] =
{
  0xd0, 0x94, 0x3f, 0x8c, 0x29, 0x76, 0x15, 0xd8,
  0x20, 0x40, 0xe3, 0x27, 0x45, 0xd8, 0x48, 0xad,
  0xea, 0x8b, 0x2a, 0x73, 0x16, 0xe9, 0xb0, 0x49,
  0x45, 0xb3, 0x39, 0x28, 0x0a, 0xc3, 0x28, 0x3c,
};

unsigned char block1[256]; //!< Workspace 1.
unsigned char block2[256]; //!< Worksapce 2.
unsigned char tempbuf[256];

unsigned char *powTbl; //!< Final location of exponentiation lookup table.
unsigned char *logTbl; //!< Final location of logarithm lookup table.
unsigned char *sBox; //!< Final location of s-box.
unsigned char *sBoxInv; //!< Final location of inverse s-box.
unsigned char *expandedKey; //!< Final location of expanded key.

void CalcPowLog(unsigned char *powTbl, unsigned char *logTbl)
{
        unsigned char i = 0;
        unsigned char 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( unsigned char * sBox )
{
        unsigned char i, rot;
        unsigned char temp;
        unsigned char 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( unsigned char * sBox, unsigned char * sBoxInv )
{
        unsigned char i = 0;
        unsigned char j = 0;
       
        // Iterate through all elements in sBoxInv using  i.
        do {
        // Search through sBox using j.
                do {
                        // Check if current j is the inverse of current i.
                        if( sBox[ j ] == i )
                        {
                                // If so, set sBoxInc and indicate search finished.
                                sBoxInv[ i ] = j;
                                j = 255;
                        }
                } while( ++j != 0 );
        } while( ++i != 0 );
}

void CycleLeft( unsigned char * row )
{
        // Cycle 4 bytes in an array left once.
        unsigned char temp = row[0];
       
        row[0] = row[1];
        row[1] = row[2];
        row[2] = row[3];
        row[3] = temp;
}

void InvMixColumn( unsigned char * column )
{
        unsigned char r0, r1, r2, r3;
       
        r0 = column[1] ^ column[2] ^ column[3];
        r1 = column[0] ^ column[2] ^ column[3];
        r2 = column[0] ^ column[1] ^ column[3];
        r3 = column[0] ^ column[1] ^ column[2];
       
        column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
        column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
        column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
        column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);
       
        r0 ^= column[0] ^ column[1];
        r1 ^= column[1] ^ column[2];
        r2 ^= column[2] ^ column[3];
        r3 ^= column[0] ^ column[3];
       
        column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
        column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
        column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
        column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);
       
        r0 ^= column[0] ^ column[2];
        r1 ^= column[1] ^ column[3];
        r2 ^= column[0] ^ column[2];
        r3 ^= column[1] ^ column[3];
       
        column[0] = (column[0] << 1) ^ (column[0] & 0x80 ? BPOLY : 0);
        column[1] = (column[1] << 1) ^ (column[1] & 0x80 ? BPOLY : 0);
        column[2] = (column[2] << 1) ^ (column[2] & 0x80 ? BPOLY : 0);
        column[3] = (column[3] << 1) ^ (column[3] & 0x80 ? BPOLY : 0);
       
        column[0] ^= column[1] ^ column[2] ^ column[3];
        r0 ^= column[0];
        r1 ^= column[0];
        r2 ^= column[0];
        r3 ^= column[0];
       
        column[0] = r0;
        column[1] = r1;
        column[2] = r2;
        column[3] = r3;
}

void SubBytes( unsigned char * bytes, unsigned char count )
{
        do {
                *bytes = sBox[ *bytes ]; // Substitute every byte in state.
                bytes++;
        } while( --count );
}

void InvSubBytesAndXOR( unsigned char * bytes, unsigned char * key, unsigned char count )
{
        do {
                // *bytes = sBoxInv[ *bytes ] ^ *key; // Inverse substitute every byte in state and add key.
                *bytes = block2[ *bytes ] ^ *key; // Use block2 directly. Increases speed.
                bytes++;
                key++;
        } while( --count );
}

void InvShiftRows( unsigned char * state )
{
        unsigned char temp;
       
        // Note: State is arranged column by column.
       
        // Cycle second row right one time.
        temp = state[ 1 + 3*4 ];
        state[ 1 + 3*4 ] = state[ 1 + 2*4 ];
        state[ 1 + 2*4 ] = state[ 1 + 1*4 ];
        state[ 1 + 1*4 ] = state[ 1 + 0*4 ];
        state[ 1 + 0*4 ] = temp;
       
        // Cycle third row right two times.
        temp = state[ 2 + 0*4 ];
        state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
        state[ 2 + 2*4 ] = temp;
        temp = state[ 2 + 1*4 ];
        state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
        state[ 2 + 3*4 ] = temp;
       
        // Cycle fourth row right three times, ie. left once.
        temp = state[ 3 + 0*4 ];
        state[ 3 + 0*4 ] = state[ 3 + 1*4 ];
        state[ 3 + 1*4 ] = state[ 3 + 2*4 ];
        state[ 3 + 2*4 ] = state[ 3 + 3*4 ];
        state[ 3 + 3*4 ] = temp;
}

void InvMixColumns( unsigned char * state )
{
        InvMixColumn( state + 0*4 );
        InvMixColumn( state + 1*4 );
        InvMixColumn( state + 2*4 );
        InvMixColumn( state + 3*4 );
}

void XORBytes( unsigned char * bytes1, unsigned char * bytes2, unsigned char count )
{
        do {
                *bytes1 ^= *bytes2; // Add in GF(2), ie. XOR.
                bytes1++;
                bytes2++;
        } while( --count );
}

void CopyBytes( unsigned char * to, unsigned char * from, unsigned char count )
{
        do {
                *to = *from;
                to++;
                from++;
        } while( --count );
}

void KeyExpansion( unsigned char * expandedKey )
{
        unsigned char temp[4];
        unsigned char i;
        unsigned char Rcon[4] = { 0x01, 0x00, 0x00, 0x00 }; // Round constant.
       
        unsigned char * key = AES_Key_Table;
       
        // Copy key to start of expanded key.
        i = KEYLENGTH;
        do {
                *expandedKey = *key;
                expandedKey++;
                key++;
        } while( --i );
       
        // Prepare last 4 bytes of key in temp.
        expandedKey -= 4;
        temp[0] = *(expandedKey++);
        temp[1] = *(expandedKey++);
        temp[2] = *(expandedKey++);
        temp[3] = *(expandedKey++);
       
        // Expand key.
        i = KEYLENGTH;
        while( i < BLOCKSIZE*(ROUNDS+1) )
        {
                // Are we at the start of a multiple of the key size?
                if( (i % KEYLENGTH) == 0 )
                {
                        CycleLeft( temp ); // Cycle left once.
                        SubBytes( temp, 4 ); // Substitute each byte.
                        XORBytes( temp, Rcon, 4 ); // Add constant in GF(2).
                        *Rcon = (*Rcon << 1) ^ (*Rcon & 0x80 ? BPOLY : 0);
                }
               
                // Keysize larger than 24 bytes, ie. larger that 192 bits?
                #if KEYLENGTH > 24
                // Are we right past a block size?
                else if( (i % KEYLENGTH) == BLOCKSIZE ) {
                SubBytes( temp, 4 ); // Substitute each byte.
                }
                #endif
               
                // Add bytes in GF(2) one KEYLENGTH away.
                XORBytes( temp, expandedKey - KEYLENGTH, 4 );
               
                // Copy result to current 4 bytes.
                *(expandedKey++) = temp[ 0 ];
                *(expandedKey++) = temp[ 1 ];
                *(expandedKey++) = temp[ 2 ];
                *(expandedKey++) = temp[ 3 ];
               
                i += 4; // Next 4 bytes.
        }
}

void InvCipher( unsigned char * block, unsigned char * expandedKey )
{
        unsigned char round = ROUNDS-1;
        expandedKey += BLOCKSIZE * ROUNDS;
       
        XORBytes( block, expandedKey, 16 );
        expandedKey -= BLOCKSIZE;
       
        do {
                InvShiftRows( block );
                InvSubBytesAndXOR( block, expandedKey, 16 );
                expandedKey -= BLOCKSIZE;
                InvMixColumns( block );
        } while( --round );
       
        InvShiftRows( block );
        InvSubBytesAndXOR( block, expandedKey, 16 );
}

void aesDecInit(void)
{
        powTbl = block1;
        logTbl = block2;
        CalcPowLog( powTbl, logTbl );
       
        sBox = tempbuf;
        CalcSBox( sBox );
       
        expandedKey = block1;
        KeyExpansion( expandedKey );
       
        sBoxInv = block2; // Must be block2.
        CalcSBoxInv( sBox, sBoxInv );
}

void aesDecrypt( unsigned char * buffer, unsigned char * chainBlock )
{
        unsigned char temp[ BLOCKSIZE ];
       
        CopyBytes( temp, buffer, BLOCKSIZE );
        InvCipher( buffer, expandedKey );
        XORBytes( buffer, chainBlock, BLOCKSIZE );
        CopyBytes( chainBlock, temp, BLOCKSIZE );
}

unsigned char Multiply( unsigned char num, unsigned char factor )
{
        unsigned char mask = 1;
        unsigned char result = 0;
       
        while( mask != 0 )
        {
        // Check bit of factor given by mask.
                if( mask & factor )
                {
                  // Add current multiple of num in GF(2).
                  result ^= num;
                }
       
                // Shift mask to indicate next bit.
                mask <<= 1;
               
                // Double num.
                num = (num << 1) ^ (num & 0x80 ? BPOLY : 0);
        }
       
        return result;
}

unsigned char DotProduct( unsigned char * vector1, unsigned char * vector2 )
{
        unsigned char result = 0;
       
        result ^= Multiply( *vector1++, *vector2++ );
        result ^= Multiply( *vector1++, *vector2++ );
        result ^= Multiply( *vector1++, *vector2++ );
        result ^= Multiply( *vector1  , *vector2   );
       
        return result;
}

void MixColumn( unsigned char * column )
{
        unsigned char row[8] = {0x02, 0x03, 0x01, 0x01, 0x02, 0x03, 0x01, 0x01};
        // Prepare first row of matrix twice, to eliminate need for cycling.
       
        unsigned char result[4];
       
        // Take dot products of each matrix row and the column vector.
        result[0] = DotProduct( row+0, column );
        result[1] = DotProduct( row+3, column );
        result[2] = DotProduct( row+2, column );
        result[3] = DotProduct( row+1, column );
       
        // Copy temporary result to original column.
        column[0] = result[0];
        column[1] = result[1];
        column[2] = result[2];
        column[3] = result[3];
}

void MixColumns( unsigned char * state )
{
        MixColumn( state + 0*4 );
        MixColumn( state + 1*4 );
        MixColumn( state + 2*4 );
        MixColumn( state + 3*4 );
}

void ShiftRows( unsigned char * state )
{
        unsigned char temp;
       
        // Note: State is arranged column by column.
       
        // Cycle second row left one time.
        temp = state[ 1 + 0*4 ];
        state[ 1 + 0*4 ] = state[ 1 + 1*4 ];
        state[ 1 + 1*4 ] = state[ 1 + 2*4 ];
        state[ 1 + 2*4 ] = state[ 1 + 3*4 ];
        state[ 1 + 3*4 ] = temp;
       
        // Cycle third row left two times.
        temp = state[ 2 + 0*4 ];
        state[ 2 + 0*4 ] = state[ 2 + 2*4 ];
        state[ 2 + 2*4 ] = temp;
        temp = state[ 2 + 1*4 ];
        state[ 2 + 1*4 ] = state[ 2 + 3*4 ];
        state[ 2 + 3*4 ] = temp;
       
        // Cycle fourth row left three times, ie. right once.
        temp = state[ 3 + 3*4 ];
        state[ 3 + 3*4 ] = state[ 3 + 2*4 ];
        state[ 3 + 2*4 ] = state[ 3 + 1*4 ];
        state[ 3 + 1*4 ] = state[ 3 + 0*4 ];
        state[ 3 + 0*4 ] = temp;
}

void Cipher( unsigned char * block, unsigned char * expandedKey )
{
        unsigned char round = ROUNDS-1;
       
        XORBytes( block, expandedKey, 16 );
        expandedKey += BLOCKSIZE;
       
        do {
                SubBytes( block, 16 );
                ShiftRows( block );
                MixColumns( block );
                XORBytes( block, expandedKey, 16 );
                expandedKey += BLOCKSIZE;
        } while( --round );
       
        SubBytes( block, 16 );
        ShiftRows( block );
        XORBytes( block, expandedKey, 16 );
}

void aesEncInit(void)
{
        powTbl = block1;
        logTbl = tempbuf;
        CalcPowLog( powTbl, logTbl );
       
        sBox = block2;
        CalcSBox( sBox );
       
        expandedKey = block1;
        KeyExpansion( expandedKey );
}

void aesEncrypt( unsigned char * buffer, unsigned char * chainBlock )
{
        XORBytes( buffer, chainBlock, BLOCKSIZE );
        Cipher( buffer, expandedKey );
        CopyBytes( chainBlock, buffer, BLOCKSIZE );
}

#include <string.h>
void AES_Test(void)
{
        unsigned char dat[16]="0123456789ABCDEF";
        unsigned char chainCipherBlock[16];
    unsigned char i;
        for(i=0;i<32;i++) AES_Key_Table[i]=i;//做运算之前先要设置好密钥,这里只是设置密钥的DEMO。


        memset(chainCipherBlock,0x00,sizeof(chainCipherBlock));

        aesEncInit();//在执行加密初始化之前可以为AES_Key_Table赋值有效的密码数据

        aesEncrypt(dat, chainCipherBlock);//AES加密,数组dat里面的新内容就是加密后的数据。
        //aesEncrypt(dat+16, chainCipherBlock);//AES源数据大于16字节时,把源数据的指针+16就好了


       
        memset(chainCipherBlock,0x00,sizeof(chainCipherBlock));//这里要重新初始化清空

        aesDecInit();//在执行解密初始化之前可以为AES_Key_Table赋值有效的密码数据

        aesDecrypt(dat, chainCipherBlock);//AES解密,密文数据存放在dat里面,经解密就能得到之前的明文。
        //aesDecrypt(dat+16, chainCipherBlock);//AES源数据大于16字节时,把源数据的指针+16就好了
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

发表于 2018-8-17 15:41:47 | 显示全部楼层
用其他的软件生成的加密数据与这个算法生成的不一样?

出0入8汤圆

发表于 2018-8-17 16:42:26 | 显示全部楼层
你这个代码,并没有实现加密模式、填充之类的。

去这边看看这个项目,用这个吧:『tiny-AES-c』

出0入0汤圆

发表于 2018-8-17 23:23:52 | 显示全部楼层
ls的项目里面的make   是avr

出0入442汤圆

发表于 2018-8-18 10:43:36 来自手机 | 显示全部楼层
3DA502 发表于 2018-8-17 23:23
ls的项目里面的make   是avr

avr是gcc实现,数据类型基本兼容32位机,除了int是16位的。avr实现简单,我就在fpga里面用一个自己做的小型的。

出0入0汤圆

发表于 2018-8-18 15:15:45 | 显示全部楼层
在Linux 下直接make   也能用

z@ubuntu:~/Desktop/tinyAES/tiny-AES-c$ ./test.elf

Testing AES128

CBC encrypt: SUCCESS!
CBC decrypt: SUCCESS!
CTR encrypt: SUCCESS!
CTR decrypt: SUCCESS!
ECB decrypt: SUCCESS!
ECB encrypt: SUCCESS!
ECB encrypt verbose:

plain text:
6bc1bee22e409f96e93d7e117393172a
ae2d8a571e03ac9c9eb76fac45af8e51
30c81c46a35ce411e5fbc1191a0a52ef
f69f2445df4f9b17ad2b417be66c3710

key:
2b7e151628aed2a6abf7158809cf4f3c

ciphertext:
3ad77bb40d7a3660a89ecaf32466ef97
f5d3d58503b9699de785895a96fdbaaf
43b1cd7f598ece23881b00e3ed030688
7b0c785e27e8ad3f8223207104725dd4

出0入4汤圆

发表于 2018-9-18 21:26:15 | 显示全部楼层
security 发表于 2018-8-17 16:42
你这个代码,并没有实现加密模式、填充之类的。

去这边看看这个项目,用这个吧:『tiny-AES-c』 ...

最近也在研究AES,感谢分享!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-27 08:11

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表