Question
I need help with the void next_state(LFSR* L) because it is not working properly the code needs to be in C. #include #include #include #include
I need help with the
void next_state(LFSR* L)
because it is not working properly the code needs to be in C.
#include
typedef struct { uint64_t state; uint64_t taps; } LFSR;
int parity(uint64_t N) { /* Return the parity of N*/ int p = __builtin_parity(N); // parity of first 32 bits N = __builtin_bswap64(N); //move back 32 to the front return (p+__builtin_parity(N))%2; //overall parity }
int read_lfsr(LFSR* L) { return = L->state & 0x0000000000000001; // return LSB (rightmost bit)
}
void next_state(LFSR* L) { /*Takes LFSR. Returns nothing. Side effect: advances L to next state.(shift to the right and replace leftmost bit with appropriate value) */ /* You implement this. Hint: make use of the parity() function provided above*/ uint64_t next_bit = parity(L->state & L->taps); L->state = next_bit;
}
void init_LFSR(LFSR* L, uint64_t initial_state, uint64_t taps) { /*Initialize with state and taps*/ L->state = initial_state; L->taps = taps; }
unsigned char get_stream_byte(LFSR* L) { /*Return one byte of keystream. Note that the byte fills up from left to right. */ unsigned char C = 0; int i = 7; for(;i>=0;i--) { //printf("%d ",read_lfsr(L)); C |= (read_lfsr(L)<
void encrypt(char* pt,char* ct,LFSR *L) { /*Use the LFSR stream cipher to encrypt the file named pt (plaintext) and write the output to the file named ct (ciphertext); */ FILE* PT = fopen(pt,"r"); FILE* CT = fopen(ct,"w"); assert(PT); //make sure files opened okay assert(CT); int c; while((c=getc(PT))!=-1) { unsigned char sb = get_stream_byte(L); fputc(sb^c,CT); } fclose(PT); fclose(CT); }
void decrypt(char* pt,char* ct,LFSR *L){ /*Make sure L has been reset to the initial state*/ encrypt(pt,ct,L); }
void get_128_keystream_bits(char* ct,char* kpt) {
FILE* ct_f = fopen(ct,"r"); FILE* fp = fopen("key_stream.txt","w"); int i = 0; for(;i<16;i++) { unsigned char ctc = getc(ct_f); unsigned char b = ctc^kpt[i]; //fprintf(stderr,"%d ",b); int j = 7; for(;j>=0;j--){ int bit = parity(b & (1< } void shape_keystream() { /* This function opens the file "key_stream.txt" created by get_128_keystream_bits(). It uses this data to create a file called "S.mat.sage" which is a 64x64 matrix of keystream bits of the form discussed in the project description. It also creates a vector (a 64x1 matrix) of keystream bits of the form described in the project description. This is stored in a file called "V.mat.sage" */ //See the file matrix.sagews for examples of what the output should look like. /*You implement this*/ } int main() { LFSR L; uint64_t initial_state = 0xbeefcafebabecab1; uint64_t taps = 0xdeaddeedacedface; init_LFSR(&L,initial_state,taps); encrypt("toy_pt.txt","ct.txt",&L); init_LFSR(&L,initial_state,taps); decrypt("ct.txt","toy_ot.txt",&L); //get_128_keystream_bits("target_ciphertext","
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started