Download code

Jump to: navigation, search

Back to Simple_XOR_cipher_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 /tmp/litprog4983476/xorcipher.c: In function 'main':
2 /tmp/litprog4983476/xorcipher.c:72:2: warning: implicit declaration of function 'getpass' [-Wimplicit-function-declaration]


xorcipher.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Simple_XOR_cipher_(C)?oldid=18706
14 */
15 
16 #include<stdio.h>
17 #include<stdlib.h>
18 #include<string.h>
19 #include<unistd.h>
20 
21 void usage(const char *cmd)
22 {
23 	fprintf(stderr, "Usage: %s <in-file> [<out-file>]\n", cmd);
24 
25 	exit(1);
26 }
27 
28 void xorcipher(const unsigned char *key, FILE *infp, FILE *outfp)
29 {
30 	int ch;
31 	const unsigned char *keyp=key;
32 
33 	while((ch=getc(infp))>=0) {
34 		putc(ch^*keyp++, outfp);
35 		if(!*keyp) keyp=key;
36 	}
37 }
38 
39 
40 
41 int main(int argc, char *argv[])
42 {
43 	char *infname;
44 	char *outfname;
45 	const unsigned char *key;
46 	FILE *infp, *outfp;
47 
48 	if(argc>1) infname=argv[1];
49 	else usage(argv[0]);
50 
51 	if(!(infp=fopen(infname, "rb"))) {
52 		fprintf(stderr, "ERROR: fopen(%s)\n", argv[1]);
53 		exit(1);
54 	}
55 
56 	if(argc>2) outfname=argv[2];
57 	else {
58 		if(!(outfname=malloc(strlen(infname)+5))) {
59 			fprintf(stderr, "ERROR: malloc failed\n");
60 			exit(1);
61 		}
62 		strcpy(outfname, argv[1]);
63 		strcat(outfname, ".xor");
64 
65 	}
66 
67 	if(!(outfp=fopen(outfname, "wb"))) {
68 		fprintf(stderr, "ERROR: fopen(%s)\n", outfname);
69 		exit(1);
70 	}
71 
72 	key=(const unsigned char*)getpass("Please enter a key: ");
73 
74 	xorcipher(key, infp, outfp);
75 
76 	fclose(outfp);
77 	fclose(infp);
78 
79 	if(argc<3) free(outfname);
80 
81 	return 0;
82 }
83 


hijacker
hijacker
hijacker
hijacker