我正在做一个作业,我们应该对使用不良加密算法加密过的PDF进行加密分析。
教授提供的代码将使用创建加密文件fd=open(filename, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR)。
在尝试解密的代码中,我使用打开了该文件fd_in=open(file, O_RDONLY)。
问题是,当我尝试从文件中读取时,出现“错误文件描述符”错误。我使用stat尝试获取有关已打开文件的文件描述符“思想”的更多信息,当文件描述符实际上为几百KB时,它表明文件的长度为0。
我正在使用的调试代码是:
if (0 > (len = read(fd_in, (char*)&read_buff, BITE))) { // BITE is defined as 8
printf("Error occured grabbing first bite of %s.\n", file);
printf("%s.\n", strerror(errno));
struct stat fileStat;
int stat = fstat(fd_in, &fileStat);
printf("fstat returned: %d.\n", stat); // Consistently printing 0
printf("Information for %s\n",file);
printf("---------------------------\n");
printf("File Size: \t\t%d bytes\n",fileStat.st_size);
printf("Number of Links: \t%d\n",fileStat.st_nlink);
printf("File inode: \t\t%d\n",fileStat.st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
return 1;
Run Code Online (Sandbox Code Playgroud)
}
我得到的结果是:
Error occured grabbing first bite of enc.pdf.
Bad file descriptor.
Information for enc.pdf
---------------------------
File Size: 0 bytes
Number of Links: 1
File inode: 16441996
File Permissions: -rw-------
Run Code Online (Sandbox Code Playgroud)
ls报告文件为
-rw------- 1 matt matt 157887 Oct 29 03:01 enc.pdf
Run Code Online (Sandbox Code Playgroud)
与打开文件有关的代码:
int fd_in=open(file, O_RDONLY);
if(fd_in<0) {
printf("Failed to open the input file %s.\n", file);
return 1;
} else {
printf("File open, descriptor is: %d.\n", fd_in);
}
Run Code Online (Sandbox Code Playgroud)
这一直为字段描述符打印出值3。
关于read_buff有一些问题。加密/解密过程涉及对读取的值进行XOR。由于这个原因,缓冲区被声明为a unsigned long long,为了读入缓冲区,我将其地址转换为(char *)。这种策略直接来自教授用于创建加密文件的代码。
我什至添加了一个带有printf的else来验证文件描述符是否有效。目前看来,3这始终是一致的-1
您可能想检查堆栈在调用open()和之间是否损坏read(),以便文件描述符的值fd_in发生更改。
| 归档时间: |
|
| 查看次数: |
10896 次 |
| 最近记录: |