c - 在 C 中为 AES 中的移位行移位数组元素

标签 c arrays pointers aes

我正在尝试实现(AES 加密的)移位行功能,我编写了以下函数,但是对于对应于位置 2,3 3,2 和 3,3 的数组元素没有正确交换。请看看并建议...

预期输出 http://en.wikipedia.org/wiki/File:AES-ShiftRows.svg

void shiftRow(unsigned char **state){

int i,j,n; unsigned char *temp;

    temp =(unsigned char *)malloc(sizeof(unsigned char));

    for(i=1;i<4;i++){
        *temp =state[i][0];
        printf("\t%x",*temp);
        for(j=0;j<4;j++){
            n = ((i+j)>3?(i+j)-4:i+j);
            state[i][j]=state[i][n];
        }
        printf("\t%x\n",*temp);
        state[i][j-i]=*temp;
    }


}

最佳答案

我不得不做一些解决方法,不是完美的解决方案,但完成了工作...

void shiftRow(unsigned char **state){
    int i,j,n; unsigned char temp;

    //  temp =(unsigned char *)malloc(sizeof(unsigned char));

        for(i=1;i<2;i++){
            temp =state[i][0];
            //printf("\t%x",temp);
            for(j=0;j<4;j++){
                n = ((i+j)>3?(i+j)-4:i+j);
                state[i][j]=state[i][n];        
            }
            printf("\t%x\n",temp);
            state[i][j-i]=temp;

        }

            temp =state[2][0];
            state[2][0]=state[2][2];
            state[2][2]=temp;
            temp =state[2][1];
            state[2][1]=state[2][3];
            state[2][3]=temp;

            temp =state[3][0];
            state[3][0]=state[3][3];
            state[3][3]=state[3][2];
            state[3][2]=state[3][1];
            state[3][1]=temp;

    } //shiftrow ends

任何其他解决方案/方法将不胜感激

关于c - 在 C 中为 AES 中的移位行移位数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462704/

相关文章:

C结构代码编译但未运行

c - 在 C 中解析特定格式的文件

c - C中的流和管道有什么区别

c - 这个 .h 正确吗?

python - numpy数组的边界框

javascript - 通过 object.property 将数组过滤为唯一对象

c - 如何交换指针变量以声明一个大小最初未知的数组?

java - 原始数据类型数组与原始数据类型操作

c - bsearch() 返回 (nil),导致段错误

c - 内存泄漏与否 - 链表