Question

I'm trying to get the basics of some weak encryption but I'm at a wall....I need a function to call that will decrypt the encrypted file. But my main question is how do i go about decrypting this file? Please be nice I'm new to this! =]
Also, the w.pdf file starts as a regular pdf inside the project folder, and I do have error checking in the program just excluded it here for the sake of saving space.

#include <stdio.h>

int main()
{
    FILE *ptr1,*file,*ptr3;
    unsigned long key;
    int B,C;

    ptr1=fopen("w.pdf","rb");
    file=fopen("encryptFile.pdf","wb");

    printf("Enter the encrypt key: ");
    scanf("%ld",&key);

    //Transfer contents
    while((B=fgetc(ptr1))!=EOF)
        fputc(C=B^(key & 0xff), file);
    key=key^C;
    puts("\nencryptFile.pdf has finished.");

    rewind(file);

    ptr3=fopen("new.pdf","wb");

    //pdf file closing.
    fclose(ptr1);
    fclose(file);
    fclose(ptr3);

    return 0;
}
Was it helpful?

Solution

No worries, everyone starts somewhere. (But you should probably start with Google, wikipedia, or a lesson rather than starting out asking on StackOverflow)

Your encryption scheme is to xor the content with a key the user entered.

To decrypt the content, you'd have to reverse the encryption, which in this case is just xoring the content with the key again.

Some other helpful advice:

  • Indent your code, it'll go a long way towards making it readable

  • Name your variables better. ptr1 could be encryptionTarget, clearText, or f_pdfFile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top