解密十六进制时如何输出文本?

时间:2015-02-26 13:12:42

标签: c encryption

我正在尝试解密十六进制字符串:0440 04b4 04d0 04d0 04dc 03a0 047c 04dc 04e8 04d0 04b0 03a4 它应该一次解密正确阅读你好世界! 但是我无法输出文本。相反,我得到一个整数列表。有没有人对如何解决这个问题有任何想法?

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


int main (int argc, char **argv)

{
    char file_name[25];
    char c; //Variable to hold current character
    int sz; //Length of file therefore size of array
    FILE *fp;
    FILE *fw;
    int flag = 1;
    while (flag)
    {
        printf("enter the name of the file you wish to decrypt\n");
        fgets(file_name, sizeof file_name, stdin); //fgets safer than gets, prevents infinite input
        printf("Reading: %s.....\n", file_name); //debug print, can remove

        strtok(file_name, "\n"); //fgets potentially leaves \n on file name, this removes

        fp=fopen(file_name,"rt"); //file is opened
        fw = fopen("revertedfile.txt", "w"); //oens write file

        if( fp == NULL ) //checks if file empty/doesn't exist
        {
            perror("Error while opening the file.\n");
            exit(EXIT_FAILURE);
        }
        //Following code finds length of file by seeking, then resets the file point to the start
        fseek(fp, 0L, SEEK_END);
        sz = ftell(fp);
        printf("Length of file is: %i\n",sz); // Debug print, can delete
        fseek(fp, 0L, SEEK_SET);


        int data[sz]; //Init array based on file length
        int i = 0; //Counter set to 0

        while((c=fgetc(fp))!=EOF)  //While haven't reached end of file.
        {
            int ic = (int)c - 200; //minus 200 to the ascii code

            //printf("ASCI Code + 100 = %i\n", ic); //debug print

            //Could do bit shift before adding to array, I Guess
            data[i] = ic; //Add ASCII code to array at i
            data[i] = data[i] >> 2; //Bit shift

            //Debug print, shows what's happening
            int test = data[i];
            printf("%i\n", data[i]);
            fprintf(fw, "%i\n", data[i]);
            //printf("Added %i to the array at point %d\n", test, i);

            i++; //Increment pointer for array
        }
        fclose(fp); //Always close file
        fclose(fw); // closes write file
    }
    return 0;
}

这是加密代码

printf("enter name of the file you wish to encrypt\n");
fgets(file_name, sizeof file_name, stdin); //fgets safer than gets, prevents infinite input
printf("Reading: %s.....\n", file_name); //debug print, can remove

strtok(file_name, "\n"); //fgets potentially leaves \n on file name, this removes

fp = fopen(file_name,"rt"); //file is opened
fw = fopen("output.txt", "w"); //oens write file

if( fp == NULL ) //checks if file empty/doesn't exist
{
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
//Following code finds length of file by seeking, then resets the file point to the start
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
printf("Length of file is: %i\n",sz); // Debug print, can delete
fseek(fp, 0L, SEEK_SET);


int data[sz]; //Init array based on file length
int i = 0; //Counter set to 0

while((c=fgetc(fp))!=EOF)  //While haven't reached end of file
{

    printf("Current char: %c\n",c); //Debug print

    int ic = (int)c + 200; //Add 200 to the ascii code

    //printf("ASCI Code + 100 = %i\n", ic); //debug print

    //Could do bit shift before adding to array, I Guess
    data[i] = ic; //Add ASCII code to array at i
    data[i] = data[i] << 2; //Bit shift

    //Debug print, shows what's happening
    int test = data[i];
    printf("%04x ", data[i]);
    fprintf(fw, "%04x ", data[i]);
    //printf("Added %i to the array at point %d\n", test, i);

    i++; //Increment pointer for array
}

fclose(fp); //Always close file
fclose(fw); // closes write file


//Debug
int test2 = data[1];
printf("The new file is named output.txt");

1 个答案:

答案 0 :(得分:1)

如果你想解密它,你需要知道它是如何加密的(嗯,这仍然可能还不够)。如果您只需要打印每个ASCII代码所代表的字符,您只需要使用printf代替%c告诉%i