设计思路

使用面向对象编程设计,包含Crypto,FileHandler,Menu类。

Crypto类

包含两个方法,Encrypt方法与Decrypt方法。

Encrypt方法

实现加密。

Decrypt方法

实现解密。

FileHandler类

包含readFile和writeFile两个方法。

readFile方法

读取文件并存储在一个string变量中。

writeFile方法

将string变量中的内容存储到文件中。

包含menuInit一个方法,主要承担程序运行初期的菜单显示。

实现步骤

初始化项目

git clone

创建HelloCV文件夹,配置好git后从github中clone下之前的库。

配置git remote

编写CMakelists

其中要求生成文件名称为CryptoTool,故使用set_target_properties指令。

生成项目

应用“代码外构建”的思想,创建build目录,并在build目录中使用cmake构建。

第一版

代码

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
#include <iostream>
#include <fstream>
using namespace std;
class Crypto {
public:
int key;
string plaintext;
string Encrypt(string plaintext) const {
for (int i=0;i<plaintext.length();i++) {
if (plaintext[i]>='A'&&plaintext[i]<='z') plaintext[i]+=key;

}
}
return plaintext;
}
string Decrypt(string plaintext) const {

for (int i=0;i<plaintext.length();i++) {
if (plaintext[i]>='A'&&plaintext[i]<='z') plaintext[i]-=key;
}
return plaintext;
}
};
class FileHandler {
public:
string file;
string readFile() {
string filecontent;
ifstream infile;
infile.open(file);
infile>>filecontent;
return filecontent;
}
void writeFile(string filecontent) {
ofstream outfile;
outfile.open(file);
outfile<<filecontent;
}
};
class Menu {
public:
int menuNum;
void menuInit() {
cout<<"---菜单---"<<endl;
cout<<"【1】文本加密"<<endl;
cout<<"【2】文本解密"<<endl;
cout<<"【3】文件加密"<<endl;
cout<<"【4】文件解密"<<endl;
cout<<"请输入选项:";
cin>>menuNum;
cin.ignore();
}
};

int main() {
int key;
string originText;
Crypto newCrypto;


Menu Menu;
Menu.menuInit();
if (Menu.menuNum==1) {
cout<<"请输入要加密的文本:";
getline(cin,originText);
cout<<"请输入密钥:";
cin>>key;
newCrypto.key=key;
string encryptedText = newCrypto.Encrypt(originText);
cout<<"加密结果:"<<encryptedText<<endl;
return 0;
}

if (Menu.menuNum==2) {
cout<<"请输入要解密的文本:";
getline(cin,originText);
cout<<"请输入密钥:";
cin>>key;
newCrypto.key=key;
string decryptedText = newCrypto.Decrypt(originText);
cout<<"解密结果:"<<decryptedText<<endl;
return 0;
}

if (Menu.menuNum==3) {
string fileAddr;
cout<<"请输入要加密的文件路径:";
getline(cin,fileAddr);
cout<<"请输入密钥:";
cin>>key;
FileHandler newFile;
newFile.file=fileAddr;
originText = newFile.readFile();
newCrypto.key=key;
string encryptedText = newCrypto.Encrypt(originText);
newFile.file="encrypted.txt";
newFile.writeFile(encryptedText);
cout<<"加密结果已保存至encrypted.txt"<<endl;
return 0;
}

if (Menu.menuNum==4) {
string fileAddr;
cout<<"请输入要解密的文件路径:";
getline(cin,fileAddr);
cout<<"请输入密钥:";
cin>>key;
FileHandler newFile;
newFile.file=fileAddr;
originText = newFile.readFile();
newCrypto.key=key;
string decryptedText = newCrypto.Decrypt(originText);
newFile.file="decrypted.txt";
newFile.writeFile(decryptedText);
cout<<"解密结果已保存至decrypted.txt"<<endl;
return 0;
}
}

存在问题

  1. fstream操作流不熟悉。实现方式为将file中的内容存储到一个string中,但是只会将第一个空格前的内容存储到string中,类似cin输入。
  2. 凯撒密码逻辑有问题。字母移位可能会存在超出限制,以至于成为特殊符号,而解密时将不会恢复。

预期解决方案

  1. 查询是否有类似getline(cin,a)的解决方案来解决文件内容存储的问题。
  2. 修改加密逻辑。由于大写字母Z与小写字母a之间ascii码相差7位,将实现大小写字母独立进行移位操作。例如Z后一位是A,z后一位是a。
  3. 考虑是否对待加密内容套一层base64加密。

第二版(加解密部分)

代码以及遇到的问题

修改加密逻辑如下:

1
2
3
4
5
6
7
8
9
10
11
string Encrypt(string plaintext) {
key%=26;//限定key范围1-25
for (int i=0;i<plaintext.length();i++) {
if (plaintext[i]>='A'&&plaintext[i]<='Z') {
if ((plaintext[i]+=key) > 'Z') plaintext[i]-=26;
}else if (plaintext[i]>='a'&&plaintext[i]<='z') {
if ((plaintext[i]+=key) > 'z') plaintext[i]-=26;
}
}
return plaintext;
}

然而,当输入z,以1-5为密钥时没有问题,以6为密钥时又出现乱码。

原因

z 的 ASCII 值是 122。

加密时,plaintext[i] += key 会使其值变为 122 + 6 = 128。

由于未正确处理超出 ‘z’ 的情况,plaintext[i] -= 26 不会被执行,因为 128 已超出 ‘z’ 的范围。

解决方案

每次移位后检查是否超出z范围

1
2
3
4
5
6
7
for (char &ch : plaintext) {
if (ch >= 'A' && ch <= 'Z') {
ch = 'A' + (ch - 'A' + key) % 26;
} else if (ch >= 'a' && ch <= 'z') {
ch = 'a' + (ch - 'a' + key) % 26;
}
}

第二版(文件读取部分)

代码及遇到的问题

1
2
3
4
5
6
7
8
9
10
11
12
string readFile() {
//string filecontent;
ifstream infile;
infile.open(file);
//infile>>filecontent;
if (!infile) return {};
string filecontent, line;
while (getline(infile, line)) {//依然使用getline
filecontent += line + '\n'; // 保留换行符
}
return filecontent;
}

依然使用getline逐行读取,并追加到变量中。

然后,输出文件没有内容?

解决方案

在writeFile方法后增加flush,刷新缓存区。

1
2
outfile.flush();
outfile.close();

最终代码

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <fstream>
using namespace std;
class Crypto {
public:
int key;
string plaintext;
//优化加密逻辑,大小写单独加密,并解决出现非字母符号问题
string Encrypt(string plaintext) {
key=(key%26+26)%26;//限定key范围1-25且为正
/*for (int i=0;i<plaintext.length();i++) {
if (plaintext[i]>='A'&&plaintext[i]<='Z') {
if ((plaintext[i]+=key) > 'Z') plaintext[i]-=26;
}else if (plaintext[i]>='a'&&plaintext[i]<='z') {
if ((plaintext[i]+=key) > 'z') plaintext[i]-=26;
}
}*/
for (char &ch : plaintext) {
if (ch >= 'A' && ch <= 'Z') {
ch = 'A' + ((ch - 'A' + key) % 26+26)%26;//这样写就不会出现越界的问题,对26取余保证是正数
} else if (ch >= 'a' && ch <= 'z') {
ch = 'a' + ((ch - 'a' + key) % 26+26)%26;
}
}
return plaintext;
}
string Decrypt(string plaintext) {
key=(key%26+26)%26;//限定key范围1-25且为正
/*for (int i=0;i<plaintext.length();i++) {
if (plaintext[i]>='A'&&plaintext[i]<='Z') {
if ((plaintext[i]-=key) < 'A') plaintext[i]+=26;
}else if (plaintext[i]>='a'&&plaintext[i]<='z') {
if ((plaintext[i]-=key) < 'a') plaintext[i]+=26;
}
}*/
for (char &ch : plaintext) {
if (ch>='A' && ch<='Z') {
ch = 'A' + ((ch - 'A' - key)%26+26)%26;
} else if (ch>='a' && ch <='z') {
ch = 'a' + ((ch - 'a' - key)%26+26)%26;
}
}
return plaintext;
}
};
class FileHandler {
public:
string file;
string readFile() {
//string filecontent;
ifstream infile;
infile.open(file);
//infile>>filecontent;
if (!infile) return {};
string filecontent, line;
while (getline(infile, line)) {//依然使用getline
filecontent += line + '\n'; // 保留换行符
}
return filecontent;
}
void writeFile(string filecontent) {
ofstream outfile;
outfile.open(file);
outfile<<filecontent;
outfile.flush();
outfile.close();
}
};
class Menu {
public:
int menuNum;
void menuInit() {
cout<<"---菜单---"<<endl;
cout<<"【1】文本加密"<<endl;
cout<<"【2】文本解密"<<endl;
cout<<"【3】文件加密"<<endl;
cout<<"【4】文件解密"<<endl;
cout<<"请输入选项:";
cin>>menuNum;
cin.ignore();
}
};

int main() {
int key;
string originText;
Crypto newCrypto;


Menu Menu;
Menu.menuInit();
if (Menu.menuNum==1) {
cout<<"请输入要加密的文本:";
getline(cin,originText);
cout<<"请输入密钥:";
cin>>key;
newCrypto.key=key;
string encryptedText = newCrypto.Encrypt(originText);
cout<<"加密结果:"<<encryptedText<<endl;
return 0;
}

if (Menu.menuNum==2) {
cout<<"请输入要解密的文本:";
getline(cin,originText);
cout<<"请输入密钥:";
cin>>key;
newCrypto.key=key;
string decryptedText = newCrypto.Decrypt(originText);
cout<<"解密结果:"<<decryptedText<<endl;
return 0;
}

if (Menu.menuNum==3) {
string fileAddr;
cout<<"请输入要加密的文件路径:";
getline(cin,fileAddr);
cout<<"请输入密钥:";
cin>>key;
FileHandler newFile;
newFile.file=fileAddr;
originText = newFile.readFile();
newCrypto.key=key;
string encryptedText = newCrypto.Encrypt(originText);
newFile.file="encrypted.txt";
newFile.writeFile(encryptedText);
cout<<"加密结果已保存至encrypted.txt"<<endl;
return 0;
}

if (Menu.menuNum==4) {
string fileAddr;
cout<<"请输入要解密的文件路径:";
getline(cin,fileAddr);
cout<<"请输入密钥:";
cin>>key;
FileHandler newFile;
newFile.file=fileAddr;
originText = newFile.readFile();
newCrypto.key=key;
string decryptedText = newCrypto.Decrypt(originText);
newFile.file="decrypted.txt";
newFile.writeFile(decryptedText);
cout<<"解密结果已保存至decrypted.txt"<<endl;
return 0;
}
}