-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
51 lines (33 loc) · 1.05 KB
/
decrypt.cpp
File metadata and controls
51 lines (33 loc) · 1.05 KB
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
#include <iostream>
#include <string>
#ifdef WIN
#include <cstdlib>
#define PAUSE system("pause");
#elif
#define PAUSE
#endif
using namespace std;
void decode(string &message, const string& key){ // Defining function for decrypting.
for(int i = 0; i < message.size(); i++)
message[i] = ((message[i] - key[i % key.size()] + 95) % 95) + 32;
/*
* Steps for decryption:
*
* 1- substract the ascii equivalent of key character from the message character.
* 2- add 95 to avoid modulo vs congruency problems (we are only working on positive values).
* 3- get the result by congruence of 95.
* 4- Add 32 to get the ascii equivalent.
* */
}
int main(){
cout << "Enter the key:\t"; // Getting the encrypting key.
string key;
getline(cin, key);
cout << "Now enter the message to decrypt:\n";
string message;
getline(cin, message); // Getting the message from the user.
decode(message, key); // Call our function.
cout << "This is the decrypted message:\n" << message << endl; // Print the decrypted message.
PAUSE
return 0;
}