TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
caesar_cipher.cpp
Go to the documentation of this file.
1
25#include <iostream>
26#include <string>
27#include <cassert>
28
32namespace ciphers {
36 namespace caesar {
37 namespace {
43 inline char get_char(const int x) {
44 // By adding 65 we are scaling 0-25 to 65-90.
45 // Which are in fact ASCII values of A-Z.
46 return char(x + 65);
47 }
53 inline int get_value(const char c) {
54 // A-Z have ASCII values in range 65-90.
55 // Hence subtracting 65 will scale them to 0-25.
56 return int(c - 65);
57 }
58 } // Unnamed namespace
65 std::string encrypt (const std::string &text, const int &shift) {
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 }
81 std::string decrypt (const std::string &text, const int &shift) {
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 }
94 } // namespace caesar
95} // namespace ciphers
96
100void test() {
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}
118
120int main() {
121 // Testing
122 test();
123 return 0;
124}
void test()
int main()
Functions for Caesar cipher algorithm.
Algorithms for encryption and decryption.
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