-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.html
More file actions
248 lines (219 loc) · 7.08 KB
/
Copy pathCaesarCipher.html
File metadata and controls
248 lines (219 loc) · 7.08 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>凯撒密码 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.shift-controls {
display: flex;
align-items: center;
gap: 10px;
margin: 15px 0;
}
.shift-controls label {
color: var(--accent-color);
}
.shift-controls input {
width: 80px;
padding: 5px;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>凯撒密码</h2>
<p>一种替换加密的技术,将明文中的各个字母按一定规律替换为其他字母</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 凯撒密码是一种最简单的加密方法之一</p>
<p>• 将字母表中的每个字母按照固定位数进行替换</p>
<p>• 例如,当偏移量为3时,A变为D,B变为E,以此类推</p>
</div>
<div class="shift-controls">
<label for="shiftValue">偏移量:</label>
<input
type="number"
id="shiftValue"
v-model.number="shiftValue"
min="0"
max="25"
@change="onShiftChange"
>
<button @click="decreaseShift">-</button>
<button @click="increaseShift">+</button>
</div>
<div>
<textarea
v-model="inputText"
placeholder="在此输入要加密或解密的文本..."
rows="8"
></textarea>
<div class="controls">
<button @click="encrypt">加密</button>
<button @click="decrypt">解密</button>
<button @click="clearText">清空</button>
<button @click="copyResult">复制结果</button>
</div>
<div class="result" v-if="result">
<h3>结果:</h3>
<p>{{ result }}</p>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref } = Vue;
createApp({
setup() {
const inputText = ref('');
const result = ref('');
const shiftValue = ref(3); // 默认偏移量为3
const caesarCipher = (text, shift, decrypt = false) => {
if (!text) return '';
// 如果是解密,则将偏移量取反
if (decrypt) {
shift = -shift;
}
// 确保偏移量在0-25范围内
shift = ((shift % 26) + 26) % 26;
return text.split('').map(char => {
// 检查是否是字母
if (char >= 'a' && char <= 'z') {
// 处理小写字母
const charCode = char.charCodeAt(0) - 'a'.charCodeAt(0);
const shiftedCode = (charCode + shift) % 26;
return String.fromCharCode(shiftedCode + 'a'.charCodeAt(0));
} else if (char >= 'A' && char <= 'Z') {
// 处理大写字母
const charCode = char.charCodeAt(0) - 'A'.charCodeAt(0);
const shiftedCode = (charCode + shift) % 26;
return String.fromCharCode(shiftedCode + 'A'.charCodeAt(0));
} else {
// 非字母字符不变
return char;
}
}).join('');
};
const encrypt = () => {
result.value = caesarCipher(inputText.value, shiftValue.value);
};
const decrypt = () => {
result.value = caesarCipher(inputText.value, shiftValue.value, true);
};
const clearText = () => {
inputText.value = '';
result.value = '';
};
const copyResult = async () => {
if (result.value) {
try {
await navigator.clipboard.writeText(result.value);
alert('已复制到剪贴板!');
} catch (err) {
console.error('复制失败:', err);
// 降级方案
const textArea = document.createElement('textarea');
textArea.value = result.value;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
alert('已复制到剪贴板!');
}
}
};
const decreaseShift = () => {
shiftValue.value = ((shiftValue.value - 1 + 26) % 26);
};
const increaseShift = () => {
shiftValue.value = ((shiftValue.value + 1) % 26);
};
const onShiftChange = () => {
// 确保偏移量在0-25范围内
if (shiftValue.value < 0) {
shiftValue.value = 0;
} else if (shiftValue.value > 25) {
shiftValue.value = 25;
}
};
return {
inputText,
result,
shiftValue,
encrypt,
decrypt,
clearText,
copyResult,
decreaseShift,
increaseShift,
onShiftChange
};
}
}).mount('#app');
</script>
</body>
</html>