mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Refactor a Caesar Cipher algorithm.
This commit is contained in:
30
src/algorithms/cryptography/caesar-cipher/README.md
Normal file
30
src/algorithms/cryptography/caesar-cipher/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Caesar Cipher Algorithm
|
||||
|
||||
In cryptography, a **Caesar cipher**, also known as **Caesar's cipher**, the **shift cipher**, **Caesar's code** or **Caesar shift**, 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 replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of `3`, `D` would be replaced by `A`, `E` would become `B`, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
|
||||
|
||||

|
||||
|
||||
## Example
|
||||
|
||||
The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain alphabet rotated left or right by some number of positions. For instance, here is a Caesar cipher using a left rotation of three places, equivalent to a right shift of 23 (the shift parameter is used as the key):
|
||||
|
||||
```text
|
||||
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW
|
||||
```
|
||||
|
||||
When encrypting, a person looks up each letter of the message in the "plain" line and writes down the corresponding letter in the "cipher" line.
|
||||
|
||||
```text
|
||||
Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
|
||||
Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
|
||||
```
|
||||
|
||||
## Complexity
|
||||
|
||||
- Time: `O(|n|)`
|
||||
- Space: `O(|n|)`
|
||||
|
||||
## References
|
||||
|
||||
- [Caesar cipher on Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher)
|
||||
Reference in New Issue
Block a user