Caesar cipher

Caesar cipher (shift cipher) is a simple substitution cipher based on a replacement of every single character of the open text with a character, which is fixed number of positions further down the alphabet.

In the times of Julius Caesar was used only the shift of 3 characters, but nowadays the term Caesar cipher refers to all variants (shifts) of this cryptosystem.

The encryption can be described with the following formula:

C_{i} = (T_{i} + k) \\pmod{m}
Ci - i-th character of the closed text
Ti - i-th character of the open text
k - shift
m - length of the alphabet

The process of decryption uses reverted procedure:

T_{i} = (C_{i} - k) \\pmod{m}

Example

Encryption

Open text: "attackatdawn", shift: 3

a + 3 = 0  + 3 = 3  = d
t + 3 = 19 + 3 = 22 = w
t + 3 = 19 + 3 = 22 = w
a + 3 = 0  + 3 = 3  = d
c + 3 = 2  + 3 = 5  = f
k + 3 = 10 + 3 = 13 = n
a + 3 = 0  + 3 = 3  = d
t + 3 = 19 + 3 = 22 = w
d + 3 = 3  + 3 = 6  = g
a + 3 = 0  + 3 = 3  = d
w + 3 = 22 + 3 = 25 = z
n + 3 = 13 + 3 = 16 = q

Closed text: "dwwdfndwgdzq"

Decryption

Closed text: "dwwdfndwgdzq", shift: 3

d - 3 = 3  - 3 = 0  = a
w - 3 = 22 - 3 = 19 = t
w - 3 = 22 - 3 = 19 = t
d - 3 = 3  - 3 = 0  = a
f - 3 = 5  - 3 = 2  = c
n - 3 = 13 - 3 = 10 = k
d - 3 = 3  - 3 = 0  = a
w - 3 = 22 - 3 = 19 = t
g - 3 = 6  - 3 = 3  = d
d - 3 = 3  - 3 = 0  = a
z - 3 = 25 - 3 = 22 = w
q - 3 = 16 - 3 = 13 = n

Open text: "attackatdawn"

Breaking the cipher

All variants

Because the number of variants of the cipher is very limited (number of characters in the alphabet), it is possible to try all of them and choose the one, where the deciphered text makes sense.

Frequency analysis

If we know in which language was the open text written, we also know, how often (percentually) are used different characters in this language. So if we calculate the frequency of characters of the encrypted text, it should be shifted in comparison to the language frequency by fixed number of characters. With the knowledge of the shift, we can decipher the closed text.

Code

/**
 * Caesar cipher
 * @author Pavel Micka
 */
public class CaesarCipher {
    /**
     * Shift (the original variant had 3)
     */
    public static final int SHIFT = 3;

    /**
     * Encrypt using Caesar cipher
     * @param s string containing only uppercase characters
     * @return ecrypted string (closed text)
     */
    public static String encipher(String s){
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < s.length(); i ++){
            if(s.charAt(i) < 65 || s.charAt(i) > 90){ //znak v ASCII
                throw new IllegalArgumentException("" +
                        "The string does not contain only uppercase characters");
            }
            //modularly add the shift
            char enciphered = s.charAt(i) + SHIFT > 90 ? (char)((s.charAt(i) + SHIFT) - 26) : (char)(s.charAt(i) + SHIFT);
            builder.append(enciphered);
        }
        return builder.toString();
    }
    /**
     * Decrypt using Caesar cipher
     * @param s string containing only uppercase characters
     * @return decrypted string (open text)
     */
    public static String decipher(String s){
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < s.length(); i ++){
            if(s.charAt(i) < 65 || s.charAt(i) > 90){ //znak v ASCII
                throw new IllegalArgumentException("" +
                        "The string does not contain only uppercase characters");
            }
            //modularly subtract the shift
            char deciphered = s.charAt(i) - SHIFT < 65 ? (char)((s.charAt(i) - SHIFT) + 26) : (char)(s.charAt(i) - SHIFT);
            builder.append(deciphered);
        }
        return builder.toString();
    }
}

www.EuAutodily.cz SEO od společnosti Digital Pylon


Online casino s algoritmem

České casino online online slot-vegas.cz





Zajímavé články: Jak najít práci snů? Zvolte kariéru v IT!, Češi mají rádi hrací automaty online, Jak funguje algoritmické obchodování Casino, Online výuka Algoritmus a online marketing mají svá pravidla, Automaty, Matematický vliv, Ratings, Jak fungují algoritmy hazardních her online: více znalostí, více peněz, SYPWAI - nástroj pro vědecký vývoj, Vynikají na globálním trhu: Nejlepší vývojáři softwaru pro online výherní automaty, Jak si vybrat nejlepší české online casino, Proč byste měli hrát online casino VPN revoluce, Kde najdeme algoritmy v každodenním životě?