-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.c
68 lines (51 loc) · 1.29 KB
/
decrypt.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "otp.h"
char*
otp_decrypt(uint8_t *ciphertext, uint8_t *key, int length)
{
int i;
char *plaintext;
plaintext = (char*)calloc(length + 1, sizeof(char));
for (i = 0; i < length; i++) {
plaintext[i] = ciphertext[i] ^ key[i];
}
plaintext[length] = '\0';
return plaintext;
}
int
main(int argc, char *argv[])
{
int option,
length;
char *key_string = NULL,
*plaintext = NULL,
*ciphertext = NULL;
uint8_t *key = NULL,
*encryption = NULL;
while ((option = getopt(argc, argv, "k:c:")) != -1) {
switch (option) {
case 'k':
key_string = optarg;
break;
case 'c':
ciphertext = optarg;
break;
}
}
assert( key_string && ciphertext );
length = strlen(ciphertext);
assert( strlen(key_string) >= length );
key = hex_to_bytes(key_string);
encryption = hex_to_bytes(ciphertext);
plaintext = otp_decrypt(encryption, key, length / 2);
printf("%s\n", plaintext);
free(plaintext);
free(key);
free(encryption);
return 0;
}