-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day1-ABitTroubling.c
54 lines (48 loc) · 1.34 KB
/
Day1-ABitTroubling.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
/*
* Jacob Urick
* urick.9@osu.edu
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* charToHex(char* ciphertext, int len){
char* retval = calloc(2*len + 1, sizeof(char));
int i = 0;
for(i = 0; i < len; i++){
sprintf(retval + 2*i, "%02X", (int)ciphertext[i]);
}
return retval;
}
char* crypt(char* key, char* plaintext){
char* ciphertext = calloc(strlen(plaintext), sizeof(char));
char k_char = key[0];
char pt_char = plaintext[0];
int k_index = 0;
int k_bit_index = 0;
int t_index = 0;
do{
if(((1 << k_bit_index) & ((int)(k_char))) != 0){
ciphertext[t_index] = (char) ((int)(plaintext[t_index]) ^ (int) (key[k_index]));
printf("A: %d\n", (int)(k_char));
}else{
printf("B\n");
ciphertext[t_index] = plaintext[t_index];
}
t_index = t_index + 1;
k_bit_index = k_bit_index + 1;
if(k_bit_index == 7){
k_bit_index = 0;
k_index = k_index + 1;
}
if(k_index == strlen(key)){
k_index = 0;
}
k_char = key[k_index];
}while(t_index < strlen(plaintext));
ciphertext = charToHex(ciphertext, strlen(plaintext));
return ciphertext;
}
int main()
{
printf("%s", (crypt("Thisismysecretkey", "Hello! Here is a secret message :)")));
}