caesar_cipher.cpp File Reference

Implementation of Caesar cipher algorithm. More...

`#include <iostream>`
`#include <string>`
`#include <cassert>`
Include dependency graph for caesar_cipher.cpp:

## Namespaces

| ciphers
| Algorithms for encryption and decryption.
| 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 ()

## Detailed Description

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.

### Algorithm

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\]

Note
  This program implements caesar cipher for only uppercase English alphabet characters (i.e. A-Z).

Author
  Deep Raval

## Function Documentation

## decrypt()

std::string ciphers::caesar::decrypt | ( | const std::string & | text,
|  | const int & | shift
| ) |  |

Decrypt given text using caesar cipher.

Parameters

  text | text to be decrypted
  shift | number of shifts to be applied

Returns
  new decrypted text

{
std::string decrypted_text = ""; // Empty string to store decrypted text
for (char c : text) { // Going through each character
int place_value = get_value(c); // Getting value of character (i.e. 0-25)
place_value = (place_value - shift) % 26;// Applying decryption formula
if(place_value < 0) { // Handling case where remainder is negative
place_value = place_value + 26;
}
char new_char = get_char(place_value); // Getting original character from decrypted value (i.e. A-Z)
decrypted_text += new_char; // Appending decrypted character
}
return decrypted_text; // Returning decrypted text
}
std::string
Here is the call graph for this function:

## encrypt()

std::string ciphers::caesar::encrypt | ( | const std::string & | text,
|  | const int & | shift
| ) |  |

Encrypt given text using caesar cipher.

Parameters

  text | text to be encrypted
  shift | number of shifts to be applied

Returns
  new encrypted text

{
std::string encrypted_text = ""; // Empty string to store encrypted text
for (char c : text) { // Going through each character
int place_value = get_value(c); // Getting value of character (i.e. 0-25)
place_value = (place_value + shift) % 26; // Applying encryption formula
char new_char = get_char(place_value); // Getting new character from new value (i.e. A-Z)
encrypted_text += new_char; // Appending encrypted character
}
return encrypted_text; // Returning encrypted text
}
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Driver Code

{
// Testing
test();
return 0;
}
test
void test()
Definition: caesar_cipher.cpp:100
Here is the call graph for this function:

## test()

void test | ( |  | ) |

Function to test above algorithm

{
// Test 1
std::string text1 = "ALANTURING";
std::string encrypted1 = ciphers::caesar::encrypt(text1, 17);
std::string decrypted1 = ciphers::caesar::decrypt(encrypted1, 17);
assert(text1 == decrypted1);
std::cout << "Original text : " << text1;
std::cout << " , Encrypted text (with shift = 21) : " << encrypted1;
std::cout << " , Decrypted text : "<< decrypted1 << std::endl;
// Test 2
std::string text2 = "HELLOWORLD";
std::string encrypted2 = ciphers::caesar::encrypt(text2, 1729);
std::string decrypted2 = ciphers::caesar::decrypt(encrypted2, 1729);
assert(text2 == decrypted2);
std::cout << "Original text : " << text2;
std::cout << " , Encrypted text (with shift = 1729) : " << encrypted2;
std::cout << " , Decrypted text : "<< decrypted2 << std::endl;
}
std::cout
ciphers::caesar::decrypt
std::string decrypt(const std::string &text, const int &shift)
Definition: caesar_cipher.cpp:81
ciphers::caesar::encrypt
std::string encrypt(const std::string &text, const int &shift)
Definition: caesar_cipher.cpp:65
std::endl
T endl(T... args)
Here is the call graph for this function: