OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4331|回复: 4

自己做的AES-ECB加密解密,有些问题想请教一下

[复制链接]

3

主题

13

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2016-9-19
在线时间
12 小时
发表于 2016-9-21 16:47:32 | 显示全部楼层 |阅读模式
10金钱
手头的事情要给数据进行加密,我用的是AES-ECB加密方式。
现在程序中加密已经调试好了,能用串口显示出来。
但是解密上遇到以下几个问题:
一、串口调试助手能显示出原来的明文,但是明文之后还有一长串不知名的数据不知道从哪来的
二、烧好程序后需要等待一段时间(30S左右)调试程序才能显示出数据
希望各位大大们帮我看看啊,小弟不胜感激。
程序在下面。

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

3

主题

13

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2016-9-19
在线时间
12 小时
 楼主| 发表于 2016-9-21 16:48:26 | 显示全部楼层
这是算法
#include "aes.h"

#define F(x)   (((x)<<1) ^ ((((x)>>7) & 1) * 0x1b))
#define FD(x)  (((x) >> 1) ^ (((x) & 1) ? 0x8d : 0))

/* -------------------------------------------------------------------------- */
uchar8_t gf_alog(uchar8_t x) // calculate anti-logarithm gen 3
{
    uchar8_t atb = 1, z;

    while (x--) {z = atb; atb <<= 1; if (z & 0x80) atb^= 0x1b; atb ^= z;}

    return atb;
} /* gf_alog */

/* -------------------------------------------------------------------------- */
uchar8_t gf_log(uchar8_t x) // calculate logarithm gen 3
{
    uchar8_t atb = 1, i = 0, z;

    do {
        if (atb == x) break;
        z = atb; atb <<= 1; if (z & 0x80) atb^= 0x1b; atb ^= z;
    } while (++i > 0);

    return i;
} /* gf_log */


/* -------------------------------------------------------------------------- */
uchar8_t gf_mulinv(uchar8_t x) // calculate multiplicative inverse
{
    return (x) ? gf_alog(255 - gf_log(x)) : 0;
} /* gf_mulinv */

/* -------------------------------------------------------------------------- */
uchar8_t rj_sbox(uchar8_t x)
{
    uchar8_t y, sb;

    sb = y = gf_mulinv(x);
    y = (y<<1)|(y>>7); sb ^= y;  y = (y<<1)|(y>>7); sb ^= y;
    y = (y<<1)|(y>>7); sb ^= y;  y = (y<<1)|(y>>7); sb ^= y;

    return (sb ^ 0x63);
} /* rj_sbox */

/* -------------------------------------------------------------------------- */
uchar8_t rj_xtime(uchar8_t x)
{
    return (x & 0x80) ? ((x << 1) ^ 0x1b) : (x << 1);
} /* rj_xtime */


/* -------------------------------------------------------------------------- */
void aes_subBytes(uchar8_t *buf)
{
    register uchar8_t i = 16;
    while (i--) buf[i] = rj_sbox(buf[i]);
} /* 字节替代变换 */

/* -------------------------------------------------------------------------- */
void aes_subBytes_inv(uchar8_t *buf)
{
    register uchar8_t i = 16, j;
    while (i--)
    {
        j = 0; do { if (rj_sbox(j) == buf[i]) {buf[i] = j; break;} } while (++j);
    }
} /* 字节替代逆变换 */

/* -------------------------------------------------------------------------- */
void aes_addRoundKey(uchar8_t *buf, uchar8_t *key)
{
    register uchar8_t i = 16;
    while (i--) buf[i] ^= key[i];
} /* 子密钥加 */

/* -------------------------------------------------------------------------- */
void aes_addRoundKey_cpy(uchar8_t *buf, uchar8_t *key, uchar8_t *cpk)
{
    register uchar8_t i = 16;
    while (i--)  buf[i] ^= (cpk[i] = key[i]), cpk[16+i] = key[16 + i];
} /* 逆子密钥加 */


/* -------------------------------------------------------------------------- */
void aes_shiftRows(uchar8_t *buf)
{
    register uchar8_t i, j; /* to make it potentially parallelable */

    i = buf[1]; buf[1] = buf[5]; buf[5] = buf[9]; buf[9] = buf[13]; buf[13] = i;
    i = buf[10]; buf[10] = buf[2]; buf[2] = i;
    j = buf[3]; buf[3] = buf[15]; buf[15] = buf[11]; buf[11] = buf[7]; buf[7] = j;
    j = buf[14]; buf[14] = buf[6]; buf[6]  = j;

} /* 移位变换 */

/* -------------------------------------------------------------------------- */
void aes_shiftRows_inv(uchar8_t *buf)
{
    register uchar8_t i, j; /* same as above */

    i = buf[1]; buf[1] = buf[13]; buf[13] = buf[9]; buf[9] = buf[5]; buf[5] = i;
    i = buf[2]; buf[2] = buf[10]; buf[10] = i;
    j = buf[3]; buf[3] = buf[7]; buf[7] = buf[11]; buf[11] = buf[15]; buf[15] = j;
    j = buf[6]; buf[6] = buf[14]; buf[14] = j;

} /* 逆移位变换 */

/* -------------------------------------------------------------------------- */
void aes_mixColumns(uchar8_t *buf)
{
    register uchar8_t i, a, b, c, d, e;

    for (i = 0; i < 16; i += 4)
    {
        a = buf[i]; b = buf[i + 1]; c = buf[i + 2]; d = buf[i + 3];
        e = a ^ b ^ c ^ d;
        buf[i] ^= e ^ rj_xtime(a^b);   buf[i+1] ^= e ^ rj_xtime(b^c);
        buf[i+2] ^= e ^ rj_xtime(c^d); buf[i+3] ^= e ^ rj_xtime(d^a);
    }
} /* 列混合变换 */

/* -------------------------------------------------------------------------- */
void aes_mixColumns_inv(uchar8_t *buf)
{
    register uchar8_t i, a, b, c, d, e, x, y, z;

    for (i = 0; i < 16; i += 4)
    {
        a = buf[i]; b = buf[i + 1]; c = buf[i + 2]; d = buf[i + 3];
        e = a ^ b ^ c ^ d;
        z = rj_xtime(e);
        x = e ^ rj_xtime(rj_xtime(z^a^c));  y = e ^ rj_xtime(rj_xtime(z^b^d));
        buf[i] ^= x ^ rj_xtime(a^b);   buf[i+1] ^= y ^ rj_xtime(b^c);
        buf[i+2] ^= x ^ rj_xtime(c^d); buf[i+3] ^= y ^ rj_xtime(d^a);
    }
} /* 逆列混合变换 */

/* -------------------------------------------------------------------------- */
void aes_expandEncKey(uchar8_t *k, uchar8_t *rc)
{
    register uchar8_t i;

    k[0] ^= rj_sbox(k[29]) ^ (*rc);
    k[1] ^= rj_sbox(k[30]);
    k[2] ^= rj_sbox(k[31]);
    k[3] ^= rj_sbox(k[28]);
    *rc = F( *rc);

    for(i = 4; i < 16; i += 4)  k[i] ^= k[i-4],   k[i+1] ^= k[i-3],
                                k[i+2] ^= k[i-2], k[i+3] ^= k[i-1];
    k[16] ^= rj_sbox(k[12]);
    k[17] ^= rj_sbox(k[13]);
    k[18] ^= rj_sbox(k[14]);
    k[19] ^= rj_sbox(k[15]);

    for(i = 20; i < 32; i += 4) k[i] ^= k[i-4],   k[i+1] ^= k[i-3],
                                k[i+2] ^= k[i-2], k[i+3] ^= k[i-1];

} /* 扩展子密钥 */

/* -------------------------------------------------------------------------- */
void aes_expandDecKey(uchar8_t *k, uchar8_t *rc)
{
    uchar8_t i;

    for(i = 28; i > 16; i -= 4) k[i+0] ^= k[i-4], k[i+1] ^= k[i-3],
                                k[i+2] ^= k[i-2], k[i+3] ^= k[i-1];

    k[16] ^= rj_sbox(k[12]);
    k[17] ^= rj_sbox(k[13]);
    k[18] ^= rj_sbox(k[14]);
    k[19] ^= rj_sbox(k[15]);

    for(i = 12; i > 0; i -= 4)  k[i+0] ^= k[i-4], k[i+1] ^= k[i-3],
                                k[i+2] ^= k[i-2], k[i+3] ^= k[i-1];

    *rc = FD(*rc);
    k[0] ^= rj_sbox(k[29]) ^ (*rc);
    k[1] ^= rj_sbox(k[30]);
    k[2] ^= rj_sbox(k[31]);
    k[3] ^= rj_sbox(k[28]);
} /* 扩展逆子密钥 */


/* -------------------------------------------------------------------------- */
void aes_init(aes_context *ctx, uchar8_t *k)
{
    uchar8_t rcon = 1;
    register uchar8_t i;
    for (i = 0; i < sizeof(ctx->key); i++) ctx->enckey[i] = ctx->deckey[i] = k[i];
    for (i = 8;--i;) aes_expandEncKey(ctx->deckey, &rcon);
} /* 初始化,对称密钥 */

/* -------------------------------------------------------------------------- */
void aes_done(aes_context *ctx)
{
    register uchar8_t i;
    for (i = 0; i < sizeof(ctx->key); i++)
        ctx->key[i] = ctx->enckey[i] = ctx->deckey[i] = 0;
} /* 清空密钥 */

/* -------------------------------------------------------------------------- */
void aes_encrypt_ecb(aes_context *ctx, uchar8_t *buf)
{
    uchar8_t i, rcon;

    aes_addRoundKey_cpy(buf, ctx->enckey, ctx->key);
    for(i = 1, rcon = 1; i < 14; ++i)
    {
        aes_subBytes(buf);
        aes_shiftRows(buf);
        aes_mixColumns(buf);
        if( i & 1 ) aes_addRoundKey( buf, &ctx->key[16]);
        else aes_expandEncKey(ctx->key, &rcon), aes_addRoundKey(buf, ctx->key);
    }
    aes_subBytes(buf);
    aes_shiftRows(buf);
    aes_expandEncKey(ctx->key, &rcon);
    aes_addRoundKey(buf, ctx->key);
} /* 加密 */

/* -------------------------------------------------------------------------- */
void aes_decrypt_ecb(aes_context *ctx, uchar8_t *buf)
{
    uchar8_t i, rcon;

    aes_addRoundKey_cpy(buf, ctx->deckey, ctx->key);
    aes_shiftRows_inv(buf);
    aes_subBytes_inv(buf);

    for (i = 14, rcon = 0x80; --i;)
    {
        if( ( i & 1 ) )           
        {
            aes_expandDecKey(ctx->key, &rcon);
            aes_addRoundKey(buf, &ctx->key[16]);
        }
        else aes_addRoundKey(buf, ctx->key);
        aes_mixColumns_inv(buf);
        aes_shiftRows_inv(buf);
        aes_subBytes_inv(buf);
    }
    aes_addRoundKey( buf, ctx->key);
} /* 解密 */
回复

使用道具 举报

3

主题

13

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2016-9-19
在线时间
12 小时
 楼主| 发表于 2016-9-21 16:49:02 | 显示全部楼层
这是具体的加密解密操作

#include "aes.h"

uchar8_t encryptdata[1000];
uchar8_t decryptdata[160];

void DATA_AES_ENCRYPT(int Len1, uchar8_t* ERes)
{
int j=0;       
int i=0,k=0;
       
aes_context ctx;
uchar8_t Ebuf[16];

uchar8_t key[32] =  {
                                        0x61, 0x65, 0x31, 0x32, 0x35, 0x65, 0x66, 0x6b, 0x6b, 0x34, 0x34, 0x35, 0x34, 0x65, 0x65, 0x66 //0
                                  ,0x66, 0x34, 0x34, 0x34, 0x66, 0x65, 0x72, 0x66, 0x6b, 0x6e, 0x79, 0x36, 0x6f, 0x78, 0x69, 0x38 //1
                                         };
                       
                        for(i=0;i<Len1;i++)                         
                        {                       
          Ebuf[i%16]=*ERes;
                                  ERes++;       
                                if((i>0) && (i%16==15))
                                {
                                aes_init(&ctx, key);                                         
        aes_encrypt_ecb(&ctx, Ebuf);
                               
                                        for(k=0;k<16;k++)                         
                             {       
                                          encryptdata[k+16 * j]=Ebuf[k];                                       
                             }               
                                j++;
                               
                                }
                                                               
                  }
                        if((Len1%16)!=0)
                        {
                                for(i=0;i<(16-Len1%16);i++)                         
                                {                       
                                                Ebuf[i+Len1%16]=0xFF;       
                                }                       
                                aes_init(&ctx, key);       
                                aes_encrypt_ecb(&ctx, Ebuf);
                                        for(i=0;i<16;i++)                         
                                         {       
                                                        encryptdata[i+16 * j]=Ebuf[i];                                       
                                         }
                               
                        }
                                                 
    aes_done(&ctx);
               
}

void DATA_AES_DECRYPT(int Len2, uchar8_t* DRes)
{
int l=0;       
int m=0,n=0;
       
aes_context ctx;
uchar8_t Dbuf[16];

uchar8_t key[32] = {
                                        0x61, 0x65, 0x31, 0x32, 0x35, 0x65, 0x66, 0x6b, 0x6b, 0x34, 0x34, 0x35, 0x34, 0x65, 0x65, 0x66 //0
                                  ,0x66, 0x34, 0x34, 0x34, 0x66, 0x65, 0x72, 0x66, 0x6b, 0x6e, 0x79, 0x36, 0x6f, 0x78, 0x69, 0x38 //1
                                         };
                       
                        for(m=0;m<Len2;m++)                         
                        {                       
          Dbuf[m%16]=*DRes;
                                  DRes++;       
                                if((m>0) && (m%16==15))
                                {
                                aes_init(&ctx, key);                                         
        aes_decrypt_ecb(&ctx, Dbuf);
                               
                                        for(n=0;n<16;n++)                         
                             {       
                                          decryptdata[n+16 * l]=Dbuf[n];                                       
                             }               
                                l++;
                               
                                }
                                                               
                  }       
                       
    aes_done(&ctx);
               
}
回复

使用道具 举报

3

主题

13

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2016-9-19
在线时间
12 小时
 楼主| 发表于 2016-9-21 16:49:35 | 显示全部楼层
主程序

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "aes.h"



int main (void)
{
          int t,encrypt_len,decrypt_len;
               
                        uchar8_t bufdata[]={0x7E,0x09,0x00,0x1F,0x00,0x01,0x62,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2C,0x00,0x00,
                                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2C,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,0x02,0x63,0x00,0x00,0x00,0x00,0x00,0x00,
                                0x00,0x01,0x38,0x36,0x31,0x33,0x31,0x31,0x30,0x30,0x36,0x31,0x36,0x30,0x38,0x38,0x30,0x00,0x5F,0x00,0x09,0x60,0x00,0x00,
                                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x05,0x00,0x00,0x00,0x00,0x00,
                                0x00,0x03,0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x00,0x19,0x00,0x21,
                                0x00,0x25,0x00,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x03,
                          0x2B,0x03,0x5D,0x00,0x9D,0x00,0x00,0x09,0x01,0xE0,0x00,0x01,0x01,0xDE,0x00,0x00,0x8E,0x09,0x7E};
                       
                                NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
                                delay_init(168);                //延时初始化
                                uart_init(9600);        //串口初始化波特率为115200
               
                                encrypt_len = (sizeof (bufdata) / sizeof (bufdata[1]));                               
                                DATA_AES_ENCRYPT(encrypt_len, (uchar8_t*) &bufdata);
                                delay_ms(10);
                                decrypt_len = (sizeof (encryptdata) / sizeof (encryptdata[1]));
                                DATA_AES_DECRYPT(decrypt_len, (uchar8_t*) &encryptdata);       
                               
//                                if (encrypt_len%16 == 0)         
//                                {
//                                        encrypt_len = encrypt_len;
//                                }
//                                else     
//                                {
//                                        encrypt_len += 16-(encrypt_len%16);
//                                }
//       
//                                for (t = 0;t < encrypt_len;t++)
//                                {
//                                        USART_ClearFlag(USART1,USART_FLAG_TC);          //防止发生数据覆盖(重要)
//                                        USART_SendData(USART1, encryptdata[t]);         //向串口1发送数据
//                                        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
//                                }               
                               
       
                                for (t = 0;t < decrypt_len;t++)
                                {
                                        USART_ClearFlag(USART1,USART_FLAG_TC);          //防止发生数据覆盖(重要)
                                        USART_SendData(USART1, decryptdata[t]);         //向串口1发送数据
                                        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
                                }               

         
        while(1);
               
               
}

回复

使用道具 举报

3

主题

13

帖子

0

精华

新手上路

积分
33
金钱
33
注册时间
2016-9-19
在线时间
12 小时
 楼主| 发表于 2016-9-21 16:57:53 | 显示全部楼层
原子哥,各位大大们在不在啊
怎么没人呢啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-28 17:02

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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