-
Notifications
You must be signed in to change notification settings - Fork 0
/
encipher.cpp
46 lines (42 loc) · 1.2 KB
/
encipher.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
// This code produces a cipher text from plain text with random key
#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main()
{
srand (time(NULL));
// plain text all lowercase
freopen("test2.txt","r",stdin);
// cipher text will be produced
freopen("test.txt","w",stdout);
// randomly generate a key
random_shuffle(alphabet.begin(),alphabet.end());
// generate key mappings
map<char,char> key;
for(int i=0;i<26;++i)
{
key[char('A' + i)] = alphabet[i];
cerr<<char('A' + i)<<" "<<alphabet[i]<<endl; // output log
}
// get the key on a std::string
string s, str = "";
while(getline(cin,s))
{
str += s;
}
cerr<<"KEY : "<<alphabet<<endl; // log output
// covert to uperacase
transform(str.begin(),str.end(),str.begin(), ::toupper);
// encode only the letters & numbers
for(int i=0;i<sz(str);++i)
{
if((str[i] >= '0' && str[i] <= '9') || (str[i]>='A' && str[i] <= 'Z') || (str[i]>='a' && str[i]<='z'))
{
str[i] = key[str[i]];
}
}
cerr<<str<<endl;
// output the cipher text
cout<<str;
}