diff --git a/312/notes/ciphers.md b/312/notes/ciphers.md new file mode 100644 index 0000000..49fa7b5 --- /dev/null +++ b/312/notes/ciphers.md @@ -0,0 +1,50 @@ +# Block Ciphers + +The main concept here is twofold : + +* we take _blocks_ of data and cipher the _blocks_ +* A given key is actually used to generate recursive keys to be further used on the data itself + + +_bs example ahead_ + +Say we have a key 7 and some data 123456. +We take the whole data set and chunk it into blocks(for example): 12 34 56. + +Let's say our function here is to just add 7 to each block so we do the first step: + +``` +12 + 7 = 19 +Unlike other ciphers we don't reuse 7; instead we use the new thing as both the new key and part of our cipher text + +19 + 34 = 53 +Cipher: 1953.. + +53 + 56 = 109 <= let's pretend that this rolls over 99 and back to 00 + 09 <= like this + +Final cipher: 195309 +``` + +_It should be noted that in practice these functions usually take in huge keys and blocks_. + +# Feistal Cipher + +Two main components: + +1. each _thing_ in the data to cipher is replaced by a _ciphered thing_ + +2. nothing is added or deleted or replaced in sequence, instead the order of _things_ is changed. + +Basically imagine that every _type of thing_ in our data maps to some other _type of thing/thing_ in the data and thus become swapped/reordered. + +# DES - Data Encryption Standard + +Widely used until about 2001 when AES surpassed it as the newer(ish(kinda)) standard. + +DEA was the actual algorithm tho: + +* 64 bit blocks +* 56 bit keys +* turns a 64-bit input into a 64-bit output (wew) +* Steps in reverse also reverse the encryption itself diff --git a/334/homework/3/chap5.txt b/334/homework/3/chap5.txt new file mode 100644 index 0000000..a98d582 --- /dev/null +++ b/334/homework/3/chap5.txt @@ -0,0 +1,14 @@ +# +# Problem lines begin with "#@". +# For each problem line, enter your answer on the following line. +# Do not modify this file in any way except to enter your answers. +# + +#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer) +b +#@ 2 -- enter 'a', 'b', or 'c' +a +#@ 3 -- enter 'a', 'b', or 'c' +b +#@ 4 -- enter 'a', 'b', or 'c' +a diff --git a/334/homework/3/chap6.txt b/334/homework/3/chap6.txt new file mode 100644 index 0000000..c874cce --- /dev/null +++ b/334/homework/3/chap6.txt @@ -0,0 +1,14 @@ +# +# Problem lines begin with "#@". +# For each problem line, enter your answer on the following line. +# Do not modify this file in any way except to enter your answers. +# + +#@ 1 -- enter 'a' or 'b' (don't use quotes in any answer) +b +#@ 2 -- enter 'a', 'b', or 'c' +a +#@ 3 -- enter 'T' or 'F' +F +#@ 4 -- enter 'T' or 'F' +F diff --git a/334/homework/3/msh.c b/334/homework/3/msh.c new file mode 100644 index 0000000..73e6511 --- /dev/null +++ b/334/homework/3/msh.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include + +#define PROMPT "msh> " +#define EXIT_CMD "exit" +#define MAX_BUF 121 + +// Statuses from builtin responses +#define RESP_HELP 1 +#define RESP_DATE 2 +#define RESP_ECHO 3 +#define RESP_SHELL 4 + +void echo(const char*); +void remove_newline(char*); +void exit_branch(const char*); +int builtin_response(char*); + + +int main(void) { + char buffer[MAX_BUF]; + while(1) { + printf("%s", PROMPT); + fgets(buffer, MAX_BUF, stdin); + // Deal with EOF input + if(!strlen(buffer)) { + exit(0); + } + exit_branch(buffer); + + remove_newline(buffer); + int status = builtin_response(buffer); + // the switch in case we need to add more response flags later + switch(status) { + case RESP_ECHO: + echo(buffer); + case RESP_SHELL: // shell command handler was used + continue; + } + memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage + } + return 0; +} + +void echo(const char* buf) { + printf("%s\n", buf); +} + + +void remove_newline(char* buf) { + char* c = buf; + while(*c != '\n') { c++; } + *c='\0'; +} + +void exit_branch(const char* buf) { + if(!strcmp(buf, EXIT_CMD)) { + exit(0); + } +} + +int builtin_response(char* buffer) { +#define STRING_DELIMITER " " +#define TIME_FMT "%D" + exit_branch(buffer); // exit is builtin so here we are + if(!strcmp(buffer, "help")) { + echo("enter Linux commands, or ‘exit’ to exit"); + return RESP_HELP; + } + + // Printout the date today + if(!strcmp(buffer, "date")) { + static char date[64]; + time_t now = time(0); + strftime(date, sizeof(date), TIME_FMT, localtime(&now)); + echo(date); + return RESP_DATE; + } + + char *tokens[64]; + int idx = 0; + for(char* cur = strtok(buffer, STRING_DELIMITER); cur != NULL; cur=strtok(NULL, STRING_DELIMITER)) { +#ifdef DBG_LINE + printf("%s ", cur); +#endif + tokens[idx] = cur; + idx++; + } + + // Execute child process + pid_t child = fork(); + if(child != 0) { + int status_child = wait(NULL); + if(status_child == 127) { + printf("msh: %s: %d", tokens[0], status_child); + } + } + else { + execvp(tokens[0], tokens); + } + // Cleanup pointers + for(int i = 0; i< 64; i++) { + tokens[i] = NULL; + } + +}