Created
January 22, 2014 11:54
-
-
Save tuxillo/8557466 to your computer and use it in GitHub Desktop.
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> | |
#include <err.h> | |
#include <unistd.h> | |
#define MAXMSGLEN 1024 | |
char ktext[MAXMSGLEN] = {0}; | |
void | |
genkey(void) | |
{ | |
FILE *r; | |
size_t rbytes; | |
if ((r = fopen("/dev/random", "r")) == NULL) | |
err(1, "Failed to open random(4)"); | |
rbytes = fread(ktext, MAXMSGLEN, 1, r); | |
if (rbytes != 1) | |
err(1, "Failed to generate the key"); | |
fclose(r); | |
} | |
void | |
enc(const char *msg, char *ctext, size_t len) | |
{ | |
int i; | |
for (i = 0; i < len; i++) | |
ctext[i] = msg[i] ^ ktext[i]; | |
} | |
void | |
dec(const char *ctext, char *msg, size_t len) | |
{ | |
int i; | |
for (i = 0; i < len; i++) | |
msg[i] = ctext[i] ^ ktext[i]; | |
} | |
void | |
usage(const char *progname) | |
{ | |
fprintf(stdout, "usage: %s [-edh]\n", progname); | |
} | |
void | |
print_hexa(char *label, char *buf, int len) | |
{ | |
int i; | |
printf("%s : ", label); | |
for (i = 0; i < len; i++) | |
printf("%.2hhx", buf[i]); | |
printf("\n"); | |
} | |
void | |
hex2char(char *inbuf, char *outbuf, int len) | |
{ | |
int i, j; | |
char pair[2]; | |
for (i = 0, j = 0; i < len; i += 2, j++) { | |
pair[0] = inbuf[i]; | |
pair[1] = inbuf[i+1]; | |
outbuf[j] = (char)strtol(pair, NULL, 16); | |
} | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
char c; | |
char buf[MAXMSGLEN] = {0}; | |
char cbuf[MAXMSGLEN] = {0}; | |
char t1buf[MAXMSGLEN] = {0}; | |
char t2buf[MAXMSGLEN] = {0}; | |
int opt_encryt = 0; | |
int opt_decrypt = 0; | |
int opt_hexa = 0; | |
while ((c = getopt(argc, argv, "edh")) != -1) { | |
switch(c) { | |
case 'e': | |
opt_encryt = 1; | |
break; | |
case 'd': | |
opt_decrypt = 1; | |
break; | |
case 'h': | |
opt_hexa = 1; | |
break; | |
default: | |
usage(argv[0]); | |
} | |
} | |
if (opt_encryt && opt_decrypt) | |
errx(1, "You can't encrypt and decrypt at once"); | |
if (opt_encryt) { | |
genkey(); | |
printf("Message to encrypt:\n"); | |
fgets(buf, MAXMSGLEN, stdin); /* Get the msg to encrypt */ | |
enc(buf, cbuf, strlen(buf)); | |
if (opt_hexa) { | |
print_hexa("Key generated", ktext, strlen(buf)); | |
print_hexa("Encrypted message", cbuf, strlen(buf)); | |
} | |
} else if (opt_decrypt) { | |
printf("Enter key:\n"); | |
fgets(t1buf, MAXMSGLEN, stdin); | |
printf("Enter ciphertext:\n"); | |
fgets(t2buf, MAXMSGLEN, stdin); | |
hex2char(t1buf, ktext, MAXMSGLEN); | |
hex2char(t2buf, cbuf, MAXMSGLEN); | |
dec(cbuf, buf, strlen(ktext)); | |
fprintf(stdout, "Decrypted: %s\n", buf); | |
if (opt_hexa) { | |
print_hexa("Key input", ktext, strlen(buf)); | |
print_hexa("Encrypted message", cbuf, strlen(buf)); | |
} | |
} else { | |
usage(argv[0]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment