The goal is to write a C program to implement a simple stream cipher. The progra
ID: 3529032 • Letter: T
Question
The goal is to write a C program to implement a simple stream cipher. The program should accept as user input a plaintext string of some fixed length and an integer seed value. Your program should implement two functions, i.e., one for encryption and one for decryption. The encryption function takes the seed value and the plaintext string as parameters. It first generates a random string of the same size as the plaintext string (using the provided seed value) and XORs individual characters of the two strings (plaintext string and the generated random string) to produce the ciphertext string. The decryption function takes a seed value and the ciphertext string as parameters and performs the reverse operation, i.e., generates the same random string (using the same seed value as the one used in the encryption function) and XORs it one character at a time with the ciphertext string in order to produce the original plaintext string. Verify that the decryption function decrypts the ciphertext string correctly to produce the original plaintext inputted by the user.Explanation / Answer
include char staticKey; void CycleKey(char data) { /* this is where the real magic should occur */ /* this code does *not* do a good job of it. */ staticKey += data; if (staticKey & 0x80) { staticKey ^= 0xD8; } else { staticKey += 0x8B; } } void ResetCipher(const char * key) { staticKey = 0; while (*key) { CycleKey(*key); key++; } } void Encrypt(const char * plaintext, char * encrypted) { while (*plaintext) { *encrypted = *plaintext + staticKey; CycleKey(*encrypted); encrypted++; plaintext++; } *encrypted = ''; } void Decrypt(char * plaintext, const char * encrypted) { while (*encrypted) { *plaintext = *encrypted - staticKey; CycleKey(*encrypted); plaintext++; encrypted++; } *plaintext = ''; } int main(void) { char * key = "123"; char * message = "Hello, World!"; char encrypted[20]; char decrypted[20]; ResetCipher(key); Encrypt(message, encrypted); ResetCipher(key); Decrypt(decrypted, encrypted); printf("output: %s ", decrypted); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.