This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCipher Codes.cpp
121 lines (114 loc) · 2.7 KB
/
Cipher Codes.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Caesar Cipher by ProxyHydra (https://github.com/ProxyHydra)
// Declaring header files
#include <iostream>
#include <string.h>
using namespace std;
void main()
{
start:
char message[1000]; // maximum permissible text - 1000 chars. To increase size of message, increase values of message[x] at line 10 and 13.
int key; // key = the number of letters to shift ahead
cout << "Enter message to encrypt/decrypt\n\n--> ";
cin.ignore(2, '\n'); //prevents the cin.getline skip when looping
cin.getline(message, 1000); // inputs message from user
system("cls");
cout << "Message: " << message << endl;
cout << endl << "Enter key: ";
cin >> key; // inputs key from user
int length = strlen(message); // counts the number of char in message and stores in 'length'
system("cls");
cout << "Message: " << message << "\nkey: " << key << endl;
goto choice;
choice:
int choice;
cout << "\nEnter your choice: \n1. Encrypt\n2. Decrypt\n\n--> ";
cin >> choice; // inputs choice to encrypt or decrypt from user
goto sort;
sort:
if (choice == 1)
{
char ch;
for (int i = 0; message[i] != '\0'; i++)
{
ch = message[i];
if (ch >= 'a' && ch <= 'z') //Lowercase Encryption
{
ch = ch + key;
if (ch>'z')
{
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z') //Uppercase Encryption
{
ch = ch + key;
if (ch > 'Z')
{
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
cout << "Encrypted message: " << message << endl << endl;
system("pause");
goto exit;
}
else if (choice == 2)
{
char ch;
for (int i = 0; message[i] != '\0'; i++)
{
ch = message[i];
if (ch >= 'a' && ch <= 'z') //Lowercase Decryption
{
ch = ch - key;
if (ch > 'z')
{
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z') //Uppercase Decryption
{
ch = ch - key;
if (ch > 'Z')
{
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
cout << "Decrypted message: " << message << endl << endl;
system("pause");
goto exit;
}
else
{
system("cls");
cout << "Invalid input entered. Please try again using '1' or '2'\n\n";
goto choice;
}
exit:
system("cls");
cout << "Would you like to use the cipher service again? (y/n)\n\n--> ";
char askexit;
cin >> askexit;
if (askexit == 'n' || askexit == 'N')
{
system("cls");
cout << "Thank you for using the cipher.\n\n";
exit(0);
}
else if (askexit == 'y' || askexit == 'Y')
{
system("cls");
goto start;
}
else
{
system("cls");
cout << "\nInvalid input entered. Please try again using 'y' or 'n'\n\n";
goto exit;
}
}