gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:02:42 26 4
gpt4 key购买 nike

我正在尝试解密十六进制字符串: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/

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