Created
February 14, 2013 01:02
-
-
Save loriopatrick/4949820 to your computer and use it in GitHub Desktop.
Something I created a long time ago while familiarizing myself with bitwise operators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void encrypt (char * msg, int msg_len, char * key, int key_len) { | |
int i, p; | |
char c; | |
for (i = 0; i < msg_len; i++) { | |
c = msg[i]; | |
p = key [i % key_len] % (msg_len - i) + i; | |
msg[i] = msg[p]; | |
msg[p] = c; | |
msg[i] ^= key[p % key_len]; | |
} | |
} | |
void decrypt (char * msg, int msg_len, char * key, int key_len) { | |
int i, p; | |
char c; | |
for (i = msg_len - 1; i > -1; --i) { | |
p = key [i % key_len] % (msg_len - i) + i; | |
msg[i] ^= key[p % key_len]; | |
c = msg[i]; | |
msg[i] = msg[p]; | |
msg[p] = c; | |
} | |
} | |
void print (char * message, int len) { | |
int i; | |
for (i = 0; i < len; i++) { | |
printf("%c", message[i]); | |
} | |
printf("\n"); | |
} | |
void encrypt_file (char * fname, char * key) { | |
FILE *f = fopen(fname, "rb"); | |
fseek(f, 0, SEEK_END); | |
long pos = ftell(f); | |
fseek(f, 0, SEEK_SET); | |
char *bytes = (char *)malloc(pos); | |
fread(bytes, pos, 1, f); | |
fclose(f); | |
encrypt(bytes, pos, key, strlen(key)); | |
FILE *f2 = fopen(fname, "wb"); | |
fwrite(bytes, pos, 1, f2); | |
fclose(f2); | |
free(bytes); | |
} | |
void decrypt_file (char * fname, char * key) { | |
FILE *f = fopen(fname, "rb"); | |
fseek(f, 0, SEEK_END); | |
long pos = ftell(f); | |
fseek(f, 0, SEEK_SET); | |
char *bytes = (char *)malloc(pos); | |
fread(bytes, pos, 1, f); | |
fclose(f); | |
decrypt(bytes, pos, key, strlen(key)); | |
FILE *f2 = fopen(fname, "wb"); | |
fwrite(bytes, pos, 1, f2); | |
fclose(f2); | |
free(bytes); | |
} | |
int main (int args, char ** argv) { | |
int i, type = -1, state = 0, pass = -1, file = -1; | |
for (i = 1; i < args; i++) { | |
if (!strcmp(argv[i], "-f")) { | |
state = 0; | |
} else if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "-k")) { | |
state = 1; | |
} else if (!strcmp(argv[i], "-e")) { | |
type = 0; | |
} else if (!strcmp(argv[i], "-d")) { | |
type = 1; | |
} else if (state == 0) { | |
file = i; | |
} else if (state == 1) { | |
pass = i; | |
} | |
} | |
if (file == -1) { | |
printf ("no file '-f'\n"); | |
return 1; | |
} | |
if (pass == -1) { | |
printf ("need encrypt '-p' or '-k'"); | |
return 1; | |
} | |
if (type == -1) { | |
printf("did not define type encrypt '-e' or decrypt '-d'"); | |
} | |
printf("file: %s\nkey: %s\n", argv[file], argv[pass]); | |
if (type == 0) { | |
encrypt_file(argv[file], argv[pass]); | |
} else if (type == 1) { | |
decrypt_file(argv[file], argv[pass]); | |
} | |
return 0; | |
decrypt_file("test.txt", "test"); | |
return 0; | |
char pwd[] = "password"; | |
char message[] = "this is a wonderful test that I have created"; | |
int len = strlen(message); | |
encrypt(message, len, pwd, strlen(pwd)); | |
decrypt(message, len, pwd, strlen(pwd)); | |
print(message, len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment