The Key to Cryptography: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Kmk21
Created page with "This problem asks us to rotate the characters in a string by the value of the character in the same position in a key string. If you use ascii math and char arrays this is ea..."
 
imported>Kmk21
No edit summary
Line 4: Line 4:


encrypt[i] = (((plain[i] - 'A') + (key[i] - 'A')) %26) + 'A'
encrypt[i] = (((plain[i] - 'A') + (key[i] - 'A')) %26) + 'A'
plain[i] = (((encrypt[i] - 'A') - (key[i] - 'A') + 26) % 26) + 'A'
plain[i] = (((encrypt[i] - 'A') - (key[i] - 'A') + 26) % 26) + 'A'



Revision as of 22:29, 2 November 2017

This problem asks us to rotate the characters in a string by the value of the character in the same position in a key string.

If you use ascii math and char arrays this is easy.

encrypt[i] = (((plain[i] - 'A') + (key[i] - 'A')) %26) + 'A'

plain[i] = (((encrypt[i] - 'A') - (key[i] - 'A') + 26) % 26) + 'A'

be sure to add 26 when modding after doing a subtraction.