-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.c
More file actions
68 lines (51 loc) · 1.29 KB
/
encrypt.c
File metadata and controls
68 lines (51 loc) · 1.29 KB
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"
uint8_t*
otp_encrypt(char *plaintext, uint8_t *key)
{
int i, length;
uint8_t *ciphertext;
length = strlen(plaintext);
ciphertext = (uint8_t*)calloc(length, sizeof(uint8_t));
for (i = 0; i < length; i++) {
ciphertext[i] = plaintext[i] ^ key[i];
}
return ciphertext;
}
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:p:")) != -1) {
switch (option) {
case 'k':
key_string = optarg;
break;
case 'p':
plaintext = optarg;
break;
}
}
assert( key_string && plaintext );
length = strlen(plaintext);
assert( (strlen(key_string) / 2) >= length );
key = hex_to_bytes(key_string);
encryption = otp_encrypt(plaintext, key);
ciphertext = bytes_to_hex(encryption, length);
printf("%s\n", ciphertext);
free(key);
free(encryption);
free(ciphertext);
return 0;
}