错误:二进制操作数无效^(有'char *'和'char *')

时间:2014-03-14 18:31:49

标签: c aes

 KeyExpansion(char **key){
        char **word, *temp;int i;
    //Allocate memory for word 4x11  expanded key matrix dynamically (44)
        word =(char **) malloc(4*sizeof(char*));

        for(i=0;i<4;i++)
            word[i]=(char *) malloc(11*sizeof(char));

    //Allocate memory for temp 4 byte word
        //temp =(int **) malloc(4*sizeof(int*));
        //for(i=0;i<4;i++)
            temp=(char *) malloc(4*sizeof(char));


        for(i=0;i<4;i++)
            word[i]=(key[4*i],key[4*i+1],key[4*i+2],key[4*i+3]);

        for(i=4;i<44;i++){
            temp = word[i-1];
            if(i%4==0)
            //  temp = SubByte(RotByte((int *)temp)) ^ Rcon[i/4];
            word[i]= (word[i-4]) ^ (temp);
        }
        PrintArr((char**)word);
    }

我尝试了类型转换,

word[i]= ((int)word[i-4]) ^ ((int)temp);
然后错误就消失了,但后来我得到了分段错误......

请告知这条代码有什么问题。完整代码如下所示

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<limits.h>
#include<string.h>
#define CHAR_BIT 8
//#define cryptkey "0123456789abcdef"

 char cryptkey[]= "0123456789abcdef";

void ReadInput(char **);
void PrintArr(char **);
int AddRoundKey(int k,char **,char**);

void main(){

char **state,i,j;
char **key;int c=0;
    //Allocate memory for 4x4  state matrix dynamically
    state =(char **) malloc(4*sizeof(char*));

    for(i=0;i<4;i++)
        state[i]=(char *) malloc(4*sizeof(char));

    //Allocate memory for 4x4  key matrix dynamically
    key =(char **) malloc(4*sizeof(char*));

    for(i=0;i<4;i++)
        key[i]=(char *) malloc(4*sizeof(char));

    //Read plaintext entered by user and store in 4x4 matrix.
    ReadInput((char**) state); 
    printf("\n User input stored as 4x4 Matrix...\n");

    PrintArr((char**)state);          

    printf("\nStored key as 4x4 Matrix...\n");
    // Store the key in 4x4 matrix
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
          key[j][i]= cryptkey[c];c++;
        }
    }
    //verify that key stored properly by printing on console
    PrintArr((char**)key);


    //Begin AES Process
    //Add Round Key for 0th Iteration
    printf("\nState Array Adding Round Key for 0th Iteration\n");
    if(AddRoundKey(0,(char **)key,(char **)state)!=0)
        printf("Error in Add Round key for 0th Iteration...\n");
    else
        PrintArr((char**)state);
   KeyExpansion((char**)key);
}

void ReadInput(char **state){
    int i,j;
    printf("Enter the hexadecimal Plaintext..\t");
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
            scanf("%x",&state[j][i]);
}

void PrintArr(char **state){
    int i,j;
   // printf("\nArray After Operation is...\n");
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            printf("%d ",state[i][j]);
        }
    printf("\n");
    }        
}

AddRoundKey(int k,char **key,char **state){
    int i,j;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
            state[j][i] ^= key[j][i];
    return 0;
}


KeyExpansion(char **key){
    char **word, *temp;int i;
//Allocate memory for word 4x11  expanded key matrix dynamically (44)
    word =(char **) malloc(4*sizeof(char*));

    for(i=0;i<4;i++)
        word[i]=(char *) malloc(11*sizeof(char));

//Allocate memory for temp 4 byte word
    //temp =(int **) malloc(4*sizeof(int*));
    //for(i=0;i<4;i++)
        temp=(char *) malloc(4*sizeof(char));


    for(i=0;i<4;i++)
        word[i]=(key[4*i],key[4*i+1],key[4*i+2],key[4*i+3]);

    for(i=4;i<44;i++){
        temp = word[i-1];
        if(i%4==0)
        //  temp = SubByte(RotByte((int *)temp)) ^ Rcon[i/4];
        word[i]= (word[i-4]) ^ (temp);
    }
    PrintArr((char**)word);
}
如果愚蠢的问题,请道歉......请指引我正确的方向。

1 个答案:

答案 0 :(得分:0)

如果您要求的是两个字符上的按位或,那么您只需要取消引用它们

word[i]= *word[i-4] ^ *temp;