-
Notifications
You must be signed in to change notification settings - Fork 1
/
test1.cpp
118 lines (100 loc) · 2.68 KB
/
test1.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
//
// Created by 李文博 on 2018-12-20.
//
#include <iostream>
#include <fstream>
#include <string>
#include "common.h"
using namespace std;
string encryptfile="data/1/encrypt.txt";
string decryptfile="data/1/decrypt.txt";
string output="data/1/answer.txt";
int encryption(int k1, int k2,int mod,int m){
return quick_mod(k1+k2*m,1,mod);
}
int decryption(int k1,int k2,int mod,int m){
return quick_pow(anti_mod(k2,mod)*(m-k1),1,mod);
}
bool rw_file(int k1, int k2,bool is_encrypt,string infile,string outfile){
ifstream ifile;
ifile.open(infile,ios::in);
ofstream ofile;
ofile.open(outfile,ios::out);
if (!ifile.is_open()){
cout << "Can not find file!"<<endl;
return false;
}
while(!ifile.eof()){
string temp;
getline(ifile,temp);
for(int i = 0;temp[i]!='\0';i++){
if(temp[i]>='a'&&temp[i]<='z'){
temp[i]-='a';
if(is_encrypt)temp[i]=encryption(k1,k2,26,temp[i]);
else temp[i]=decryption(k1,k2,26,temp[i]);
temp[i]+='a';
}
else if(temp[i]>='A'&&temp[i]<='Z'){
temp[i]-='A';
if(is_encrypt)temp[i]=encryption(k1,k2,26,temp[i]);
else temp[i]=decryption(k1,k2,26,temp[i]);
temp[i]+='A';
}
}
ofile<<temp<<endl;
}
ifile.close();
ofile.close();
return true;
}
bool cal_alp(string filename){
int alp[256];
memset(alp,0, sizeof(alp));
ifstream ifile;
ifile.open(filename,ios::in);
if (!ifile.is_open()){
cout << "Can not find file!"<<endl;
return false;
}
int sum = 0;
while(!ifile.eof()){
string temp;
getline(ifile,temp);
for(int i = 0;temp[i]!='\0';i++){
if(temp[i]>='a'&&temp[i]<='z'){
alp[temp[i]]++;
sum++;
}
else if(temp[i]>='A'&&temp[i]<='Z'){
alp[temp[i]]++;
sum++;
}
}
}
cout<<"字母 "<<"出现频率"<<endl;
for(int i=0;i<150;i++){
if(i >= 'a' && i <= 'z'){
cout<<char(i)<<" "<<double(alp[i])/sum<<endl;
}
else if( i >='A' && i <= 'Z'){
cout<<char(i)<<" "<<double(alp[i])/sum<<endl;
}
}
ifile.close();
return true;
}
int main(){
int k1=1;
int k2=3;
rw_file(k1,k2,true,encryptfile,decryptfile);
cout<<"加密成功!"<<endl;
rw_file(k1,k2,false,decryptfile,output);
cout<<"解密成功!"<<endl;
cal_alp(encryptfile);
cout<<endl;
cal_alp(decryptfile);
cout<<endl;
cal_alp(output);
cout<<endl;
return 0;
}