A simple method of encryption is just to process each source byte with a key byte, using an operation such as XOR
, ADD
or SUB
.
Each of these processes can be reversed if you know the key byte.
These are not processor intensive, and can be run for large amounts of data quickly.
A key of multiple bytes can be used, and just looped so it matches the length of the source data.
The processes can be picked randomly too, and embedded in the key.
If we wanted an 8 byte key, we'd need an 8 byte array for the key bytes, and an 8 byte array for the operations, each byte being a number that represents an operation.
The processes, and their reverse operations are as follows (B is our key byte, A is the source data, C is the output):
A xor B = C
C xor B = A
A add B = C
C sub B = A
A sub B = C
C add B = A
The key can be randomly generated on the fly, and then looped to fit the data:
This is our data.
keykeykeykeykeyke
This applies to both the processes and the key bytes.
A key where all the key bytes are zero will not modify the data, as adding, subtracting, and xoring by zero yeild the original number.
This is useful because our first version of the program (the one we compile) is not encrypted.
An all-zero key means it will not change when decrypted, which is good, as we don't want to change it from the already functional code.