1 /** Practical One-time Pad Library
25 load_config("otp.conf");
29 printf("offset=%ld\n", read_offset(pads));
30 write_offset(pads, 1213475);
31 printf("offset=%ld\n", read_offset(pads));
34 otp_decrypt("--EMOTP_BEGIN--1213434,\n"
35 "gK1O22FPbxLmxrROfFHDCsM1LTsOAjjbRlHVM1p+WG+s6yslYVfzvtc=\n"
36 "--EMOTP_END--\n", &o
);
40 otp_decrypt("--EMOTP_BEGIN--978,dc,\n"
41 "hUZm1q0gX7pa6Alzbo9OZiT8wA==\n"
42 "--EMOTP_END--\n", &o
);
46 o
= otp_encrypt("hello world", strlen("hello world"), "dc", &i
);
47 printf("encrypt = %s\n", o
);
50 otp_decrypt("junkasdsjdfldjf jdsfjunk--EMOTP_BEGIN--1213475,dc,\n"
52 "--EMOTP_END--\n-tasjdktrailingjunkksjdjf", &o
);
67 fprintf(stderr
, "usage: otp [-e | -d | ] [-t <pad_name>]\n"
72 "-r Replace what an existing encrypted message decrypts to\n"
73 "Default is automatic.\n"
75 "-t Specify pad name to encrypt with. Default: first pad.\n");
81 * @param size Set to number of bytes read.
83 * @return Input, caller frees.
85 char *read_input(unsigned int *size
)
87 unsigned int i
, buffer_size
;
92 input
= malloc(buffer_size
);
94 perror("malloc input buffer");
99 input
[i
++] = fgetc(stdin
);
100 if (input
[i
- 1] == EOF
)
102 if (i
>= buffer_size
) {
104 input
= realloc(input
, buffer_size
);
106 perror("realloc input buffer");
107 exit(EX_UNAVAILABLE
);
117 int main(int argc
, char **argv
)
120 unsigned int length
, output_length
;
121 char *to
, *input
, *output
;
126 while((ch
= getopt(argc
, argv
, "erdt:")) != -1) {
149 /* Automatic mode - guess based on magic markers.
150 * Explicit modes still allowed so can encrypt data with
151 * magic markers in it (if you ever want to). */
153 if (strstr(input
, MARKER_BEGIN
))
159 load_config("otp.conf");
161 input
= read_input(&length
);
163 if (mode
== ENCRYPT
) {
164 output
= otp_encrypt(input
, length
, to
, &output_length
);
165 } else if (mode
== DECRYPT
) {
166 output_length
= otp_decrypt(input
, &output
);
167 } else if (mode
== REPLACE
) {
168 if (!strstr(input
, MARKER_END
)) {
169 fprintf(stderr
, "need encrypted pad, trailed by new message\n");
173 otp_replace(input
, strstr(input
, MARKER_END
) + strlen(MARKER_END
) + 1);
177 for (i
= 0; i
< output_length
; ++i
)