The cipher works as the following;
- Assume we have the plaintext : "helloworld"
- And the key : "key"
- The key will repeat alongside the plaintext like;
helloworld
keykeykeyk
- Then we simply need to XOR every byte in our plaintext and the key.
I have been struggling to find the desired index of the key for the encryption.
As an example, if I am at w I need to XOR it with y, at d my program need to know to XOR it with k. How can tell my program to XOR it with the needed element of the key?
>Solution :
I believe you are asking about the formula you need to use. The formula for the vernam cipher is;
- For encrypting Ci = Pi ^ Ki
- For decrypting Pi = Ci ^ Ki
I believe there two approaches you can use. You can either;
- Have a second string where the key repeats until you reach the length of your plaintext.
- Use the length of the key as your modulo for the index of the plaintext you are at.
For the latter, it looks like the following;
while(str[i] != '\0'){
cipher[i] = str[i] ^ key[i%strlen(key)];
i++;
}