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 (1)

Powered by Vanilla. Made with Bootstrap.
xor encoder
  • s1n4
    Posts: 88
    Hi guys,
    A simple xor encoder in C for windows.

    /*
    ___ ______________
    \ \/ / _ \_ __ \
    > < <_> ) | \/
    /__/\_ \____/|__|
    \/
    */


    #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>

    void SetColor(const int, const int);

    void err(unsigned const int);

    void main()
    {
    unsigned int c = 0;
    char i, res;
    char key[128], fn[128];

    const char head[] = (
    \"##################################\n\"
    \" ##### #####\n\"
    \" ##### Xor encoder #####\n\"
    \" ##### for encoding #####\n\"
    \" ##### text files #####\n\"
    \" ##### #####\n\"
    \" ##### Writen by s1n4 #####\n\"
    \" ##### June 17 , 2011 #####\n\"
    \" ##### 4:10 AM #####\n\"
    \" ##### #####\n\"
    \"##################################\n\n\");


    FILE *input, *output;

    SetColor(11, 0);
    printf(head);

    SetColor(7, 0);

    printf(\"Please enter key >> \");
    gets(key);

    printf(\"Please enter filename >> \");
    gets(fn);

    if ((input = fopen(fn, \"rb\")) == NULL)
    {
    err(1);
    }

    i = getc(input);

    if ((output = fopen(fn, \"wb\")) == NULL)
    {
    err(2);
    }

    while (i != EOF)
    {
    res = i ^ key[c];
    putc(res, output);
    i = getc(input);
    if (c == strlen(key)-1)
    {
    c = 0;
    continue;
    }
    c++;
    }

    fclose(output);
    fclose(input);

    SetColor(10, 0);

    printf(key);
    printf(\" xor \");
    printf(fn);

    getch();
    exit(0);
    }


    void SetColor(const int foreground, const int background)
    {
    int Color = foreground + (background * 16);

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, Color);
    }


    void err(unsigned const int code)
    {
    SetColor(12, 0);
    if (code == 1)
    printf(\"Cannot open the file\");

    if (code == 2)
    printf(\"Cannot replace the file\");

    getch();
    exit(1);
    }


    If you want to use in other OS you should remove the following code:

    #include <windows.h>
    void SetColor(const int, const int);

    SetColor(11, 0);
    SetColor(7, 0);
    SetColor(10, 0);

    void SetColor(const int foreground, const int background)
    {
    int Color = foreground + (background * 16);

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, Color);
    }

    SetColor(12, 0);
  • nu11byte
    Posts: 53
    Nice code you've got there. I had a little play with it and it seems really good.
  • Sh3llc0d3
    Posts: 1,910
    I'd not noticed this before, good job sin4! I'll have a mess about with this later :)