Caesar Cipher
Definition​
- Definition
- Explanation
- Guidance
- Tips
The Caesar Cipher is a simple substitution cipher technique in cryptography. It involves shifting the letters of the alphabet by a fixed number of positions. It's named after Julius Caesar, who is reputed to have used it to communicate with his generals
In the Caesar Cipher algorithm, a fixed number called the "key" or "shift" is chosen. This key determines how many positions each letter in the plaintext message is shifted. For instance, with a shift of 3, 'A' becomes 'D', 'B' becomes 'E', and so forth. The alphabet wraps around, meaning 'Z' becomes 'C'. Ultimately, the shifted letters constitute the ciphertext
- Decide on a shift value, typically denoted by 'k'
- Iterate through each character in the plaintext message
- For each character
- if it's a letter, shift it by 'k' positions
- preserve uppercase/lowercase and handle wrapping around the alphabet
- if it's not a letter, leave it unchanged
- Assemble the shifted characters to form the ciphertext
- remember to handle both uppercase and lowercase letters separately
- ensure the shift value is within the range 1-25 to avoid redundant shifts
- watch out for non-alphabetic characters in the plaintext and maintain their positions in the ciphertext
Practice​
- Practice
- Solution
caesarCipher(plaintext, shift):
ciphertext = ""
for each character in plaintext:
if character is an uppercase letter:
shifted_char = (character + shift - 'A') % 26 + 'A'
else if character is a lowercase letter:
shifted_char = (character + shift - 'a') % 26 + 'a'
else:
shifted_char = character
ciphertext += shifted_char
return ciphertext
package main
func caesarCipher(text string, shift int) string {
result := ""
for _, char := range text {
if char >= 'A' && char <= 'Z' {
result += string((int(char-'A')+shift)%26 + 'A')
} else if char >= 'a' && char <= 'z' {
result += string((int(char-'a')+shift)%26 + 'a')
} else {
result += string(char)
}
}
return result
}
public class CaesarCipher {
public static String caesarCipher(String text, int shift) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isUpperCase(c)) {
result.append((char) ((c + shift - 'A') % 26 + 'A'));
} else if (Character.isLowerCase(c)) {
result.append((char) ((c + shift - 'a') % 26 + 'a'));
} else {
result.append(c);
}
}
return result.toString();
}
}
function caesarCipher(text, shift) {
return text.replace(/[a-zA-Z]/g, function (char) {
const baseCharCode = char <= "Z" ? 65 : 97;
return String.fromCharCode(
((char.charCodeAt(0) - baseCharCode + shift) % 26) + baseCharCode,
);
});
}
fun caesarCipher(text: String, shift: Int): String {
return text.map { char ->
when {
char in 'A'..'Z' -> ((char - 'A' + shift) % 26 + 'A'.toInt()).toChar()
char in 'a'..'z' -> ((char - 'a' + shift) % 26 + 'a'.toInt()).toChar()
else -> char
}
}.joinToString("")
}
def caesar_cipher(text, shift):
result = ""
for char in text:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
result += chr((ord(char) - base + shift) % 26 + base)
else:
result += char
return result
fn caesar_cipher(text: &str, shift: u8) -> String {
text.chars()
.map(|c| {
if c.is_ascii_alphabetic() {
let base = if c.is_ascii_uppercase() { b'A' } else { b'a' };
((c as u8 - base + shift) % 26 + base) as char
} else {
c
}
})
.collect()
}
function caesarCipher(text: string, shift: number): string {
return text.replace(/[a-zA-Z]/g, (char: string) => {
const baseCharCode = char <= "Z" ? 65 : 97;
return String.fromCharCode(
((char.charCodeAt(0) - baseCharCode + shift) % 26) + baseCharCode,
);
});
}