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

Pages: 1-

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 8:16 ID:1KLdrByp

use rc4. it's just a little bit more complicated but a lot more secure.

Name: Anonymous 2007-03-14 9:36 ID:IkJ8Jiok

Or just use AES256.

Name: Anonymous 2007-03-14 9:45 ID:Mzg2kEAL

>>3
isn't AES like md5 or DES ? (i mean it can only be bruteforced)
If not, can you please post a tutorial/library for it ?
As for rc4 i found some related articles on google and wikipedia and well it complicates things without them needed to be so complicated

Name: Anonymous 2007-03-14 9:48 ID:eP/Fxom3

no, it isn't

Name: Anonymous 2007-03-14 9:53 ID:IkJ8Jiok

>>4

Might want to do some research before you say something retarded. Oops, too late.

Name: Anonymous 2007-03-14 9:54 ID:uSilKFbC

crypt(char *p, int KEY) { ... }
This should have return type void?

*p++=~*p^KEY;
The behavior of this statement is undefined. You are both modifying p and making another read of p that is not used to determine p's new value between the same two sequence points.

If you're picky, there looks to be a bunch of potential problems in the I/O code: fread/scanf buffer overflow, etc.. In the decrypting part, instead of parsing the string yourself directly from the file, you could just read the first line of your file into a buffer and use sscanf.

Name: Anonymous 2007-03-14 9:55 ID:IkJ8Jiok

Name: Anonymous 2007-03-14 10:59 ID:Mzg2kEAL

Ok something new which works with a password and it's better in my oppinion

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

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

crypt(char *key, char *buffer, int keylen, int buflen) {
    int i;
    for(i=0;i<buflen;i++)
        buffer[i] = ~buffer[i]^key[i%keylen];
}

int main(int argc, char **argv) {
    int i;
    short int mode, pos = 1;
    FILE *fp;
    char *buffer, temp[50], _key[50];
    long filesize;
    int msglen, keylen;
   
    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);
        printf("Enter password for file:\n> ");
        scanf("%s%n", _key, &keylen);
        while(temp[0] != '.') {
            ++pos;
            fseek(fp, filesize-pos, SEEK_SET);
            fread(temp, sizeof(char), 1, fp);
        }
        fread(temp, sizeof(char), pos-1, fp);
        msglen = atoi(temp);
        if(!(buffer = (char*)malloc(msglen+1))) {
            perror("malloc");
            fclose(fp);
            return 2;
        }
        fseek(fp, -(msglen+pos), SEEK_CUR);
        fread(buffer, sizeof(char), msglen, fp);
        crypt(_key, buffer, keylen, msglen);
        printf("key: %s\nlength: %d\nmessage: %s\n", _key, 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;
        }
        printf("Enter a password (no spaces allowed)\n> ");
        scanf("%s", temp);
        crypt(temp, buffer, strlen(temp), msglen);
        fprintf(fp, "%s.%d", buffer, msglen);
       
    break;
   
    case UNKOWN:
        fprintf(stderr, "Unkown option \"%s\"\n", argv[1]);
        return 0;
    }
   
    free(buffer);
    fclose(fp);
    return 0;
}

I am going to look into AES now

Name: Anonymous 2007-03-14 11:54 ID:MKq+mgES

Use the key as the seed for a PRNG, then xor the data with the random numbers.

Name: Anonymous 2007-03-14 19:24 ID:YZW4G9vm

Use AES

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

How do i use AES in C ?

Name: Anonymous 2007-03-14 20:40 ID:YZW4G9vm

>>12
Google

Name: Anonymous 2009-01-14 12:42

LISP

Name: Anonymous 2009-01-14 12:44


#include <stdio.h>
#error "* g o a t s e x * g o a t s e x * g o a t s e x *"
#error "g                                               g  "
#error "o /     \             \            /    \       o"
#error "a|       |             \          |      |      a"
#error "t|       `.             |         |       :     t"
#error "s`        |             |        \|       |     s"
#error "e \       | /       /  \\\   --__ \\       :    e"
#error "x  \      \/   _--~~          ~--__| \     |    x  "
#error "*   \      \_-~                    ~-_\    |    *"
#error "g    \_     \        _.--------.______\|   |    g"
#error "o      \     \______// _ ___ _ (_(__>  \   |    o"
#error "a       \   .  C ___)  ______ (_(____>  |  /    a"
#error "t       /\ |   C ____)/      \ (_____>  |_/     t"
#error "s      / /\|   C_____)       |  (___>   /  \    s"
#error "e     |   (   _C_____)\______/  // _/ /     \   e"
#error "x     |    \  |__   \\_________// (__/       |  x"
#error "*    | \    \____)   `----   --'             |  *"
#error "g    |  \_          ___\       /_          _/ | g"
#error "o   |              /    |     |  \            | o"
#error "a   |             |    /       \  \           | a"
#error "t   |          / /    |         |  \           |t"
#error "s   |         / /      \__/\___/    |          |s"
#error "e  |           /        |    |       |         |e"
#error "x  |          |         |    |       |         |x"
#error "* g o a t s e x * g o a t s e x * g o a t s e x *"
int main() {
  return 0;
}

Name: Anonymous 2009-01-14 12:49

Name: Anonymous 2009-02-25 6:18

The default value of   colspan or even   just have a   cell span all   columns by default.

Name: Anonymous 2009-03-06 5:40

Short miserable life 4   Be reborn as   a cockroach and   live a short   miserable life 5   Be reborn as   a maggot and   live a short   explanation and example   of how they   may be implemented   since words allow   factoring at a   young age Just   like most priviledged.

Name: Anonymous 2009-03-06 13:33


The Tag datatype just that as it   sticks to an   observer not identity.

Name: Anonymous 2010-08-13 7:26

>>15
LOL YOU ARE MY HERO. THIS IS GOING STRAIGHT TO MY <void.h>
>>20
Way to bump a five year old thread, jerk.

Name: Anonymous 2010-08-13 10:41

>>21
Way to bump a five year old thread, jerk.

Name: Anonymous 2010-12-06 9:32

Back to /b/, ``GNAA Faggot''

Name: Sgt.Kabu⃗kimanꪄ긠 2012-05-28 23:13

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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