-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17-3.cpp
95 lines (87 loc) · 1.48 KB
/
17-3.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
#include <iostream>
#include <cstring>
using namespace std;
template<class T>
T max(T* t, int n) {
T m = 0;
for (int i = 0; i < n; i++) {
if (t[i] > m) {
m = t[i];
}
}
return m;
}
//只支持整数类型的加密类界面
template<class T>
class Cryption
{
private:
T ptxt[100]; //明文
T ctxt[100]; //密文
T key; //密钥
int len; //长度
public:
Cryption(T tk, T tt[], int n); //参数依次对应密钥、明文、长度
void encrypt(); //加密
void print() //打印,无需改造
{
int i;
for (i = 0; i < len - 1; i++)
{
cout << ctxt[i] << " ";
}
cout << ctxt[i] << endl;
}
};
template<class T>
Cryption<T>::Cryption(T tk, T tt[], int n) {
len = n;
memcpy(ptxt, tt, sizeof(T) * len);
key = tk;
memset(ctxt, '\0', sizeof(ctxt));
}
template<class T>
void Cryption<T>::encrypt() {
T maxValue = max(ptxt, len);
for (int i = 0; i < len; i++) {
ctxt[i] = maxValue - ptxt[i];
ctxt[i] += key;
}
}
//支持三种类型的主函数
int main()
{
int i;
int length; //长度
int ik, itxt[100];
double dk, dtxt[100];
char ck, ctxt[100];
//整数加密
cin >> ik >> length;
for (i = 0; i < length; i++)
{
cin >> itxt[i];
}
Cryption<int> ic(ik, itxt, length);
ic.encrypt();
ic.print();
//浮点数加密
cin >> dk >> length;
for (i = 0; i < length; i++)
{
cin >> dtxt[i];
}
Cryption<double> dc(dk, dtxt, length);
dc.encrypt();
dc.print();
//字符加密
cin >> ck >> length;
for (i = 0; i < length; i++)
{
cin >> ctxt[i];
}
Cryption<char> cc(ck, ctxt, length);
cc.encrypt();
cc.print();
return 0;
}