Caesar Cipher Algorithm
The Caesar Cipher Algorithm is a substitution cipher that was used in ancient Rome for securing secret communications. Named after Julius Caesar, who is believed to have used it for his private correspondence, this encryption technique involves shifting the letters of the plaintext by a fixed number of positions in the alphabet. For instance, if the shift value is 3, the letter 'A' would be replaced by 'D', 'B' would become 'E', and so on. The Caesar Cipher is considered an early example of a monoalphabetic substitution cipher, where each letter in the plaintext is systematically replaced by another letter.
Despite its simplicity, the Caesar Cipher was a highly effective method of encryption during its time, as it was able to provide a basic level of secrecy for sensitive information. However, with the advent of modern cryptographic techniques, the Caesar Cipher is no longer considered secure due to its easy susceptibility to frequency analysis attacks. In such attacks, an adversary can analyze the frequency of letters in the ciphertext and compare them to the known frequency distribution of letters in the language, making it relatively simple to decipher the original message. Nowadays, the Caesar Cipher is mainly used for educational purposes to introduce students to the basic concepts of cryptography.
package main
import (
"bytes"
"flag"
"fmt"
"strings"
)
func main() {
cipherKey := flag.Int("c", 0, "Cipher shift amount (-26 - 26)")
input := flag.String("i", "", "Input")
flag.Parse()
if *cipherKey > 26 || *cipherKey < -26 {
flag.PrintDefaults()
} else {
fmt.Println(caesarCipher(*input, *cipherKey))
}
}
func caesarCipher(input string, key int) string {
var outputBuffer bytes.Buffer
for _, r := range strings.ToLower(input) {
newByte := int(r)
if newByte >= 'a' && newByte <= 'z' {
newByte += key
if newByte > 'z' {
newByte -= 26
} else if newByte < 'a' {
newByte += 26
}
}
outputBuffer.WriteString(string(newByte))
}
return outputBuffer.String()
}