Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C - Hide encrypted messages inside files

Name: Anonymous 2007-03-14 7:47 ID:Mzg2kEAL


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

#define ENCRYPT 0
#define DECRYPT 1
#define UNKOWN 2

crypt(char *p, int KEY) {
    while(*p)*p++=~*p^KEY; // uber huber encryption
}

int main(int argc, char **argv) {
   
    short int mode, pos = 1;
    FILE *fp;
    char *buffer, temp[50];
    long filesize;
    int msglen, cryptkey;
   
    switch(argc) {
        case 2:
            mode = DECRYPT;
        break;
       
        case 3:
            if(!strcmp(argv[1], "-e")) mode = ENCRYPT;
            else if(!strcmp(argv[1], "-d")) mode = DECRYPT;
            else mode = UNKOWN;
        break;
           
        default:
            fprintf(stderr, "Usage: %s -[option] file\n\nOptions:"
        "\n\t-e -- encrypt\n\t-d -- decrypt\n\tdefault is decrypt\n\n", argv[0]);
            return 0;
    }
    if(!(fp = fopen(argv[argc-1], mode==DECRYPT?"rb":"a+"))) {
        perror("fopen");
        return 1;
    }
    switch(mode) {
   
    case DECRYPT:
   
        memset(temp, 0, 50);
        fseek(fp, 0, SEEK_END);
        filesize = ftell(fp);
        while(temp[0] != '.') {
            ++pos;
            fseek(fp, filesize-pos, SEEK_SET);
            fread(temp, sizeof(char), 1, fp);
            if(pos > 5) {
                fprintf(stderr, "%s does not contain any encrypted message.\n", argv[argc-1]);
                fclose(fp);
                return 0;
            }
        }
        fread(temp, sizeof(char), pos-1, fp);
        cryptkey = atoi(temp);
        ++pos;
        fseek(fp, filesize-pos, SEEK_SET);
        fread(temp, sizeof(char), 1, fp);
        while(temp[0] != '.') {
            ++pos;
            fseek(fp, filesize-pos, SEEK_SET);
            fread(temp, sizeof(char), 1, fp);
        }
        fread(temp, sizeof(char), pos-2, fp);
        msglen = atoi(temp);
        if(!(buffer = (char*)malloc(msglen+1))) {
            perror("malloc");
            fclose(fp);
            return 2;
        }
       
        fseek(fp, -msglen-(pos-1), SEEK_CUR);
        fread(buffer, sizeof(char), msglen, fp);
        crypt(buffer, cryptkey);
        printf("key: %d\nlength: %d\nmessage: \n%s\n", cryptkey, msglen, buffer);
       
    break;
   
    case ENCRYPT:
       
        puts("Enter the message you want to hide and press ^D\n");
        buffer = (char*)malloc(1);
        msglen = 0;
        while(buffer = (char*)realloc(buffer, msglen+1)) {
            fread(&buffer[msglen], sizeof(char), 1, stdin);
            if(!buffer[msglen]) break;
            ++msglen;
        }
        puts("Enter the key that should be used (between 1-255)");
        scanf("%[0-9]", temp);
        cryptkey=atoi(temp);
        if(cryptkey > 255) {
            fprintf(stderr, "Hey! i said between 1 and 255 >:|\n");
            free(buffer);
            fclose(fp);
            return 4;
        }
        crypt(buffer, cryptkey);
        fprintf(fp, "%s.%d.%d", buffer, msglen, cryptkey);
       
    break;
   
    case UNKOWN:
        fprintf(stderr, "Unkown option \"%s\"\n", argv[1]);
        return 0;
    }
   
    free(buffer);
    fclose(fp);
    return 0;
}

Tell me what you think

Name: Anonymous 2007-03-14 20:19 ID:Heaven

How do i use AES in C ?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List