TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of Caesar cipher algorithm. More...
#include <iostream>
#include <string>
#include <cassert>
Go to the source code of this file.
Namespaces | |
namespace | ciphers |
Algorithms for encryption and decryption. | |
namespace | caesar |
Functions for Caesar cipher algorithm. | |
Functions | |
std::string | ciphers::caesar::encrypt (const std::string &text, const int &shift) |
std::string | ciphers::caesar::decrypt (const std::string &text, const int &shift) |
void | test () |
int | main () |
Implementation of 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.
The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25. Encryption of a letter x by a shift n can be described mathematically as,
\[ E(x) = (x + n)\;\mbox{mod}\; 26\]
while decryption can be described as,
\[ D(x) = (x - n) \;\mbox{mod}\; 26\]
Definition in file caesar_cipher.cpp.
std::string ciphers::caesar::decrypt | ( | const std::string & | text, |
const int & | shift ) |
Decrypt given text using caesar cipher.
text | text to be decrypted |
shift | number of shifts to be applied |
Definition at line 81 of file caesar_cipher.cpp.
std::string ciphers::caesar::encrypt | ( | const std::string & | text, |
const int & | shift ) |
Encrypt given text using caesar cipher.
text | text to be encrypted |
shift | number of shifts to be applied |
Definition at line 65 of file caesar_cipher.cpp.
int main | ( | void | ) |
Driver Code
Definition at line 120 of file caesar_cipher.cpp.
void test | ( | ) |
Function to test above algorithm
Definition at line 100 of file caesar_cipher.cpp.