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
);
59 /* Operation mode - trinary :) */
66 fprintf(stderr
, "usage: otp [-e | -d | ] [-t <pad_name>]\n"
70 "-d Decrypt with <pad_name>\n"
71 "Default is automatic.\n"
73 "-t Specify pad name to encrypt with. Default: first pad.\n");
79 * @param size Set to number of bytes read.
81 * @return Input, caller frees.
83 char *read_input(unsigned int *size
)
85 unsigned int i
, buffer_size
;
90 input
= malloc(buffer_size
);
92 perror("malloc input buffer");
97 input
[i
++] = fgetc(stdin
);
98 if (input
[i
- 1] == EOF
)
100 if (i
>= buffer_size
) {
102 input
= realloc(input
, buffer_size
);
104 perror("realloc input buffer");
105 exit(EX_UNAVAILABLE
);
115 int main(int argc
, char **argv
)
118 unsigned int length
, output_length
;
119 char *to
, *input
, *output
;
124 while((ch
= getopt(argc
, argv
, "edt:")) != -1) {
144 input
= read_input(&length
);
146 /* Automatic mode - guess based on magic markers.
147 * Explicit modes still allowed so can encrypt data with
148 * magic markers in it (if you ever want to). */
150 if (strstr(input
, MARKER_BEGIN
))
156 load_config("otp.conf");
158 if (mode
== ENCRYPT
) {
159 output
= otp_encrypt(input
, length
, to
, &output_length
);
160 } else if (mode
== DECRYPT
) {
161 output_length
= otp_decrypt(input
, &output
);
164 printf("%s", output
);