The % is modulus (remainder) operator and ^ is bitwise.
Specifically, bitwise XOR. Very important to know what operation is
But why are the ^ and % operators used here:
def encrypt(reader, writer)
key_index = 0
while not reader.eof?
clear_char = reader.getc
encrypted_char = clear_char ^ @key[key_index]
key_index = (key_index + 1) % @key.size
You're using ^ because you are encrypting each char by doing a bitwise
XOR with the corresponding byte of the key.
You're using % because the the key, in this case, is of finite length.
If your plaintext is longer, then you have to repeat the key, i.e.,
start over at the beginning of the key but keep going within the
plaintext. (Or of course come up with a new key, and find some way to
tell the correspondent what the new key is, but that's a whole 'nother
Google things like basic cryptography, introduction to cryptography,
cryptography tutorial, etc. for more info.