One time pad encryption

One time pad encryption using xor is the only algorithm known to be completely uncrackable, because a key can be made which decrypts the message to any message the would-be cracker wants. This program implements the algorithm in a fast and crude way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*this program copyright Oren Watson. Do whatever you want with it.
This code has no warranty at all, ever.*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define DEFAULT_KEYLEN 10

int memxor(char *a, char *b, int len){
	int i;
	for(i=0;i<len;i++)a[i]^=b[i];
}

int main(int argc, char **argv){
	int keylen=DEFAULT_KEYLEN;
	if(argc < 2){
		fputs("You have to give a key file!\n",stderr);
		return 1;
	}
	if(argc >= 3)sscanf(argv[2],"%d",&keylen);
	else fputs("Taking default key length of 10\n",stderr);
	char *key=malloc(keylen);
	FILE *keyf=fopen(argv[1],"r");
	fread(key,1,keylen,keyf);
	fclose(keyf);
	int i=keylen;
	char *buf=malloc(keylen);
	int j=0;
	while(i==keylen){
		i=fread(buf,1,keylen,stdin);
		memxor(buf,key,i);
		fwrite(buf,1,i,stdout);
		if(j++==1 && i) fputs("Be advised that this encryption is only "
		"completely secure if the key is at least as long as the text.\n",stderr);
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.