xor encoder in C

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);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s