Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (3)

Powered by Vanilla. Made with Bootstrap.
[C] Simple File Crypt
  • GT3X
    Posts: 20


    #include <stdio.h>

    #define ENCRYPTION (int)Byte + 30
    #define DECRYPTION (int)Byte - 30

    int Encrypt(char *file, char* newfile)
    {
    FILE *in, *out;

    char Byte, newByte;

    in = fopen(file, \"rb\");
    out = fopen(newfile, \"w\");

    if(in == NULL || out == NULL)
    {
    printf(\"error in\");
    }
    else
    {
    printf(\"File opend, Encrypting...\");
    while(1)
    {
    printf(\".\");

    if(Byte != EOF)
    {
    Byte=fgetc(in);
    newByte=Byte+30;
    fputc(newByte, out );
    }
    else
    {
    printf(\"End of File\");
    break;
    }
    }
    fclose(in);
    fclose(out);
    }

    }

    int Decrypt(char *file, char *newfile)
    {
    FILE *in,*out;

    char Byte, newByte;

    in = fopen(file, \"rb\");
    out = fopen(newfile, \"w\");

    if(in == NULL || out == NULL)
    {
    printf(\"Erro\");
    }
    else
    {
    printf(\"File Opened, Decrypting\");
    while(1)
    {
    printf(\".\");
    if(Byte!=EOF)
    {
    Byte = fgetc(in);
    newByte = Byte-30;
    }
    else
    {
    printf(\"End of file\");
    break;
    }
    }
    fclose(in);
    fclose(out);
    }
  • Xin
    Posts: 3,251
    Nice share you should comment your code for people that dont know C
    Xin
  • Mr. P-teoMr. P-teo
    Posts: 269
    Learning C at the min and would help alot if this was commented, nice share though
    Skype: mrpt3o
    Twitter: MrPteo


    image
  • Praxis
    Posts: 20
    said:


    Learning C at the min and would help alot if this was commented, nice share though



    Same goes for me, I'd really appreciate it if you commented your code so I can follow it a bit better :)
  • Deque
    Posts: 78
    I believe you forgot a curly brace at the end.

    You didn't provide a main to try this out fast. Make it a bit more convenient for others to use your code. I don't mind missing comments as long as the code is readable. One part of readability is to be consistent. I.e. consistency with naming variables and functions: Sometimes you start with uppercase letters, sometimes you don't.
    Consistency with whitespaces: Sometimes you have blanks before and after operators (==, !=, +, -) and assignments (=), sometimes you don't. You really should do something about that.

    #define ENCRYPTION (int)Byte + 30
    #define DECRYPTION (int)Byte - 30


    Why do you define this, but don't use it (apart from that I wouldn't use it, it has no advantage in this case). Delete those lines.

    if(in == NULL || out == NULL)
    {
    printf(\"Erro\");
    }


    Error messages should be printed to the error stream.

    I wonder if you ever ran your Decrypt function, because it only creates an empty file. You forgot the fputc.

    int Encrypt(char *file, char* newfile)
    int Encrypt(char *file, char* newfile)


    You defined return type int for both functions, but you never return anything. If you use gcc as compiler you should compile with -Wall option. That will tell you such things.

    Same goes for me, I'd really appreciate it if you commented your code so I can follow it a bit better :)



    Rather learn from someone who can actually code.
  • sangf
    Posts: 203
    said:


    You don't close your streams in the if-case.


    is it necessary to fclose if fopen returns NULL (ie. because of an error like files not existing)? i can't see the sense, but i suppose it won't hurt.
  • Deque
    Posts: 78
    said:


    said:


    You don't close your streams in the if-case.


    is it necessary to fclose if fopen returns NULL (ie. because of an error like files not existing)? i can't see the sense, but i suppose it won't hurt.

    You are right it isn't necessary. Thank you. Edit: I corrected my post.
  • Sh3llc0d3
    Posts: 1,910
    It annoys me when users post code that people are supposed to learn from but hasn't been checked or just isn't up to scratch.