Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vernam Cipher – C implementation / Key Repetition

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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++;
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading