簡體   English   中英

在Linux和Windows上編譯時,C程序的結果不同

[英]C Program don't have the same result when compiled on linux and windows

當我在Windows上使用mingw並在Linux上使用gcc編譯程序時,可執行文件不會給我相同的結果。

該代碼應獲取文件並對其進行加密。

但是,如果如果最后一個塊未滿或文件小於32個字節,我需要添加一些字節時,Linux會像我想要的那樣添加值“ 0”,但是在Windows上它不起作用。

順便說一下,每個的十六進制轉儲都不相似。

執行它:./encrypt [FileIn] [FileOut] 1 [yourKey] [yourIV]

將您想要的任何東西放在Key和IV上,它們被寫在測試代碼中(我只是輸入“ 1”和“ 1”以免出錯)

argv [3]必須為“ 1”才能加密

我正在使用內部寫有“ toto”的文件來測試它(輸入您想要的任何內容,但請確保不超過32個字符,因為這是我目前正在測試的東西)。

Key和IV在要調試的代碼內相同,這不是錯誤。

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

/*FUNCTIONS*/
int encrypt(char* in, char* out, char* key, char* primalIv);
int decrypt(char* in, char* out);
void myprint(char* content, int size);
char* xor(char* data, char* key, int size);
void bitStuffing(char* in, int rem);

/*GLOBAL VARIABLES*/
int errorLevel;
int blockSize = 32; /*Tailles de blocs en octet*/

/*CALL : ./main inFile outFile methode key iv*/
/*FILE STRUC : [datas] [stuffing] [stuffingValue] [iv]*/
int main(int argc, char** argv)
{
    /*Support de l'aide à l'utilisateur*/
    if(argc == 2 && !strcmp(argv[1],"help"))
    {
        printf("\nUsage : ./cied [inFile] [outFile] [methode] [key] [iv]\n\n");
        printf("[inFile]  : Input file to use\n");
        printf("[outFile] : Ouput file to use\n");
        printf("[methode] : Encrypt (1) or decrypt (0)\n");
        printf("[key]     : The key to use\n");
        printf("[iv]      : The initialisation vector.\n");
        printf("            (none for decrypt !)\n");
        return 0;       /*Fermeture gratieuse du programme*/
    }

    /*Support des erreurs d'arguments*/
    if(argc != 5 && argc != 6)
    {
        printf("[ERR] Args error!\n");
        printf("[INF] Enter 'help' to display help\n");
        return 1;
    }
    else if(atoi(argv[3]) == 1 && argc != 6)        /*Nombre d'arguments pour chiffrement*/
    {
        printf("[ERR] Args error : given %d when waiting 5!\n",(argc-1));
        printf("[INF] Enter 'help' to display help\n");
        return 1;
    }
    else if(atoi(argv[3]) == 0 && argc != 5)/*Nombre d'arguments pour dechiffrement*/
    {
        printf("[ERR] Args error : given %d when waiting 4!\n",(argc-1));
        printf("[INF] Enter 'help' to display help\n");
        return 1;
    }


    /***Chiffrement et dechiffremet***/
    if(atoi(argv[3]) == 1)
    {
        errorLevel = encrypt(argv[1], argv[2],"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");/*Chiffrement*/
        if(!errorLevel)
        {   
            printf("[INF] finished\n");
        }
        else
        {
            printf("[ERR] An error as occured.\n");
        }
    }
    else if(atoi(argv[3]) == 0)
    {
        errorLevel = decrypt(argv[1], argv[2]);     /*Dechiffrement*/
        if(!errorLevel)
        {   
            printf("[INF] finished\n");
        }
        else
        {
            printf("[ERR] An error as occured.\n");
        }
    }
    else
    {
        printf("[ERR] Wrong method given!");
        printf("[INF] Enter 'help' to display help\n");
        return 1;
    }
    return 0;
}


/*FONCTIONS CORE*/
int encrypt(char* in, char* out, char* key, char* primalIv)
{
    /*Variables*/
    char* block = NULL;
    char* xored = NULL;
    char* iv = NULL;
    int readed;
    int inFileSize;
    int numOfBlock;
    int rem = 0;
    int i;
    FILE *inFile;
    FILE *outFile;

    iv = primalIv;

    /*Ouverture des fichiers*/
    inFile = fopen(in,"rb");
    outFile = fopen(out,"w");

    /*Gestion des erreurs de fichier*/
    if(inFile == NULL)
    {
        printf("[ERR] Problem reading input file. Please check path and permissions.\n");
    }
    else if(outFile == NULL)
    {
        printf("[ERR] Problem reading output file. Please check path and permissions.\n");
    }

    block = malloc(blockSize);

    /*Récupération de la longueur du fichier*/
    fseek(inFile, 0, SEEK_END);
    inFileSize = ftell(inFile);
    rewind(inFile);

    /*Calcul du nombre de bloc et du reste*/
    numOfBlock = inFileSize / blockSize;
    rem = inFileSize % blockSize;
    printf("[INF] File will be split in %d block(s). %d will remain.\n",numOfBlock,rem);
    if(inFileSize < blockSize)
    {
        readed = fread(block,1,blockSize,inFile);
        printf("[INF] readed %d bytes \n",readed);
        /*Bourrage*/
        bitStuffing(block,(blockSize-readed));
        /*Xor avec l'IV*/
        xored = xor(block,iv,blockSize);
        /*Xor avec la clé*/
        xored = xor(xored,key,blockSize);
        /*Sauvegarde du block pour réutilisation en tant qu'IV*/
        iv = xored;
        /*Ecriture dans le fichier*/
        fwrite(xored,1,blockSize,outFile);
    }
    else
    {
        for(i=0;i<numOfBlock;i++)
        {
            printf("qzekonfk le\n");
            readed = fread(block,1,blockSize,inFile); 
            printf("[INF] readed %d bytes \n",readed);
            if(readed != blockSize)
            {
                printf("[WRN] The readed block siez is different than usual !(%d != %d)\n",blockSize,readed);
            }
            /*Xor avec l'IV*/
            printf("IV :\n");
            xored = xor(block,iv,readed);
            /*Xor avec la clé*/
            printf("KEY :\n");
            xored = xor(xored,key,readed);
            /*Sauvegarde du block pour réutilisation en tant qu'IV*/
            iv = xored;
            /*Ecriture dans le fichier*/
            fwrite(xored,1,readed,outFile);
        }
        /*Bourrage*/
        if(rem)
        {
            readed = fread(block,1,blockSize,inFile);
            printf("[INF] readed %d bytes \n",readed);
            /*Bourrage*/
            bitStuffing(block,(blockSize-readed));
            /*Xor avec l'IV*/
            xored = xor(block,iv,readed);
            /*Xor avec la clé*/
            xored = xor(xored,key,readed);
            /*Sauvegarde du block pour réutilisation en tant qu'IV*/
            iv = xored;
            /*Ecriture dans le fichier*/
            fwrite(xored,1,readed,outFile);
        }
    }

    /*Inscription de rem dans le fichier pour préparer le déchiffrement*/
    fprintf(outFile, "%c",rem);

    /*Inscription de l'IV*/
    printf("IV (again):\n");
    fwrite(xor(primalIv,key,blockSize),1,blockSize,outFile);

    /*Free des malloc*/
    free(block);
    free(xored);

    return 0;
}

int decrypt(char* in, char* out)
{

    return 0;
}

/*FONCTIONS UTILITAIRES*/
char* xor(char* data, char* key, int size)
{
    int i;
    char* result = malloc(size);
    for(i=0; i<size; i++)
    {
        result[i] = data[i] ^ key[i];
        printf("  %x ^ %x = %x\n",data[i],key[i],result[i]);
    }
    return result;
}

void myprint(char* content, int size)
{
    int i;
    printf("  ");
    for(i=0;i<=blockSize;i++)
    {
        printf(" %x",content[i]);
    }
    printf("\n");
}

void bitStuffing(char* in, int rem) {
    int i;
    printf("\nBEGIN STUFFING");
    for(i=rem; i<sizeBlock; i++) {
        in[i] = 0;
        /*printf("%x", in[i]);*/
    }
    printf("\nEND STUFFING\n");
}

@alk您是對的,我在閱讀您的帖子之前就發現了它。 我的朋友給bitStuffing()函數錯誤的值。 他不需要給出空字節數,而需要給出循環必須開始的字節數。 那會是

bitStuffing(block, readed);

並不是

bitStuffing(block, (blockSize-readed));

對於每個想要少一點代碼的人,我將其創建為實質上具有填充物:

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

void bitStuffing(char* in, int rem);
void myprint(char* content, int size);
int blockSize = 32;

int main()
{
    char* block;
    int inFileSize = 4; /*In my example, the file used is a text file containing "toto"*/
    int readed;
    FILE *outFile;
    readed = inFileSize % blockSize; /*Getting the number of bits to stuff*/
    block  = malloc(blockSize);
    block[0] = "t";
    block[1] = "o";
    block[2] = "t";
    block[3] = "o";
    bitStuffing(block, readed);
    outFile  = fopen("C:/Users/Julien/Desktop/text3.txt", "wb");
    fwrite(block,1,blockSize,outFile);
    fclose(outFile);
    return 0;
}


void bitStuffing(char* in, int begin) {
    printf("\nBEGIN STUFFING");
    printf("\nrem =%2d", begin);
    int i;
    for(i=begin; i<blockSize; i++) {
        in[i] = 0;
        printf("\ni =%1d | value =%1x\n     ",i, in[i]);
        myprint(in, i);
    }
    printf("\nEND STUFFING\n");
}


void myprint(char* content, int size)
{
    int i;
    printf("\n");
    for(i=0;i<size;i++)
    {
        printf("%2x",content[i]);
    }
    printf("\n");
}

這和我想要的一樣好。 然后我確定問題不是出自bitStuffing

謝謝大家

現在,我要拍打我的朋友

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM