duanlei8119 2016-10-30 10:21
浏览 17
已采纳

PHP加密算法

I am working on a web server that receives encrypted data from an ESP8266 module. I have managed to get the encryption and decryption fully functional in C, however PHP is giving me a small problem. I have narrowed the problem the these two functions in the decryption (AES encrypt/decrypt is used):

This function performs multiplication with the Galo's field:

function gmul($a, $b) {
$p = 0;
$counter;
$hi_bit_set;
for($counter = 0; $counter < 8; $counter++) {
    if(($b & 1) == 1) 
        $p ^= $a;
    $hi_bit_set = ($a & 0x80);
    $a <<= 1;
    if($hi_bit_set == 0x80) 
        $a ^= 0x1b;     
    $b >>= 1;
}
return $p;}

This will mix the columns with the use of multiplication of the Galos field:

    function invMixColumns($w){
  $i;
  $j;
  $k;
  $temp;

  for($j = 0; $j < 4; $j++){
    for($k =0; $k < 4; $k++){
      $temp[$j][$k] = $w[$j][$k];
    }
  }


  for($i = 0; $i < 4; $i++)
  {
    $w[0][$i] = gmul($temp[0][$i],14) ^ gmul($temp[3][$i],9) ^ gmul($temp[2][$i],13) ^ gmul($temp[1][$i],11);
    $w[1][$i] = gmul($temp[1][$i],14) ^ gmul($temp[0][$i],9) ^ gmul($temp[3][$i],13) ^ gmul($temp[2][$i],11);
    $w[2][$i] = gmul($temp[2][$i],14) ^ gmul($temp[1][$i],9) ^ gmul($temp[0][$i],13) ^ gmul($temp[3][$i],11);
    $w[3][$i] = gmul($temp[3][$i],14) ^ gmul($temp[2][$i],9) ^ gmul($temp[1][$i],13) ^ gmul($temp[0][$i],11);
  }

  return $w;

}

The invMixColumns function recieves the following input (hex)

[ff][6][20][d5]
[10][bd][eb][4c]
[28][59][4e][69]
[6e][84][79][7]

And the expected output should be (hex): (Tested in C)

[dd][77][53][34]
[15][f1][99][3f]
[43][e4][de][7d]
[22][04][e8][81]

The actual output in PHP is close but there is a prefix hex value as seen here (hex):

[7dd][377][753][434]
[415][2f1][499][63f]
[643][3e4][6de][57d]
[522][204][5e8][781]

It is possibly a syntax error that I made. Any Ideas? Is there a way to simply remove the first hex number, for example 0x7dd -> 0xdd ? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doubianyan9749 2016-11-03 06:58
    关注

    By using the & (bitwise and operator) I managed to get the output that I wanted.

    0xd77 & 0xff = 0x77
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧