Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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

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 ()
 

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
texttext to be decrypted
shiftnumber of shifts to be applied
Returns
new decrypted text
81 {
82 std::string decrypted_text = ""; // Empty string to store decrypted text
83 for (char c : text) { // Going through each character
84 int place_value = get_value(c); // Getting value of character (i.e. 0-25)
85 place_value = (place_value - shift) % 26;// Applying decryption formula
86 if(place_value < 0) { // Handling case where remainder is negative
87 place_value = place_value + 26;
88 }
89 char new_char = get_char(place_value); // Getting original character from decrypted value (i.e. A-Z)
90 decrypted_text += new_char; // Appending decrypted character
91 }
92 return decrypted_text; // Returning decrypted text
93 }
char get_char(const std::string &input, std::size_t pos)
Returns the character at pos after the input is padded.
Definition sha256.cpp:170

◆ encrypt()

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

Encrypt given text using caesar cipher.

Parameters
texttext to be encrypted
shiftnumber of shifts to be applied
Returns
new encrypted text
65 {
66 std::string encrypted_text = ""; // Empty string to store encrypted text
67 for (char c : text) { // Going through each character
68 int place_value = get_value(c); // Getting value of character (i.e. 0-25)
69 place_value = (place_value + shift) % 26; // Applying encryption formula
70 char new_char = get_char(place_value); // Getting new character from new value (i.e. A-Z)
71 encrypted_text += new_char; // Appending encrypted character
72 }
73 return encrypted_text; // Returning encrypted text
74 }

◆ main()

int main ( void )

Driver Code

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

◆ test()

void test ( )

Function to test above algorithm

100 {
101 // Test 1
102 std::string text1 = "ALANTURING";
103 std::string encrypted1 = ciphers::caesar::encrypt(text1, 17);
104 std::string decrypted1 = ciphers::caesar::decrypt(encrypted1, 17);
105 assert(text1 == decrypted1);
106 std::cout << "Original text : " << text1;
107 std::cout << " , Encrypted text (with shift = 21) : " << encrypted1;
108 std::cout << " , Decrypted text : "<< decrypted1 << std::endl;
109 // Test 2
110 std::string text2 = "HELLOWORLD";
111 std::string encrypted2 = ciphers::caesar::encrypt(text2, 1729);
112 std::string decrypted2 = ciphers::caesar::decrypt(encrypted2, 1729);
113 assert(text2 == decrypted2);
114 std::cout << "Original text : " << text2;
115 std::cout << " , Encrypted text (with shift = 1729) : " << encrypted2;
116 std::cout << " , Decrypted text : "<< decrypted2 << std::endl;
117}
std::string decrypt(const std::string &text, const int &shift)
Definition caesar_cipher.cpp:81
std::string encrypt(const std::string &text, const int &shift)
Definition caesar_cipher.cpp:65
T endl(T... args)
Here is the call graph for this function: