CFree注册码

用户名:tianfang
??邮箱:quart@163.com
??注册码:2NnUqd3shO2agta0xNjcusfK1LXO

凯撒密码加解密测试代码

新版
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
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <regex>
using namespace std;

regex reg_aTOz("^[a-z]$");
regex reg_AtoZ("^[A-Z]$");
regex reg_num("^[0-9]$");
regex reg_lb("^[\u4e00-\u9fa5]+$");

void ui()
{
cout << "\t\t\t—————————————— 循环移位密码 Caesar Normal Stu ——————————————" << endl;
cout << "" << endl;
}

char encryptionOrDecryptionChanges(char originalChar, char singleChar, int key, int isDecryption = 0)
{
key = abs(key);
string singleStr(1,singleChar);
// 用 isalnum() 判断是否为字符或数字,相当于 isalpha(string) || isdigit(number)
if (!isalnum(originalChar)) {
if((singleChar >= 32 && singleChar <= 47) || (singleChar >= 58 && singleChar <= 64)) return singleChar + 40;
if(singleChar >= 91 && singleChar <= 96) return singleChar + 15;
if(singleChar >= 123 && singleChar <= 126) return singleChar - 10;
if((singleChar >= 72 && singleChar <= 87) || (singleChar >= 98 && singleChar <= 104)) return singleChar - 40;
if(singleChar >= 106 && singleChar <= 111) return singleChar - 15;
if(singleChar >= 113 && singleChar <= 116) return singleChar + 10;
}
// regex_match(string, reg)
if (regex_match(singleStr, reg_AtoZ))
{
if(isDecryption != 0) return 'A' + (singleChar - 'A' - (key % 26) + 26) % 26;
return 'A' + (singleChar - 'A' + (key % 26)) % 26;
}
else if (regex_match(singleStr, reg_aTOz))
{
if(isDecryption != 0) return 'a' + (singleChar - 'a' - (key % 26) + 26) % 26;
return 'a' + (singleChar - 'a' + (key % 26)) % 26;
}
else if (regex_match(singleStr, reg_num))
{
if(isDecryption != 0) return '0' + (singleChar - '0' - (key % 10) + 10) % 10;
return '0' + (singleChar - '0' + (key % 10)) % 10;
}

// if (singleChar >= 'A' && singleChar <= 'Z')
// {
// if(isDecryption != 0) return 'A' + (singleChar - 'A' - (key % 26) + 26) % 26;
// return 'A' + (singleChar - 'A' + (key % 26)) % 26;
// }
// else if (singleChar >= 'a' && singleChar <= 'z')
// {
// if(isDecryption != 0) return 'a' + (singleChar - 'a' - (key % 26) + 26) % 26;
// return 'a' + (singleChar - 'a' + (key % 26)) % 26;
// }
// else if (singleChar >= '0' && singleChar <= '9')
// {
// if(isDecryption != 0) return '0' + (singleChar - '0' - (key % 10) + 10) % 10;
// return '0' + (singleChar - '0' + (key % 10)) % 10;
// }
// else return singleChar;
return ' ';
}

void caesar()
{
char plaintext[1000] = {}; // 明文
char ciphertext[1000] = {}; // 密文
int i, len, encryptionKey, decryptionKey; // 加密密钥:encryptionKey,解密密钥:decryptionKey

ui();

// 101个字符测试
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
while (true)
{
cout << "输入明文(<1000 个字符):";
cin >> plaintext;

if(strlen(plaintext) < 1000) break;
cout << "当前长度为:" << strlen(plaintext) << " 字符最大长度为:999,请重新输入" << endl;
}

len = strlen(plaintext);
// 指向 plaintext 首地址的指针
char* pointerPlaintext = plaintext;
cout << "查看输入的明文:";
// 指针在 plaintext 中移动(遍历),逐个输出其中字符
for (i = 0; i < len; i++) cout << *pointerPlaintext++;

cout << endl
<< "开启加密,输入密钥(任意数):";
cin >> encryptionKey;

// 加密算法:遍历,转数字,移位,模运算
for (i = 0; i < len; i++) ciphertext[i] = encryptionOrDecryptionChanges(plaintext[i], plaintext[i], encryptionKey);

cout << "完成加密,密文为:";
for (i = 0; i < len; i++) cout << ciphertext[i];

cout << endl;

// mingWen[] 与 plaintext[] 需进行对比
char mingWen[1000] = {};

cout << "进行解密,输入密钥(需要和加密密钥一致):";
cin >> decryptionKey;

// 加解密密钥一致是前提
if (encryptionKey == decryptionKey)
{
// 解密算法:遍历,转数字,逆向移位,模运算
for (i = 0; i < len; i++) mingWen[i] = encryptionOrDecryptionChanges(plaintext[i], ciphertext[i], decryptionKey, 1);

// 检查解密后的明文 mingWen 与输入时的明文 plaintext 是否完全一致
string strP = plaintext, strM = mingWen;
// str1.compare(str2),如果返回值为0则表示两个字符串相等
if (strP.compare(strM) == 0)
{
cout << "??????? 完成解密,明文为:";
for (i = 0; i < len; i++) cout << mingWen[i];
}
else cout << "解密失败,因解密后明文与输入时明文不一致!!";
cout << endl;
}
else cout << "加解密密钥不一致,解密失败!" << endl;
}

int main()
{
while (1) caesar();
return 0;
}
旧版
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
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <regex>
using namespace std;

regex reg_aTOz("^[a-z]$");
regex reg_AtoZ("^[A-Z]$");
regex reg_num("^[0-9]$");
regex reg_lb("^[\u4e00-\u9fa5]+$");

void ui()
{
cout << "\t\t\t—————————————— 循环移位密码 Caesar Normal Stu ——————————————" << endl;
cout << "" << endl;
}

char encryptionOrDecryptionChanges(char singleChar,int key, int isDecryption = 0)
{
string singleStr(1,singleChar);
// regex_match(sC, ex)
if (regex_match(singleStr, reg_AtoZ))
{
if(isDecryption != 0) return 'A' + (singleChar - 'A' - (key % 26) + 26) % 26;
return 'A' + (singleChar - 'A' + (key % 26)) % 26;
}
else if (regex_match(singleStr, reg_aTOz))
{
if(isDecryption != 0)
return 'a' + (singleChar - 'a' - (key % 26) + 26) % 26;
return 'a' + (singleChar - 'a' + (key % 26)) % 26;
}
else if (regex_match(singleStr, reg_num))
{
if(isDecryption != 0)
return '0' + (singleChar - '0' - (key % 10) + 10) % 10;
return '0' + (singleChar - '0' + (key % 10)) % 10;
}
else
return singleChar;

// if (singleChar >= 'A' && singleChar <= 'Z')
// {
// if(isDecryption != 0) return 'A' + (singleChar - 'A' - (key % 26) + 26) % 26;
// return 'A' + (singleChar - 'A' + (key % 26)) % 26;
// }
// else if (singleChar >= 'a' && singleChar <= 'z')
// {
// if(isDecryption != 0) return 'a' + (singleChar - 'a' - (key % 26) + 26) % 26;
// return 'a' + (singleChar - 'a' + (key % 26)) % 26;
// }
// else if (singleChar >= '0' && singleChar <= '9')
// {
// if(isDecryption != 0) return '0' + (singleChar - '0' - (key % 10) + 10) % 10;
// return '0' + (singleChar - '0' + (key % 10)) % 10;
// }
// else
// return singleChar;
}

void caesar()
{
char plaintext[100] = {}; // 明文
char ciphertext[100] = {}; // 密文
int i, len, encryptionKey, decryptionKey; // 加密密钥:encryptionKey,解密密钥:decryptionKey

ui();

// 101个字符测试
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
while (true)
{
cout << "输入明文(<100 个字符):";
cin >> plaintext;

if(strlen(plaintext) < 100) break;
cout << "当前长度为:"
<< strlen(plaintext)
<< " 字符最大长度为:99,请重新输入"
<< endl;
}

len = strlen(plaintext);
// 指向 plaintext 首地址的指针
char* pointerPlaintext = plaintext;
cout << "查看输入的明文:";
// 指针在 plaintext 中移动(遍历),逐个输出其中字符
for (i = 0; i < len; i++)
cout << *pointerPlaintext++;

cout << endl
<< "开启加密,输入密钥(任意数):";
cin >> encryptionKey;

// 加密算法:遍历,转数字,移位,模运算
for (i = 0; i < len; i++)
{
ciphertext[i] = encryptionOrDecryptionChanges(plaintext[i], encryptionKey);
}

cout << "完成加密,密文为:";
for (i = 0; i < len; i++)
cout << ciphertext[i];

cout << endl;

// mingWen[] 与 plaintext[] 需进行对比
char mingWen[100] = {};

cout << "进行解密,输入密钥(需要和加密密钥一致):";
cin >> decryptionKey;

// 加解密密钥一致是前提
if (encryptionKey == decryptionKey)
{
// 解密算法:遍历,转数字,逆向移位,模运算
for (i = 0; i < len; i++)
{
mingWen[i] = encryptionOrDecryptionChanges(ciphertext[i], decryptionKey, 1);
}

// 检查解密后的明文 mingWen 与输入时的明文 plaintext 是否完全一致
string strP = plaintext, strM = mingWen;
// str1.compare(str2),如果返回值为0则表示两个字符串相等
if (strP.compare(strM) == 0)
{
cout << "??????? 完成解密,明文为:";
for (i = 0; i < len; i++)
cout << mingWen[i];
}
else
cout << "解密失败,因解密后明文与输入时明文不一致!!";

cout << endl;
}
else
cout << "加解密密钥不一致,解密失败!" << endl;
}

int main()
{
while (1)
{
caesar();
}
return 0;
}