[CT437]: Add WK06 materials
This commit is contained in:
3
year4/semester2/CT437/exam
Normal file
3
year4/semester2/CT437/exam
Normal 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
|
||||
|
55
year4/semester2/CT437/materials/code/lfsr.c
Normal file
55
year4/semester2/CT437/materials/code/lfsr.c
Normal 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;
|
||||
}
|
||||
|
55
year4/semester2/CT437/materials/code/nlfsr.c
Normal file
55
year4/semester2/CT437/materials/code/nlfsr.c
Normal 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;
|
||||
}
|
||||
|
67
year4/semester2/CT437/materials/code/rc4.c
Normal file
67
year4/semester2/CT437/materials/code/rc4.c
Normal 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;
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user