Merge branch 'master' of gitlab.com:shockrah/csnotes

This commit is contained in:
shockrahwow 2019-09-23 18:57:33 -07:00
commit 4efc07c2fe
5 changed files with 115 additions and 52 deletions

59
312/hw/2/2.md Normal file
View File

@ -0,0 +1,59 @@
---
title: Homework 2
author: Alejandro Santillana
---
# Section 1 - Symmetric Key Cryptography
1. The two approaches to attacking a given cipher are using _brute force_ or _cryptanalysis_. Brute force methods are a simpler method of attack because they naively try all possible inputs for a given output. Cryptanalysis is the process of using the particular characteristics of a cipher to de-cipher the original message.
2. Chosen plain text attacks involve using a given plain text to determine a cipher text. The attacker themselves choose the plain text and have some kind of cipher text available to them. The algorithm on the other hand is not known to the attacker when performing cryptanalysis. In this way a black box approach to reversing the cipher is taken.
3. Cryptography is the study and practice of creating hidden messages from plain messages. Cryptanalysis is the practice of analyzing hidden messages to determine a plain message.
4. Unconditionally secure ciphers are ciphers which are considered secure regardless of how many resources or time are given to reversing the cipher, sometimes due to not having enough information to determine anything useful. Computationally secure ciphers exist on the premise that it is impractical to attempt to break the cipher given a limited amount of compute power.
5. Key distribution is the first major hurdle to ensure that the one time pad is used effectively. Without properly secure channels to share the key the scheme of a one time pad can not be trusted. The other issue is that the key must be longer than the message itself in order to ensure that parts of it are never reused.
# Section 2
1. Encode: _This is an easy problem_. \
Decode: _rmij'u uamu xyj_. \
Plaintext letter: a b c d e f g h i j k l m n o p q r s t u v w x y z \
Ciphertext letter: m n b v c x z a s d f g h j k l p o i u y t r e w q \
```
Encode portion: uasi si mncmiw lokngch
Decode portion: wasn't that fun
```
2. Plain Text: _Must see you over Cadogan West. Coming at once._ \
Key: _largest_
```
Part A:
L A R G E
S T B C D
F H I K M
N O P Q U
V W X Y Z
Encoded: UZTB DLG ZPN NWLG TGTUERO VLDB DUHFPE RH WQSRZ
Part B:
Encoded: UZTB DLG ZPN NWLG TGTUERO VLDB DUHFPE RH WQSRZ
```
3. Message: "_Let's skip class and go to the beach_" \
Depth = 4 \
```
L S C S G T E
E K L A O H A
T I A N T E C
S P S D O B H
Message: LSCSGTEEKLAOHATIANTECSPSDOBH
```

4
334/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
bin
msh
*txt
*sh

View File

@ -1,10 +1,16 @@
# Single make file for everything becauz lazy
object=hw.c
object=homework/msh/msh.c
cc=gcc
output=bin
output=msh
tmps=*txt
default:
$(cc) $(object) -o $(output)
run:
./bin
clean:
rm -f $(output)
rm -f $(tmps)

View File

@ -1,46 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROMPT "msh> "
#define EXIT_CMD "exit"
#define MAX_BUF 121
void echo(const char*);
void remove_newline(char*);
void exit_branch(const char*);
int main(void) {
char buffer[MAX_BUF];
while(1) {
printf("%s", PROMPT);
fgets(buffer, MAX_BUF, stdin);
if(!strlen(buffer)) {
return 0;
}
// process the input
remove_newline(buffer);
exit_branch(buffer);
echo(buffer);
}
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)) {
return;
}
exit(0);
return; // just for gcc but exit shouldn't fail
}

View File

@ -15,18 +15,36 @@
#define RESP_DATE 2
#define RESP_ECHO 3
#define RESP_SHELL 4
#define RESP_CD_FAIL -1
#define RESP_CD_SUCCESS 6
// Sub statuses that are generally nice to have
#define CD_NOP 0x1
void echo(const char*);
void remove_newline(char*);
void exit_branch(const char*);
int builtin_response(char*);
int cd_handler(char**, const int);
int main(void) {
int main(int argc, char** argv) {
FILE* file = NULL;
if(argc == 2) {
file = fopen(argv[1], "r");
if(!file) { return 1; }
}
char buffer[MAX_BUF];
while(1) {
printf("%s", PROMPT);
fgets(buffer, MAX_BUF, stdin);
// determine if we're reading from stdin or a stream of some sort
if(file == NULL && isatty(STDIN_FILENO)) {
printf("%s", PROMPT);
fgets(buffer, MAX_BUF, stdin);
}
else {
fgets(buffer, MAX_BUF, file);
printf("buffer: %s\n", buffer);
}
// Deal with EOF input
if(!strlen(buffer)) {
exit(0);
@ -41,6 +59,8 @@ int main(void) {
echo(buffer);
case RESP_SHELL: // shell command handler was used
continue;
case RESP_CD_FAIL:
printf("msh: cd: No such file or directory\n");
}
memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage
}
@ -82,6 +102,9 @@ int builtin_response(char* buffer) {
return RESP_DATE;
}
// Populate tokens
char *tokens[64];
int idx = 0;
for(char* cur = strtok(buffer, STRING_DELIMITER); cur != NULL; cur=strtok(NULL, STRING_DELIMITER)) {
@ -92,6 +115,12 @@ int builtin_response(char* buffer) {
idx++;
}
// Handling director changes
int cd_result = cd_handler(tokens, idx);
if(cd_result != CD_NOP) {
return cd_result;
}
// Execute child process
pid_t child = fork();
if(child != 0) {
@ -109,3 +138,14 @@ int builtin_response(char* buffer) {
}
}
int cd_handler(char** tokens, const int t_count) {
if(!strcmp(tokens[0], "cd")) {
if(t_count < 2) {
int ret = chdir(getenv("HOME"));
return ret;
}
return chdir(tokens[1]);
}
return CD_NOP;
}