2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This code was based on:
9 * PC1 Cipher Algorithm ( Pukall Cipher 1 )
10 * By Alexander PUKALL 1991
11 * free code no restriction to use
12 * please include the name of the Author in the final software
14 * http://membres.lycos.fr/pc1/
22 #include <unistd.h> /* for unlink() */
24 #include <getopt.h> /* for getopt() */
37 unsigned short x1a0
[8];
43 unsigned short compte
;
44 unsigned char cle
[17];
48 static void pc1_finish(struct pc1_ctx
*pc1
)
50 /* erase all variables */
51 memset(pc1
, 0, sizeof(struct pc1_ctx
));
54 static void pc1_code(struct pc1_ctx
*pc1
)
56 pc1
->dx
= pc1
->x1a2
+ pc1
->i
;
57 pc1
->ax
= pc1
->x1a0
[pc1
->i
];
70 pc1
->ax
= pc1
->ax
* pc1
->bx
;
78 pc1
->ax
= pc1
->ax
* pc1
->si
;
79 pc1
->cx
= pc1
->ax
+ pc1
->cx
;
85 pc1
->ax
= pc1
->ax
* pc1
->bx
;
86 pc1
->dx
= pc1
->cx
+ pc1
->dx
;
88 pc1
->ax
= pc1
->ax
+ 1;
91 pc1
->x1a0
[pc1
->i
] = pc1
->ax
;
93 pc1
->res
= pc1
->ax
^ pc1
->dx
;
97 static void pc1_assemble(struct pc1_ctx
*pc1
)
99 pc1
->x1a0
[0] = (pc1
->cle
[0] * 256) + pc1
->cle
[1];
102 pc1
->inter
= pc1
->res
;
104 pc1
->x1a0
[1] = pc1
->x1a0
[0] ^ ((pc1
->cle
[2]*256) + pc1
->cle
[3]);
106 pc1
->inter
= pc1
->inter
^ pc1
->res
;
108 pc1
->x1a0
[2] = pc1
->x1a0
[1] ^ ((pc1
->cle
[4]*256) + pc1
->cle
[5]);
110 pc1
->inter
= pc1
->inter
^ pc1
->res
;
112 pc1
->x1a0
[3] = pc1
->x1a0
[2] ^ ((pc1
->cle
[6]*256) + pc1
->cle
[7]);
114 pc1
->inter
= pc1
->inter
^ pc1
->res
;
116 pc1
->x1a0
[4] = pc1
->x1a0
[3] ^ ((pc1
->cle
[8]*256) + pc1
->cle
[9]);
118 pc1
->inter
= pc1
->inter
^ pc1
->res
;
120 pc1
->x1a0
[5] = pc1
->x1a0
[4] ^ ((pc1
->cle
[10]*256) + pc1
->cle
[11]);
122 pc1
->inter
= pc1
->inter
^ pc1
->res
;
124 pc1
->x1a0
[6] = pc1
->x1a0
[5] ^ ((pc1
->cle
[12]*256) + pc1
->cle
[13]);
126 pc1
->inter
= pc1
->inter
^ pc1
->res
;
128 pc1
->x1a0
[7] = pc1
->x1a0
[6] ^ ((pc1
->cle
[14]*256) + pc1
->cle
[15]);
130 pc1
->inter
= pc1
->inter
^ pc1
->res
;
135 static unsigned char pc1_decrypt(struct pc1_ctx
*pc1
, short c
)
138 pc1
->cfc
= pc1
->inter
>> 8;
139 pc1
->cfd
= pc1
->inter
& 255; /* cfc^cfd = random byte */
141 c
= c
^ (pc1
->cfc
^ pc1
->cfd
);
142 for (pc1
->compte
= 0; pc1
->compte
<= 15; pc1
->compte
++) {
143 /* we mix the plaintext byte with the key */
144 pc1
->cle
[pc1
->compte
] = pc1
->cle
[pc1
->compte
] ^ c
;
150 static unsigned char pc1_encrypt(struct pc1_ctx
*pc1
, short c
)
153 pc1
->cfc
= pc1
->inter
>> 8;
154 pc1
->cfd
= pc1
->inter
& 255; /* cfc^cfd = random byte */
156 for (pc1
->compte
= 0; pc1
->compte
<= 15; pc1
->compte
++) {
157 /* we mix the plaintext byte with the key */
158 pc1
->cle
[pc1
->compte
] = pc1
->cle
[pc1
->compte
] ^ c
;
160 c
= c
^ (pc1
->cfc
^ pc1
->cfd
);
165 static void pc1_init(struct pc1_ctx
*pc1
)
167 memset(pc1
, 0, sizeof(struct pc1_ctx
));
169 /* ('Remsaalps!123456') is the key used, you can change it */
170 strcpy(pc1
->cle
, "Remsaalps!123456");
173 static void pc1_decrypt_buf(struct pc1_ctx
*pc1
, unsigned char *buf
,
178 for (i
= 0; i
< len
; i
++)
179 buf
[i
] = pc1_decrypt(pc1
, buf
[i
]);
182 static void pc1_encrypt_buf(struct pc1_ctx
*pc1
, unsigned char *buf
,
187 for (i
= 0; i
< len
; i
++)
188 buf
[i
] = pc1_encrypt(pc1
, buf
[i
]);
195 static char *progname
;
202 #define ERR(fmt, ...) do { \
204 fprintf(stderr, "[%s] *** error: " fmt "\n", \
205 progname, ## __VA_ARGS__ ); \
208 #define ERRS(fmt, ...) do { \
211 fprintf(stderr, "[%s] *** error: " fmt "\n", \
212 progname, ## __VA_ARGS__, strerror(save)); \
215 void usage(int status
)
217 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
218 struct board_info
*board
;
220 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
224 " -d decrypt instead of encrypt"
225 " -i <file> read input from the file <file>\n"
226 " -o <file> write output to the file <file>\n"
227 " -h show this screen\n"
233 #define BUFSIZE (64 * 1024)
235 int main(int argc
, char *argv
[])
238 int res
= EXIT_FAILURE
;
244 FILE *outfile
, *infile
;
246 progname
= basename(argv
[0]);
251 c
= getopt(argc
, argv
, "di:o:h");
274 if (ifname
== NULL
) {
275 ERR("no input file specified");
279 if (ofname
== NULL
) {
280 ERR("no output file specified");
284 err
= stat(ifname
, &st
);
286 ERRS("stat failed on %s", ifname
);
291 buf
= malloc(BUFSIZE
);
293 ERR("no memory for buffer\n");
297 infile
= fopen(ifname
, "r");
298 if (infile
== NULL
) {
299 ERRS("could not open \"%s\" for reading", ifname
);
303 outfile
= fopen(ofname
, "w");
304 if (outfile
== NULL
) {
305 ERRS("could not open \"%s\" for writing", ofname
);
319 fread(buf
, datalen
, 1, infile
);
321 ERRS("unable to read from file %s", ifname
);
326 pc1_decrypt_buf(&pc1
, buf
, datalen
);
328 pc1_encrypt_buf(&pc1
, buf
, datalen
);
331 fwrite(buf
, datalen
, 1, outfile
);
333 ERRS("unable to write to file %s", ofname
);
348 if (res
!= EXIT_SUCCESS
) {
This page took 0.074943 seconds and 5 git commands to generate.