Caesar Cipher
Introduction
The Caesar cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is shifted a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is apparently named after Julius Caesar, who used it to communicate with his officials.
Implementation
The Caesar cipher is implemented in OCaml as follows:
let islower c =
c >= 'a' && c <= 'z'
let isupper c =
c >= 'A' && c <= 'Z'
let rot x str =
let upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
and lowchars = "abcdefghijklmnopqrstuvwxyz" in
let rec decal x =
if x < 0 then decal (x + 26) else x
in
let x = (decal x) mod 26 in
let decal_up = x - (int_of_char 'A')
and decal_low = x - (int_of_char 'a') in
String.map (fun c ->
if islower c then
let j = ((int_of_char c) + decal_low) mod 26 in
lowchars.[j]
else if isupper c then
let j = ((int_of_char c) + decal_up) mod 26 in
upchars.[j]
else
c
) str
The rot function takes an integer x and a string str as arguments. The integer x represents the number of positions to shift each letter in the string str. The function returns a new string that is the result of applying the Caesar cipher with the given shift.
Here is how to use the function.
let () =
let key = 3 in
let orig = "The five boxing wizards jump quickly" in
let enciphered = rot key orig in
print_endline enciphered;
let deciphered = rot (- key) enciphered in
print_endline deciphered;
Printf.printf "equal: %b\n" (orig = deciphered)
;;
Step-by-Step Explanation
- The
islowerfunction checks if a given character is a lowercase letter. - The
isupperfunction checks if a given character is an uppercase letter. - The
rotfunction initializes two strings:upcharscontaining all uppercase letters andlowcharscontaining all lowercase letters. - The
decalfunction takes an integerxand returns the result of decrementingxby 26 until it is non-negative. - The
xvariable is assigned the result of applyingdecaltoxand then taking the modulus of 26. - The
decal_upvariable is assigned the value ofxminus the integer value of the character ‘A’. - The
decal_lowvariable is assigned the value ofxminus the integer value of the character ‘a’. - The
String.mapfunction applies a function to each character in the stringstr. - For each character
cin the stringstr, the function checks if it is a lowercase letter using theislowerfunction. - If
cis a lowercase letter, the function calculates a new indexjby adding the integer value ofctodecal_lowand taking the modulus of 26. The new character is then obtained by taking thej-th character of thelowcharsstring. - If
cis an uppercase letter, the function calculates a new indexjby adding the integer value ofctodecal_upand taking the modulus of 26. The new character is then obtained by taking thej-th character of theupcharsstring. - If
cis not a letter, the function returnscunchanged. - The function returns the resulting string.
Complexity Analysis
The time complexity of the Caesar cipher algorithm is O(n), where n is the length of the input string str. This is because the String.map function applies a function to each character in the string str, and this operation takes O(1) time per character. Therefore, the overall time complexity is O(n).
The space complexity of the algorithm is also O(n), since the resulting string has the same length as the input string.
In terms of security, the Caesar cipher is very weak and easy to break, since there are only 26 possible shifts and an attacker can easily try all of them. Therefore, it is not suitable for use in modern cryptography, but it can still be used for simple purposes such as obfuscation or educational purposes.