AES加密算法代码学习之mixColumns函数分析

本文探讨了AES加密中的关键步骤——mixColumns函数,详细解释了矩阵运算过程和数据排列方式。通过实例展示了如何计算出it[0]的新值,并强调了异或运算在其中的作用,帮助读者理解这一核心概念。

工程参考:https://github.com/bricke/Qt-AES 

函数定义如下:

// MixColumns function mixes the columns of the state matrix
//optimized!!
void QAESEncryption::mixColumns()
{
  QByteArray::iterator it = m_state->begin();
  quint8 tmp, tm, t;

  for(int i = 0; i < 16; i += 4){
    t       = (quint8)it[i];
    tmp     =  (quint8)it[i] ^ (quint8)it[i+1] ^ (quint8)it[i+2] ^ (quint8)it[i+3] ;

    tm      = xTime( (quint8)it[i] ^ (quint8)it[i+1] );
    it[i]   = (quint8)it[i] ^ (quint8)tm ^ (quint8)tmp;

    tm      = xTime( (quint8)it[i+1] ^ (quint8)it[i+2]);
    it[i+1] = (quint8)it[i+1] ^ (quint8)tm ^ (quint8)tmp;

    tm      = xTime( (quint8)it[i+2] ^ (quint8)it[i+3]);
    it[i+2] =(quint8)it[i+2] ^ (quint8)tm ^ (quint8)tmp;

    tm      = xTime((quint8)it[i+3] ^ (quint8)t);
    it[i+3] =(quint8)it[i+3] ^ (quint8)tm ^ (quint8)tmp;
  }
}

矩阵运算用的的常量矩阵如下:

数据排列是从上到下,从左到右,也就是it[0] = 0xd4,it[1] = 0xbf,it[2] = 0x5d,it[3] = 0x30;

那么混叠列运算得到的it[0] = 02*d4 ⊕ 03*bf ⊕ 5d ⊕ 30 = 02*d4 ⊕ 03*bf ⊕ 5d ⊕ 30 ⊕ d4 ⊕ d4 = 02*d4 ⊕ 02*bf ⊕ bf ⊕ 5d ⊕ 30 ⊕ d4 ⊕ d4

= d4 ⊕ 02*d4 ⊕ 02*bf ⊕ bf ⊕ 5d ⊕ 30 ⊕ d4

= d4 ⊕ 02*d4 ⊕ 02*bf ⊕ d4 ⊕ bf ⊕ 5d ⊕ 30

= d4 ⊕ 02*(d4 ⊕ bf)d4 ⊕ bf ⊕ 5d ⊕ 30

其中02*(d4 ⊕ bf)对应代码中的tm,4 ⊕ bf ⊕ 5d ⊕ 30对应代码中的tmp。

理解的关键在于异或运算:

  1. A ⊕ A = 0,其他项与其异或是其本身,比如1011 = 1011 ⊕ 1010 ⊕ 1010

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值