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
 
(One intermediate revision by the same user not shown)
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'


Line 11: Line 12:
[[Category:Ecna2016]]
[[Category:Ecna2016]]
[[Category:Algorithm Trivial]]
[[Category:Algorithm Trivial]]
[[Category:Implementation Easy]]
[[Category:Implementation Trivial]]
[[Category:ASCII]]
[[Category:ASCII]]
[[Category:Modulus]]
[[Category:Modulus]]

Latest 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.