Caesar Shift Cipher

Problem #47

Tags: strings classical cryptography c-0 popular-algorithm

Who solved this?

Also available in: Slovak

If you feel this problem too easy for you, try Caesar Cipher Cracker instead!

Cryptography is one of most interesting branches of programming. Studying its algorithms usually begins with the simple method named after famous Roman emperor Julius Caesar who used it for communicating his military secrets (and perhaps for love letters to Cleopatra).

We will practice deciphering encrypted messages in this problem.

The idea of the algorithm is simple. Each letter of the original text is substituted by another, by the following rule:

For example, if K = 3 (shift value used by Caesar himself), then A becomes D, B becomes E, W becomes Z and Z becomes C and so on, according to the following table:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

| | | | | | | | | | | | | | | | | | | | | | | | | |
V V V V V V V V V V V V V V V V V V V V V V V V V V 

D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

So if the source message was VENI VIDI VICI then after encoding it becomes YHQL YLGL YLFL.

On the other hand encrypted message should be "shifted back" to decode it - or shifted by 26 - K which is the same.

So if we have encoded message HYHQ BRX EUXWXV, we can apply shift of 26 - K = 26 - 3 = 23 and find the original text to be EVEN YOU BRUTUS.

Input data will contain two integers - the number of lines of encrypted text and the value of K.
The following lines will contain encrypted text, consisting of capital latin letters A ... Z, each line is terminated with a dot . which should not be decoded. Answer should contain the decrypted message (in a single line, no line splitting is needed).

Example:

input data:
2 3
YHQL YLGL YLFL.
HYHQ BRX EUXWXV.

answer:
VENI VIDI VICI. EVEN YOU BRUTUS.
You need to login to get test data and submit solution.