gpt4 book ai didi

c - 错误: invalid operands to binary ^ (have ‘char *’ and ‘char *’ )

转载 作者:行者123 更新时间:2023-11-30 17:37:24 25 4
gpt4 key购买 nike

 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);
}

抱歉,如果问题很愚蠢......请引导我正确的方向。

最佳答案

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

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

关于c - 错误: invalid operands to binary ^ (have ‘char *’ and ‘char *’ ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com