[CT437]: Add WK06 materials

This commit is contained in:
2025-02-19 15:40:48 +00:00
parent 40f1bcab0b
commit 119ea085e1
6 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,3 @@
- always question about rsa and diffie helman
- don't expecct any diffie helman calculations on the exam, that would be ridiciulous

View File

@ -0,0 +1,55 @@
//
// Example for a 16-bit NFLSR
//
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define LFSR_SIZE 16
#define DEFAULT_START_STATE 0xACE1 // Default initial state for NLFSR
uint16_t lfsr;
uint8_t used[65535];
uint8_t non_linear_feedback(uint16_t state) {
// Example non-linear function: XOR of specific bits and AND of others
uint8_t bit1 = (state >> 0) & 1;
uint8_t bit2 = (state >> 2) & 1;
uint8_t bit3 = (state >> 3) & 1;
uint8_t bit4 = (state >> 5) & 1;
uint8_t bit5 = (state >> 9) & 1;
return (bit1 ^ bit2 ^ bit3 ^ bit4 ^ bit5);
}
uint8_t clock_and_shift_lfsr() {
uint8_t ret = lfsr & 1;
uint8_t new_bit = non_linear_feedback(lfsr);
lfsr = (lfsr >> 1) | (new_bit << (LFSR_SIZE - 1));
return ret;
}
int main() {
uint16_t input;
uint16_t cycle = 0;
memset(used, sizeof(used), 0);
printf("Enter IV for LFSR: "); scanf("%hd", &input);
input = input & 0xFFFF;
lfsr = input;
while (1) {
cycle++;
printf("%d", clock_and_shift_lfsr());
if (used[lfsr] > 0)
break;
else
used[lfsr] = 1;
// printf("%d\n", nlfsr);
}
printf("\nCycle length: %d bits.\n", cycle);
return 0;
}

View File

@ -0,0 +1,55 @@
//
// Example for a 16-bit NFLSR
//
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define NLFSR_SIZE 16
#define DEFAULT_START_STATE 0xACE1 // Default initial state for NLFSR
uint16_t nlfsr;
uint8_t used[65535];
uint8_t non_linear_feedback(uint16_t state) {
// Example non-linear function: XOR of specific bits and AND of others
uint8_t bit1 = (state >> 0) & 1;
uint8_t bit2 = (state >> 2) & 1;
uint8_t bit3 = (state >> 3) & 1;
uint8_t bit4 = (state >> 5) & 1;
uint8_t bit5 = (state >> 9) & 1;
return (bit1 ^ bit2) | (bit3 & bit4) ^ bit5;
}
uint8_t clock_and_shift_nlfsr() {
uint8_t ret = nlfsr & 1;
uint8_t new_bit = non_linear_feedback(nlfsr);
nlfsr = (nlfsr >> 1) | (new_bit << (NLFSR_SIZE - 1));
return ret;
}
int main() {
uint16_t input;
uint16_t cycle = 0;
memset(used, sizeof(used), 0);
printf("Enter IV for NLFSR: "); scanf("%hd", &input);
input = input & 0xFFFF;
nlfsr = input;
while (1) {
cycle++;
printf("%d", clock_and_shift_nlfsr());
if (used[nlfsr] > 0)
break;
else
used[nlfsr] = 1;
// printf("%d\n", nlfsr);
}
printf("\nCycle length: %d bits.\n", cycle);
return 0;
}

View File

@ -0,0 +1,67 @@
//
// Example implementation of RC4
//
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define A_LEN 256
#define SWAP(a, b) { uint8_t temp = a; a = b; b = temp; }
void rc4_init(uint8_t *key, uint8_t *S, int key_length) {
int i, j = 0;
for (i = 0; i < 256; i++) {
S[i] = i;
}
for (i = 0; i < 256; i++) {
j = (j + S[i] + key[i % key_length]) % 256;
SWAP(S[i], S[j]);
}
}
void rc4_generate_keystream(uint8_t *S, uint8_t *keystream, int length) {
int i = 0, j = 0, k;
for (k = 0; k < length; k++) {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
SWAP(S[i], S[j]);
keystream[k] = S[(S[i] + S[j]) % 256];
}
}
void rc4_encrypt_decrypt(uint8_t *data, uint8_t *keystream, int length) {
for (int i = 0; i < length; i++) {
data[i] ^= keystream[i];
}
}
int main() {
uint8_t key[A_LEN];
uint8_t S[A_LEN];
uint8_t data[A_LEN];
int data_length;
uint8_t keystream[A_LEN];
printf("Enter key: "); scanf("%s", key);
printf("Enter plaintext: "); scanf("%s", data);
data_length = strlen((char *)data);
rc4_init(key, S, strlen((char *)key));
rc4_generate_keystream(S, keystream, data_length);
rc4_encrypt_decrypt(data, keystream, data_length);
printf("Encrypted: ");
for (int i = 0; i < data_length; i++) {
printf("%02X ", data[i]);
}
printf("\n");
// Decrypting the data
rc4_encrypt_decrypt(data, keystream, data_length);
printf("Decrypted: %s\n", data);
return 0;
}