Your job is to encrypt a string using a number key.
Your program will receive 2 strings: one containing a sequence of characters to encrypt, and another one containing an even amount of digits (1
-9
, never 0
).
The encrypting will work like this:
- take the string to be encrypt and decode it to binary (ISO-8859-1)
- for every pair of numbers
x
andy
in the number string do: for every character in the string of zeroes and ones, starting from the first one, if the bit is0
toggle thex
th bit in front of the bit you are reading right now. If the bit is1
, toggle they
th bit in front of the bit you are reading right now. If the bit to be toggled is out of bounds, do not toggle anything.
Other than that:
This is a code golf challenge, shortest code wins. Standard Loopholes apply.
Here's a more graphic explanation of what happens:
inputs: "a", "23"
"a" =BIN> "01100001"
"01100001" //first bit is a 0, get the first number in the key, 2.
- ^ //toggle the bit 2 steps in front
"01000001" //next bit is 1, get second number in the key, 3.
- ^ //toggle the bit 3 steps in front
"01001001" //0 bit, toggle the bit that is 2 steps in front.
- ^
"01000001" //etc
- ^
"01000101"
- ^
"01000111"
- ^ //no change, out of bounds.
"01000111"
- ^
"01000111"
- ^
"01000111" =ASCII> "G"
So: a
, 23
= G
. Then, running To decrypt a string encrypted using key G
, 23
should be a
because this process is perfectly reversible.12438135
, reverse the ordering of the pairs, 35814312
and run the encrypted string through a similar program that performs the operations in reverse order (starting at the end, approaching the beginning) using that key. (thanks to Martin Büttner for pointing that out)
Good luck and have fun! (My first challenge, tell me if i missed anything :))
for (i=0; i+1 < length; i++)
? What does "in front of" mean? And why does the code block only usex
and nevery
? \$\endgroup\$n
if I feed itG
. \$\endgroup\$