Professional Documents
Culture Documents
AIM:
I mplementing Simple text-based ciphers
CAESAR CIPHER
Code :
1 . #include <iostream>
2. #include <fstream>
3. #include <string>
4.
5. using namespace std;
6.
7. string encrypt(const string& text, int key) {
8. string encryptedText = "";
9.
10. for (char ch : text) {
11. if (isalpha(ch)) {
12. char base = isupper(ch) ? 'A' : 'a';
13. encryptedText += static_cast<char>((ch - base + key) % 26 + base);
14. } else {
15. encryptedText += ch;
16. }
17. }
18.
19. return encryptedText;
20.}
21.
22.string decrypt(const string& encryptedText, int key) {
23. return encrypt(encryptedText, 26 - key);
24.}
25.
26.void saveToFile(const string& data, const string& filename) {
27. ofstream file(filename, ios::app);
28. if (file.is_open()) {
29. file << data << endl;
30. file.close();
31. cout << "Data saved to " << filename << endl;
32. } else {
33. cerr << "Unable to open file: " << filename << endl;
34. }
3 5.}
36.
37.string readFromFile(const string& filename) {
38. string data;
39. ifstream file(filename);
40. if (file.is_open()) {
41. getline(file, data);
42. file.close();
43. } else {
44. cerr << "Unable to open file: " << filename << endl;
45. }
46. return data;
47.}
48.
49.int main() {
50. int key = 3;
51.
52. while (true) {
53.
54. cout << "Enter text to be encrypted (or 'exit' to quit): ";
55. string originalText;
56. getline(cin, originalText);
57.
58.
59. if (originalText == "exit") {
60. break;
61. }
62.
63. string encryptedText = encrypt(originalText, key);
64. cout << "Encrypted Text: " << encryptedText << endl;
65.
66.
67. saveToFile(originalText, "original_text.txt");
68. saveToFile(encryptedText, "encrypted_text.txt");
69.
70. cout << endl;
71. }
72.
73. return 0;
74.}
OUTPUT :
MULTIPLICATIVE CIPHER
Code :
1 . #include <iostream>
2. #include <fstream>
3. #include <string>
4.
5. using namespace std;
6.
7. int modInverse(int a, int m) {
8. a = a % m;
9. for (int x = 1; x < m; x++)
10. if ((a * x) % m == 1)
11. return x;
12. return 1;
13.}
14.
15.string encrypt(const string& text, int key) {
16. string encryptedText = "";
17.
18. for (char ch : text) {
19. if (isalpha(ch)) {
20. char base = isupper(ch) ? 'A' : 'a';
21. encryptedText += static_cast<char>((((ch - base) * key) % 26) + base);
22. } else {
23. encryptedText += ch;
24. }
25. }
2 6.
27. return encryptedText;
28.}
29.
30.string decrypt(const string& encryptedText, int key) {
31. int inverseKey = modInverse(key, 26);
32. return encrypt(encryptedText, inverseKey);
33.}
34.
35.void saveToFile(const string& data, const string& filename) {
36. ofstream file(filename, ios::app);
37. if (file.is_open()) {
38. file << data << endl;
39. file.close();
40. cout << "Data saved to " << filename << endl;
41. } else {
42. cerr << "Unable to open file: " << filename << endl;
43. }
44.}
45.
46.string readFromFile(const string& filename) {
47. string data;
48. ifstream file(filename);
49. if (file.is_open()) {
50. getline(file, data);
51. file.close();
52. } else {
53. cerr << "Unable to open file: " << filename << endl;
54. }
55. return data;
56.}
57.
58.int main() {
59. int key = 7;
60.
61. while (true) {
62. cout << "Enter text to be encrypted (or 'exit' to quit): ";
63. string originalText;
64. getline(cin, originalText);
65.
6 6. if (originalText == "exit") {
67. break;
68. }
69.
70. string encryptedText = encrypt(originalText, key);
71. cout << "Encrypted Text: " << encryptedText << endl;
72.
73. saveToFile(originalText, "my_plan_text.txt");
74. saveToFile(encryptedText, "my_encrypted_text.txt");
75.
76. cout << endl;
77. }
78.
79. return 0;
80.}
OUTPUT :
RAIL CIPHER
Code :
1 . # include <iostream>
2. #include <string>
3.
4. using namespace std;
5.
6. string encryptRailFence(const string& text, int rails) {
7. string encryptedText = "";
8. int len = text.length();
9.
1 0. for (int i = 0; i < rails; ++i) {
11. for (int j = i; j < len; j += rails * 2 - 2) {
12. encryptedText += text[j];
13. if (i > 0 && i < rails - 1 && j + 2 * (rails - i - 1) < len) {
14. encryptedText += text[j + 2 * (rails - i - 1)];
15. }
16. }
17. }
18.
19. return encryptedText;
20.}
21.
22.string decryptRailFence(const string& encryptedText, int rails) {
23. string decryptedText = "";
24. int len = encryptedText.length();
25. int cycleLen = 2 * (rails - 1);
26.
27. for (int i = 0; i < rails; ++i) {
28. for (int j = i; j < len; j += cycleLen) {
29. decryptedText += encryptedText[j];
30. if (i > 0 && i < rails - 1 && j + cycleLen - 2 * i < len) {
31. decryptedText += encryptedText[j + cycleLen - 2 * i];
32. }
33. }
34. }
35.
36. return decryptedText;
37.}
38.
39.int main() {
40. int rails = 3;
41. string plaintext, encryptedText, decryptedText;
42.
43. cout << "Enter text to be encrypted: ";
44. getline(cin, plaintext);
45.
46.
47. encryptedText = encryptRailFence(plaintext, rails);
48. cout << "Encrypted Text: " << encryptedText << endl;
49.
5 0.
51. decryptedText = decryptRailFence(encryptedText, rails);
52. cout << "Decrypted Text: " << decryptedText << endl;
53.
54. return 0;
55.}
OUTPUT :