Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用xor操作加密结构元素_C_Encryption_Xor - Fatal编程技术网

C 使用xor操作加密结构元素

C 使用xor操作加密结构元素,c,encryption,xor,C,Encryption,Xor,我用C编写了一个程序,用xor操作在每个字符中加密结构的元素 当我使用包含非数字字符的密码时,代码正常运行。但是,如果密码包含数字字符,则结构的元素不能完全打印。 以下是文件的内容(文件名为“输入”): 代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> //This structure stores the file data typedef struct arqc{ c

我用C编写了一个程序,用xor操作在每个字符中加密结构的元素

当我使用包含非数字字符的密码时,代码正常运行。但是,如果密码包含数字字符,则结构的元素不能完全打印。 以下是文件的内容(文件名为“输入”):

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//This structure stores the file data
typedef struct arqc{
    char* npontos;
    char* carta;
    char* ordem;
    char** matriz;
}Arqc;

int learq( Arqc* fp, FILE* arq );
char criptografa( char ch, char chave );
void decriptografa( Arqc* fp, char* chave, int tam_matriz );

int main()
{
    FILE* arq = fopen("input.txt","r");
    Arqc* fp;
    int i;
    int tam;
    char* chave = "adr";
    fp = (Arqc*) malloc(sizeof(Arqc));
    if( fp == NULL )
        exit(-1);
    tam = learq( fp, arq );
    decriptografa( fp, chave, tam );
    puts( fp->npontos);
    puts( fp->carta);
    puts( fp->ordem);
    for( i = 0; i < tam ; i++){
        puts( *(fp->matriz + i));
    }

    decriptografa( fp, chave, tam );
    puts( fp->npontos);
    puts( fp->carta);
    puts( fp->ordem);
    for( i = 0; i < tam ; i++){
        puts( *(fp->matriz + i));
    }

    for( i = 0; i < tam ; i++){
        free( *(fp->matriz + i));
    }
    free(fp->npontos);
    free(fp->carta);
    free(fp->ordem);
    free(fp);
    fclose( arq );
    return 0;
}

//read the file and stores it in a struct
int learq( Arqc* fp, FILE* arq ){
    int i = 0;
    int tam;
    int n;
    int sair = 1;
    char aux[101];//stores a string read from the file
    /* *************************************************** */
    //this stretch every element of the struct will be filled
    // with the characters in each line of the file
    fgets( aux, 10, arq);
    tam = strlen( aux );
    fp->npontos = (char*) malloc(tam*sizeof(char));
    strncpy( fp->npontos, aux, tam - 1);
    *(fp->npontos + tam - 1) = '\0';
    fgets( aux, 10, arq);
    tam = strlen( aux );
    fp->carta = (char*) malloc(tam*sizeof(char));
    strncpy( fp->carta, aux, tam - 1);
    *(fp->carta + tam - 1) = '\0';
    fgets( aux, 10, arq);
    tam = strlen( aux );
    fp->ordem = (char*) malloc(tam*sizeof(char));
    strncpy( fp->ordem, aux, tam - 1);
    *(fp->ordem + tam - 1) = '\0';
    /* ************************************************** */
    //read here is the character corresponding to the order of the matrix
    //that is converted to the corresponding integer
    n = atoi(fp->ordem);
    //llocating the number of rows of the matrix
    fp->matriz = ( char**) malloc( n*sizeof(char*));
    while( sair ){
        //while the file is not closed, the struct will receive the file data
        if( fgets( aux, 101, arq) != NULL){
            tam = strlen( aux );
            *(fp->matriz + i) = (char*) malloc(tam*sizeof(char));
            strncpy( *(fp->matriz + i), aux, tam - 1);
            *(*(fp->matriz + i) + tam - 1) = '\0';
            i++;
        }
        else
            sair = 0;
    }
 //retorna a dimensão da matriz
 return n;
}

//criptografa cada caractere ch com um caractere chave
char criptografa( char ch, char chave ){
     ch = ( ch&~chave)|(~ch&chave);
     return ch;
}

//decrypts the file that was stored in fp using 'chave' to decrypt
void decriptografa( Arqc* fp, char* chave, int tam_matriz ){
    char aux[101];
    int i, j;
    int n;
    int tchave;
    strcpy( aux, fp->npontos);
    n = strlen( aux );
    tchave = strlen( chave );
    for( i = 0; i < n; i++){
        //decrypts each character read from the struct using the 'chave' characters
        *(fp->npontos + i) = criptografa( *(fp->npontos + i), *(chave + i%tchave));
    }
    strcpy( aux, fp->carta);
    n = strlen( aux );
    for( i = 0; i < n; i++){
        //decrypts each character read from the struct using the 'chave' characters
        *(fp->carta + i) = criptografa( *(fp->carta + i), *(chave + i%tchave));
    }
    strcpy( aux, fp->ordem);
    n = strlen( aux );
    for( i = 0; i < n; i++){
        //decrypts each character read from the struct using the 'chave' characters
        *(fp->ordem + i) = criptografa( *(fp->ordem + i), *(chave + i%tchave));
    }
    for( i = 0; i < tam_matriz; i++){
        strcpy( aux, *(fp->matriz + i));
        n = strlen( aux );
        for( j = 0; j < n; j++){
            //decrypts each character read from the struct using the 'chave' characters
            *(*(fp->matriz + i) + j) = criptografa( *(*(fp->matriz + i) + j), *(chave + i%tchave));
            printf("%c\n", *(*(fp->matriz + i) + j));
        }
    }

}
#包括
#包括
#包括
//此结构存储文件数据
类型定义结构arqc{
char*npontos;
char*carta;
char*ordem;
字符**矩阵;
}Arqc;
int learq(Arqc*fp,文件*arq);
char-criptografa(char-ch,char-chave);
无效说明(Arqc*fp、char*chave、int tam_matriz);
int main()
{
文件*arq=fopen(“input.txt”,“r”);
Arqc*fp;
int i;
INTTAM;
char*chave=“adr”;
fp=(Arqc*)malloc(sizeof(Arqc));
如果(fp==NULL)
出口(-1);
tam=learq(fp,arq);
法令(fp、chave、tam);
看跌期权(fp->NPONTO);
看跌期权(fp->carta);
看跌期权(fp->ordem);
对于(i=0;imatriz+i));
}
法令(fp、chave、tam);
看跌期权(fp->NPONTO);
看跌期权(fp->carta);
看跌期权(fp->ordem);
对于(i=0;imatriz+i));
}
对于(i=0;imatriz+i));
}
免费(fp->NPONTO);
免费(fp->carta);
自由(fp->ordem);
免费(fp);
fclose(arq);
返回0;
}
//读取文件并将其存储在结构中
int learq(Arqc*fp,文件*arq){
int i=0;
INTTAM;
int n;
int-sair=1;
char aux[101];//存储从文件读取的字符串
/* *************************************************** */
//此拉伸将填充结构的每个元素
//在文件的每一行中都有字符
fgets(辅助、10、arq);
tam=strlen(辅助);
fp->npontos=(char*)malloc(tam*sizeof(char));
strncpy(fp->npontos,aux,tam-1);
*(fp->npontos+tam-1)='\0';
fgets(辅助、10、arq);
tam=strlen(辅助);
fp->carta=(char*)malloc(tam*sizeof(char));
strncpy(fp->carta,aux,tam-1);
*(fp->carta+tam-1)='\0';
fgets(辅助、10、arq);
tam=strlen(辅助);
fp->ordem=(char*)malloc(tam*sizeof(char));
strncpy(fp->ordem,aux,tam-1);
*(fp->ordem+tam-1)='\0';
/* ************************************************** */
//此处读取的是与矩阵顺序对应的字符
//转换为相应的整数
n=原子(fp->ordem);
//分配矩阵的行数
fp->matriz=(字符**)malloc(n*sizeof(字符*);
while(sair){
//文件未关闭时,结构将接收文件数据
如果(fgets(辅助,101,arq)!=NULL){
tam=strlen(辅助);
*(fp->matriz+i)=(char*)malloc(tam*sizeof(char));
strncpy(*(fp->matriz+i),辅助,tam-1);
*(*(fp->matriz+i)+tam-1)='\0';
i++;
}
其他的
sair=0;
}
//迪蒙斯·达马特里兹酒店
返回n;
}
//手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿手稿
char-criptografa(char-ch,char-chave){
ch=(ch&chave)|(~ch&chave);
返回ch;
}
//使用“chave”解密存储在fp中的文件
无效说明(Arqc*fp、char*chave、int tam_matriz){
char-aux[101];
int i,j;
int n;
国际贸易组织;
strcpy(辅助、fp->NPONTO);
n=strlen(辅助);
tchave=strlen(chave);
对于(i=0;inpontos+i)=scriptografa(*(fp->npontos+i),*(chave+i%tchave));
}
strcpy(辅助,fp->carta);
n=strlen(辅助);
对于(i=0;icarta+i)=scriptografa(*(fp->carta+i),*(chave+i%TCHAD));
}
strcpy(辅助,fp->ordem);
n=strlen(辅助);
对于(i=0;iordem+i)=criptografa(*(fp->ordem+i),*(chave+i%tchad));
}
对于(i=0;imatriz+i));
n=strlen(辅助);
对于(j=0;jmatriz+i)+j)=scriptografa(*(*(fp->matriz+i)+j),*(chave+i%tchad));
printf(“%c\n”,*(*(fp->matriz+i)+j));
}
}
}

由于您提供了输入文件的内容,我几乎可以肯定,主要问题是您在加密文本上使用了面向字符串的string.h和stdio.h函数

想想看:你的输入文件大部分是由数字组成的,你的问题密码也包含数字。一个字符与自身异或将产生一个零字节,大多数处理字符串的C函数都会将其视为字符串的结尾

编辑:

解决此问题的方法是在每个缓冲区中附带一个整数,该整数将指示缓冲区中的字符数,然后使用内存区域操纵函数,如memcpy、memcmp等。您还应该避免文件I/O的*put()和*get(),因为它们也是面向字符串的

编辑2:


基本上,您应该将输入文件视为包含未知内容的二进制文件,而不是文本文件。这将使您的代码更加健壮和通用,但也意味着您不能使用任何str*()函数。

由于您提供了输入文件的内容,我几乎可以肯定,主要问题是您在加密文本上使用了面向字符串的string.h和stdio.h函数

想想看:你的输入文件主要由数字和你的问题密码组成
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//This structure stores the file data
typedef struct arqc{
    char* npontos;
    char* carta;
    char* ordem;
    char** matriz;
}Arqc;

int learq( Arqc* fp, FILE* arq );
char criptografa( char ch, char chave );
void decriptografa( Arqc* fp, char* chave, int tam_matriz );

int main()
{
    FILE* arq = fopen("input.txt","r");
    Arqc* fp;
    int i;
    int tam;
    char* chave = "adr";
    fp = (Arqc*) malloc(sizeof(Arqc));
    if( fp == NULL )
        exit(-1);
    tam = learq( fp, arq );
    decriptografa( fp, chave, tam );
    puts( fp->npontos);
    puts( fp->carta);
    puts( fp->ordem);
    for( i = 0; i < tam ; i++){
        puts( *(fp->matriz + i));
    }

    decriptografa( fp, chave, tam );
    puts( fp->npontos);
    puts( fp->carta);
    puts( fp->ordem);
    for( i = 0; i < tam ; i++){
        puts( *(fp->matriz + i));
    }

    for( i = 0; i < tam ; i++){
        free( *(fp->matriz + i));
    }
    free(fp->npontos);
    free(fp->carta);
    free(fp->ordem);
    free(fp);
    fclose( arq );
    return 0;
}

//read the file and stores it in a struct
int learq( Arqc* fp, FILE* arq ){
    int i = 0;
    int tam;
    int n;
    int sair = 1;
    char aux[101];//stores a string read from the file
    /* *************************************************** */
    //this stretch every element of the struct will be filled
    // with the characters in each line of the file
    fgets( aux, 10, arq);
    tam = strlen( aux );
    fp->npontos = (char*) malloc(tam*sizeof(char));
    strncpy( fp->npontos, aux, tam - 1);
    *(fp->npontos + tam - 1) = '\0';
    fgets( aux, 10, arq);
    tam = strlen( aux );
    fp->carta = (char*) malloc(tam*sizeof(char));
    strncpy( fp->carta, aux, tam - 1);
    *(fp->carta + tam - 1) = '\0';
    fgets( aux, 10, arq);
    tam = strlen( aux );
    fp->ordem = (char*) malloc(tam*sizeof(char));
    strncpy( fp->ordem, aux, tam - 1);
    *(fp->ordem + tam - 1) = '\0';
    /* ************************************************** */
    //read here is the character corresponding to the order of the matrix
    //that is converted to the corresponding integer
    n = atoi(fp->ordem);
    //llocating the number of rows of the matrix
    fp->matriz = ( char**) malloc( n*sizeof(char*));
    while( sair ){
        //while the file is not closed, the struct will receive the file data
        if( fgets( aux, 101, arq) != NULL){
            tam = strlen( aux );
            *(fp->matriz + i) = (char*) malloc(tam*sizeof(char));
            strncpy( *(fp->matriz + i), aux, tam - 1);
            *(*(fp->matriz + i) + tam - 1) = '\0';
            i++;
        }
        else
            sair = 0;
    }
 //retorna a dimensão da matriz
 return n;
}

//criptografa cada caractere ch com um caractere chave
char criptografa( char ch, char chave ){
     ch = ( ch&~chave)|(~ch&chave);
     return ch;
}

//decrypts the file that was stored in fp using 'chave' to decrypt
void decriptografa( Arqc* fp, char* chave, int tam_matriz ){
    char aux[101];
    int i, j;
    int n;
    int tchave;
    strcpy( aux, fp->npontos);
    n = strlen( aux );
    tchave = strlen( chave );
    for( i = 0; i < n; i++){
        //decrypts each character read from the struct using the 'chave' characters
        *(fp->npontos + i) = criptografa( *(fp->npontos + i), *(chave + i%tchave));
    }
    strcpy( aux, fp->carta);
    n = strlen( aux );
    for( i = 0; i < n; i++){
        //decrypts each character read from the struct using the 'chave' characters
        *(fp->carta + i) = criptografa( *(fp->carta + i), *(chave + i%tchave));
    }
    strcpy( aux, fp->ordem);
    n = strlen( aux );
    for( i = 0; i < n; i++){
        //decrypts each character read from the struct using the 'chave' characters
        *(fp->ordem + i) = criptografa( *(fp->ordem + i), *(chave + i%tchave));
    }
    for( i = 0; i < tam_matriz; i++){
        strcpy( aux, *(fp->matriz + i));
        n = strlen( aux );
        for( j = 0; j < n; j++){
            //decrypts each character read from the struct using the 'chave' characters
            *(*(fp->matriz + i) + j) = criptografa( *(*(fp->matriz + i) + j), *(chave + i%tchave));
            printf("%c\n", *(*(fp->matriz + i) + j));
        }
    }

}