简体   繁体   English

关于在c中解密图像文件的许多疑点

[英]Many doubts about decrypt an image file in c

Firstly, i'm not very familiarized with C, i come from Java, C#, C++... and possibly i inherited defects from this languages in order to realize this practice, well i have the follows question, here is my code: 首先,我不是很熟悉C,我来自Java,C#,C ++ ...并且我可能继承了这些语言的缺陷以实现这种做法,我有以下问题,这是我的代码:

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

void decrypt(unsigned long* v, unsigned long* k);

const int MAX = 32;
const long delta = 0x9e3779b9;
long sum=0xC6EF3720;

int main() {
  FILE *fp;
  FILE *destino;
  unsigned long v[2];
  unsigned long k[4] = { 128, 129, 130, 131 };
  unsigned long tam=0;
  char* buffer;
  char* aux[sizeof(unsigned long)];
  int i;

if ((fp = fopen("image.png", "rb")) == NULL) {
    printf ("Error! \n ");
    return 0;
}
else {
    fread(&aux,sizeof(unsigned long),1,fp);
    memcpy(&tam,&aux,sizeof(unsigned long));
    buffer = (char*)malloc(tam);
    //fread(&buffer,1,tam,fp);
    char *buffer2[28568];
    fread(&buffer2,1,28568,fp);
    /*for(i = 0;i < tam;++i) {
         printf("%c", ((char *)buffer2)[i]);
     }*/
    for(i=4;i<tam;i+=8) {
        memcpy(&v,&buffer2[i],8);
        decrypt(&v,&k);
    }
    if ((result= fopen("image2.png", "rb")) == NULL) {
        printf ("Error! \n ");
        return 0;
    }
    else {
        fwrite(v,sizeof(unsigned long)*2,1,result);
        fclose (result);
        fclose(fp);
    }
}
return 0;
}

void decrypt(unsigned long* v, unsigned long* k) {
int i=0;
while(i<MAX) {
    v[1] = v[1] -((4 << v[0])+(k[2]^v[0])+(sum^(5 >> v[0]))+k[3]);
    v[0] = v[0] -((4 << v[1])+(k[0]^v[1])+(sum^(5 >> v[1]))+k[1]);
    sum = sum-delta;
    i++;
   }

}

Where tam is the size of my binary file (image in this case) where i store first 4 bytes (unsigned long) where is located the size in my png file (28568) 其中tam是我的二进制文件的大小(在这种情况下为图像),其中我存储前4个字节(无符号长),其中的大小位于我的png文件中(28568)

When i create my char* buffer i have to assign dynamically with malloc but when i make a new fread from my file i get a "No source available for "msvrct!memcpy() at 0xrandom_memory_address" from Eclipse when i debug, well, i comment this line and i try to make it manually set a new buffer2 with 28568 as size of my array, apparently works, making a iteration of buffer2 prints ascii characters values but when i call decrypt for make the decryption of my image, the final result is stored in v array which i have to copy in a new file, i tried to search how to make a empty image png in C but i didn't find anything, so i created a copy of my encrypt image calling it "image2.png" but i suppose this not the "clean solution" for that, because for the other hand is not working at all. 当我创建我的char *缓冲区时,我必须动态分配malloc,但是当我从我的文件中创建一个新的fread时,我得到一个“没有可用的源代码”msvrct!memcpy()在0xrandom_memory_address“来自Eclipse,当我调试时,好吧,我注释这一行,我尝试手动设置一个新的buffer2与28568作为我的数组的大小,显然有效,使缓冲的buffer2打印ascii字符值,但当我调用decrypt进行解密我的图像,最后的结果存储在v数组中,我必须在一个新文件中复制,我试图搜索如何在C中制作一个空图像png但我没有找到任何东西,所以我创建了我的加密图像的副本,称之为“image2”。 png“但我认为这不是”干净的解决方案“,因为另一方面根本不起作用。

For more explanation about this exercise just say that the decrypt funcion work with blocks of 8 bytes (64 bits) that through a key (array k) make a series of operation where they store in v array itself, crossing through the loop 8 in 8 and retrieve the value of buffer in v in each one, after the loop execution we have the result in v and only left to copy in a new file where finally show up the image decrypt. 有关此练习的更多解释,只需说解密函数使用8字节(64位)的块通过键(数组k)进行一系列操作,它们存储在v数组本身中,穿过8中的循环8并在每个循环执行中检索v中缓冲区的值,循环执行后我们得到v中的结果,只留下复制到最终显示图像解密的新文件中。

It's a very complex practice for all of one newbies in C, it's driving my crazy trying to figure out what i doing wrong. 对于C中的所有新手来说,这是一个非常复杂的练习,它让我疯狂地试图弄清楚我做错了什么。

I hope anyone can see what i'm not able to for now. 我希望任何人都能看到我现在无法做到的。

I think you are having problems with the declarations of the buffers. 我认为你在缓冲区的声明方面遇到了问题。 I think the correct should be: 我认为正确的应该是:

FILE *fp;
  FILE *destino;
  unsigned long v[2];
  unsigned long k[4] = { 128, 129, 130, 131 };
  unsigned long tam=0;
  char* buffer;
  char aux[sizeof(unsigned long)]; // without the "*"
  int i;

if ((fp = fopen("image.png", "rb")) == NULL) {
    printf ("Error! \n ");
    return 0;
}
else {
    fread(aux,sizeof(unsigned long),1,fp);
    memcpy(&tam,aux,sizeof(unsigned long));
    buffer = (char*)malloc(tam);
    //fread(buffer,1,tam,fp); // without the "&" in this case
    char buffer2[28568]; // without the "*"
    fread(buffer2,1,28568,fp); // or fread(buffer,1,tam,fp);
    /*for(i = 0;i < tam;++i) {
         printf("%c", buffer2[i]); // or buufer[i] if you change to use it again
     }*/
    for(i=4;i<tam;i+=8) {
        memcpy(v,&buffer2[i],8);
        decrypt(v,k);
    }
    ...

I don't fully understand what you are trying to accomplish, but one problem is here: 我不完全明白你想要完成什么,但有一个问题在这里:

char* aux[sizeof(unsigned long)];
// ... some code ...
fread(&aux,sizeof(unsigned long),1,fp);

Understand that char* aux[sizeof(unsigned long)]; 理解char* aux[sizeof(unsigned long)]; means that you are declaring a double pointer, but fread() prototype states that the destination is a single pointer: 表示您正在声明一个双指针,但fread()原型声明目标是一个指针:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

so what you should be doing instead is: 所以你应该做的是:

char aux[sizeof(unsigned long)]; 
// ... some code ...
fread(aux,sizeof(unsigned long),1,fp);

Don't complicate things that are not complicated! 不要复杂的事情并不复杂!

You also do this mistake in other parts of your code, you need to re-check everything, ok? 你也在代码的其他部分犯了这个错误,你需要重新检查一切,好吗? Again: 再次:

char *buffer2[28568];
fread(&buffer2,1,28568,fp);

should be: 应该:

char buffer2[28568];
fread(buffer2, 1, 28568, fp);
// or: fread(buffer2, 1, sizeof(buffer2), fp);

There are some interesting tutorials on pointers and arrays , I suggest you read some. 关于指针和数组有一些有趣的教程,我建议你阅读一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM