#C2532. XOR Cipher: Encode and Decode
XOR Cipher: Encode and Decode
XOR Cipher: Encode and Decode
In this problem, you are given a string and a sequence of integer keys. You need to implement an XOR cipher that encrypts (encodes) the string by applying the XOR operation between each character’s ASCII code and a key. The keys are used in a round-robin fashion. Since the XOR operation is reversible (i.e. a \oplus b \oplus b = a
), you will then decrypt (decode) the encoded string to recover the original message.
Task:
- Write a function that takes a string and a list of keys (integers).
- For each character in the string, compute its new character as: \( c' = c \oplus k_i \) where \( k_i \) is the key selected by the position in a round-robin manner.
- Output the encoded string on the first line and the decoded string on the second line.
The solution should use standard input (stdin) to read the data and standard output (stdout) to print the results.
inputFormat
The input consists of:
- A single line containing the string \( S \) (which may be empty).
- A line containing an integer \( n \) which represents the number of keys.
- A line with \( n \) space-separated integers representing the keys.
outputFormat
The output should contain two lines:
- The first line is the encoded string obtained by XORing the original string with the given keys in a round-robin manner.
- The second line is the decoded string, which should match the original input string.
hello
3
4 5 6
l`jhj
hello
</p>