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

标签 c encryption

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

#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");

最佳答案

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

关于c - 解密十六进制时如何输出文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743421/

相关文章:

c - 纯 winapi c GUI 更改静态文本背景

c++ - "Access violation reading location"从 directx 检索缓冲区时遇到问题

c - 使用链表的堆栈实现有什么问题?

encryption - winrt中加密数据的简单方法

database - SQLite - 创建加密数据库。怎么……?

c - strcpy 无法正确处理动态分配的 char 数组

c++ - 人为限制 C/C++ 内存使用

java - 在哪里可以找到 CTR 或 GCM 分组密码模式的 Java/Kotlin 实现?

java - 在 iText PDF Java 中验证签名和安全 PDF 的证书

c++ - 创建包含密码的安全配置文件