【问题标题】:Attempting to produce linear approximation table. Values in output are incorrect试图生成线性近似表。输出中的值不正确
【发布时间】:2020-12-16 20:35:15
【问题描述】:

这里是c语言给的代码,s box表是{0xE, 0x4, 0xD, 0x1, 0x2, 0xF, 0xB, 0x8, 0x3, 0xA, 0x6, 0xC, 0x5, 0x9, 0x0, 0x7 };,当我们运行这段代码时,我们得到了错误的输出。我们应该从这段代码中得到线性逼近表。我认为我得到的错误出现在 sbox 输出部分。

#include <stdio.h>
#include <stdlib.h>
typedef unsigned short int UINT16; 

UINT16  sbox_table[16] =  {0xE, 0x4, 0xD, 0x1, 0x2, 0xF, 0xB, 0x8, 0x3, 0xA, 0x6, 0xC, 0x5, 0x9, 0x0, 0x7}; 

int lin_appr_table[16][16];


void construct_lin_appr_table()
{
    UINT16 i, j, k;

    UINT16 X, Y, x;
    UINT16 X_xor, Y_xor;
    int counter;

    for (i=0 ; i<16 ; i++)//sbox input
    {
        
        for (j=0 ; j<16 ; j++)//sbox output
        {
            X=i;
            Y=j;
            
            counter=0;
            for (k=0;k<16;k++)
            {
              X_xor=X&k;
              Y_xor=Y&sbox_table[k];
              
              if(X_xor^Y_xor==0) counter++;
              
             
            }
            
            lin_appr_table[i][j]=counter-8;
            
            //Write the code that makes up the table

        }

    }

    //Write the code that printed the table on the screen
    
    
    for (i=0 ; i<16 ; i++)//sbox input
    {
        for (j=0 ; j<16 ; j++)//sbox output
        {
            printf("% d ", lin_appr_table[i][j]);
        }
        printf("\n");
    }
}  

int main()
{
    construct_lin_appr_table();
    
    getch();
    return 0;
}

预期输出:

【问题讨论】:

  • 注意if(X_xor^Y_xor==0) 中的运算符优先级。你的意思是if((X_xor^Y_xor)==0)
  • X_xor=X&amp;k 应该是X_xor = X &amp; (1 &lt;&lt; k) 吗?只是猜测。
  • 您可以在此处编辑帖子以显示预期输出吗? (我看到您几乎将其发布为答案,但最好在这里。)
  • 这样做的目的是:上面的代码没有给我们这个期望的表,目的是找到代码中的错误,以便把这个表(期望的表)作为输出。跨度>
  • 感谢您的推荐。希望得到答案

标签: c algorithm encryption cryptography des


【解决方案1】:

建议进行以下两项更改以生成下表:

      //if(X_xor^Y_xor==0) counter++;
      if((X_xor^Y_xor)==0) counter++;

注意:对于上述语句,打开警告应该会导致类似于:

36, 19    warning: ^ has lower precedence than ==; == will be evaluated first   
      //X_xor=X&k;
      X_xor = X & (1 << k); //credit to Weather Vane in comments

结果如下表:

Asides:通常,在声明期间初始化变量也是一个好主意,并且总是在使用之前。例如:

int lin_appr_table[16][16] = {{0}};//initializes all locations to 0

如果可以,请选择使用便携式而不是non-portable 代码:

    getchar(); //portable
    //getch(); //not portable

【讨论】:

  • 当我做出改变时,我得到了另一张桌子。
  • 如果您指的是 ^ 更改,那么是的,这将导致不同的值。你是说最好先评估Y_xor==0? (即:X_xor^(Y_xor==0))作为预期评估?
  • 我可以发邮件或其他什么来讨论这个问题
  • @S.Nesimi - 没有。不幸的是,这行不通。只需在原始帖子的代码部分下方的某处发布预期的输出,并在那里添加关于正确算法的额外说明
【解决方案2】:

键码变更:见//if ((X_xor ^ Y_xor) == 0)

#include <stdio.h>
#include <stdlib.h>
typedef unsigned short int UINT16;

const UINT16 sbox_table[16] = {0xE, 0x4, 0xD, 0x1, 0x2, 0xF, 0xB, 0x8, 0x3, 0xA,
    0x6, 0xC, 0x5, 0x9, 0x0, 0x7};

int lin_appr_table[16][16];

// There exist various hacks to quickly count the ones in an integer.
// This is just a simple loop.
unsigned count1(unsigned i) {
  unsigned count = 0;
  while (i) {
    count += i & 1;
    i >>= 1;
  }
  return count;
}

void construct_lin_appr_table() {
  UINT16 i, j, k;
  UINT16 X, Y; //, x;
  UINT16 X_xor, Y_xor;
  int counter;

  for (i = 0; i < 16; i++) {
    for (j = 0; j < 16; j++) {
      X = i;
      Y = j;

      counter = 0;
      for (k = 0; k < 16; k++) {
        X_xor = X & k;
        Y_xor = Y & sbox_table[k];

        //if ((X_xor ^ Y_xor) == 0)
        //if ((count1(X_xor) - count1(Y_xor)) % 2 == 0)
        if ((count1(X_xor ^ Y_xor) & 1) == 0)
          counter++;
      }
      lin_appr_table[i][j] = counter - 16 / 2;
    }
  }

  for (i = 0; i < 16; i++) {
    for (j = 0; j < 16; j++) {
      printf("% d ", lin_appr_table[i][j]);
    }
    printf("\n");
  }
}

int main() {
  construct_lin_appr_table();
  return 0;
}

输出

 8  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 
 0  0 -2 -2  0  0 -2  6  2  2  0  0  2  2  0  0 
 0  0 -2 -2  0  0 -2 -2  0  0  2  2  0  0 -6  2 
 0  0  0  0  0  0  0  0  2 -6 -2 -2  2  2 -2 -2 
 0  2  0 -2 -2 -4 -2  0  0 -2  0  2  2 -4  2  0 
 0 -2 -2  0 -2  0  4  2 -2  0 -4  2  0 -2 -2  0 
 0  2 -2  4  2  0  0  2  0 -2  2  4 -2  0  0 -2 
 0 -2  0  2  2 -4  2  0 -2  0  2  0  4  2  0  2 
 0  0  0  0  0  0  0  0 -2  2  2 -2  2 -2 -2 -6 
 0  0 -2 -2  0  0 -2 -2 -4  0 -2  2  0  4  2 -2 
 0  4 -2  2 -4  0  2 -2  2  2  0  0  2  2  0  0 
 0  4  0 -4  4  0  4  0  0  0  0  0  0  0  0  0 
 0 -2  4 -2 -2  0  2  0  2  0  2  4  0  2  0 -2 
 0  2  2  0 -2  4  0  2 -4 -2  2  0  2  0  0  2 
 0  2  2  0 -2 -4  0  2 -2  0  0 -2 -4  2 -2  0 
 0 -2 -4 -2 -2  0  2  0  0 -2  4 -2 -2  0  2  0 

Ref

请参阅How to count the number of set bits in a 32-bit integer? 以获得更快的count1()

【讨论】:

  • 它正在工作。非常感谢你帮助我。我真的很感激。
猜你喜欢
  • 1970-01-01
  • 2020-05-22
  • 2016-05-16
  • 2020-04-03
  • 1970-01-01
  • 2014-03-06
  • 2023-03-07
  • 2021-03-18
  • 2019-08-29
相关资源
最近更新 更多