-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.cpp
More file actions
53 lines (35 loc) · 1.28 KB
/
encrypt.cpp
File metadata and controls
53 lines (35 loc) · 1.28 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
52
53
#include <iostream>
#include <string>
#ifdef WIN
#include <cstdlib>
#define PAUSE system("pause");
#elif
#define PAUSE
#endif
using namespace std;
void code(string &message,const string& key){ // fonction made to encrypt a message;
for(int i = 0; i < message.size(); i++) // looping for every character of the message.
message[i] = ((message[i] + key[i % key.size()] - 64) % 95) + 32; // main trnsformation.
/*
* Steps for encryption:
*
* 1- get the ascii equivalent of the message character and substart 32 to remove unprinting characters.
* 2- get the ascii equivalent of the key character and substract 32 for the same reason.
* 3- Add the two integers and obtain the congruent result by 95.
* 4- Add 32 to the result to have an ascii valid character.
*
* Note: characters we are working on are from 32 to 126 (95 characters).
* */
}
int main(){
cout << "Enter the key:\t"; // Obtain the encryptor key from the user.
string key;
getline(cin, key);
cout << "Enter the message to encrypt:\t"; // inputing the message.
string message;
getline(cin, message);
code(message, key); // encrypting via our defined fuction.
cout << "This is your encrypted message:\n" << message << endl; // showing out the encrypted message.
PAUSE
return 0;
}