Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of Caesar cipher algorithm. More...
#include <iostream>
#include <string>
#include <cassert>
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\]
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 |
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 |
int main | ( | void | ) |
void test | ( | ) |
Function to test above algorithm