-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
caeser_cipher.cpp
90 lines (77 loc) · 2.45 KB
/
caeser_cipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Encrypts a plain text to decrypts an encrypted text back to plain using Caeser Cipher.
* 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
*
* Enter plain text:the brown fox jumped over the lazy dog
* Enter shift length:24
* Encrypted text:rfc zpmul dmv hskncb mtcp rfc jyxw bme
* Decrypted text:the brown fox jumped over the lazy dog.
*/
#include <iostream>
#include <string>
#include <sstream>
char caeser_encrypt(char c, int shift_length);
char caeser_decrypt(char c, int shift_length);
int main()
{
std::string plain_text, encrypted_text;
int shift_length;
std::cout << "Enter plain text:";
std::getline(std::cin, plain_text);
std::cout << "Enter shift length:";
std::cin >> shift_length;
std::stringstream ss;
unsigned int text_length = plain_text.length();
// Encrypt the text
//
for (unsigned int i = 0; i < text_length; ++i)
{
ss << caeser_encrypt(plain_text[i], shift_length);
}
encrypted_text = ss.str();
std::cout << "Encrypted text:" << encrypted_text << std::endl;
// Reset the stream
//
ss.str(std::string());
// Decrypt the text again
//
for (unsigned int i = 0; i < text_length; ++i)
{
ss << caeser_decrypt(encrypted_text[i], shift_length);
}
std::cout << "Decrypted text:" << ss.str() << std::endl;
return 0;
}
// Encrypts a character based on shift length
//
char caeser_encrypt(char c, int shift_length)
{
if (c >= 'A' && c <= 'Z')
{
return (((c - 'A' + shift_length) % 26) + 'A');
}
else if (c >= 'a' && c <= 'z')
{
return (((c - 'a' + shift_length) % 26) + 'a');
}
// In case of non-alpha characters, return as it is.
return c;
}
// Decrypts a character based on shift length
//
char caeser_decrypt(char c, int shift_length)
{
if (c >= 'A' && c <= 'Z')
{
return (((c - 'A' + (26 - shift_length)) % 26) + 'A');
}
else if (c >= 'a' && c <= 'z')
{
return (((c - 'a' + (26 - shift_length)) % 26) + 'a');
}
// In case of non-alpha characters, return as it is.
return c;
}