Skip to content

Commit 68779bd

Browse files
committed
fix: caesar pwd
1 parent dc0c86c commit 68779bd

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

src/components/MainContent/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ const MainContent: React.FC<{
147147
direction='row'
148148
alignItems='center'
149149
justifyContent='space-between'
150+
spacing={1}
150151
sx={{
151152
position: 'relative',
152153
width: '100%',
@@ -175,6 +176,7 @@ const MainContent: React.FC<{
175176
sx={{
176177
borderRadius: '4px',
177178
height: '24px',
179+
minWidth: '80px',
178180
}}
179181
variant='outlined'
180182
startIcon={<FullscreenIcon />}

src/pages/caesar.tsx

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import React, { useState } from 'react';
2+
import { Container, Grid, TextField, Button, Box } from '@mui/material';
3+
import SendIcon from '@mui/icons-material/Send';
4+
import TranslateIcon from '@mui/icons-material/Translate';
5+
import MainContent from '@/components/MainContent';
6+
import alertActions from '@/components/Alert';
7+
8+
const encode = (text: string, pwdOffest: number) => {
9+
if (pwdOffest >= 26) pwdOffest = pwdOffest % 26;
10+
return Array.from(text)
11+
.map((item) => {
12+
const count = item.charCodeAt(0);
13+
if ((count >= 97 && count <= 122) || (count >= 65 && count <= 90)) {
14+
if (
15+
count + pwdOffest > 122 ||
16+
(count <= 90 && count + pwdOffest > 90)
17+
) {
18+
return String.fromCharCode(count + pwdOffest - 26);
19+
}
20+
return String.fromCharCode(count + pwdOffest);
21+
}
22+
return item;
23+
})
24+
.join('');
25+
};
26+
const decode = (pwd: string, pwdOffest: number) => {
27+
if (pwdOffest > 25) pwdOffest = pwdOffest % 25;
28+
return Array.from(pwd)
29+
.map((item) => {
30+
const count = item.charCodeAt(0);
31+
if ((count >= 97 && count <= 122) || (count >= 65 && count <= 90)) {
32+
if (count - pwdOffest < 65 || (count >= 97 && count - pwdOffest < 97)) {
33+
return String.fromCharCode(count - pwdOffest + 26);
34+
}
35+
return String.fromCharCode(count - pwdOffest);
36+
}
37+
return item;
38+
})
39+
.join('');
40+
};
41+
const CaesarCodeTranslator = () => {
42+
const [text, setText] = useState('');
43+
const [pwd, setPwd] = useState('');
44+
const [pwdOffest, setPwdOffest] = useState(3);
45+
46+
const onTextChange = (event: React.ChangeEvent<HTMLInputElement>) => {
47+
setText(event.target.value);
48+
};
49+
50+
const onPwdChange = (event: React.ChangeEvent<HTMLInputElement>) => {
51+
setPwd(event.target.value);
52+
};
53+
54+
const encodeToMorse = () => {
55+
try {
56+
const encoded = encode(text, pwdOffest);
57+
setPwd(encoded);
58+
} catch (error) {
59+
const err = error as Error;
60+
console.log(err);
61+
alertActions.error('加密失败!');
62+
}
63+
};
64+
65+
const decodeFromMorse = () => {
66+
try {
67+
const decoded = decode(pwd, pwdOffest);
68+
setText(decoded);
69+
} catch (error) {
70+
const err = error as Error;
71+
console.log(err);
72+
alertActions.error('解密失败!');
73+
}
74+
};
75+
76+
return (
77+
<MainContent>
78+
<>
79+
<TextField
80+
fullWidth
81+
label='密码位移数'
82+
placeholder='请输入密码位移的数值'
83+
value={pwdOffest}
84+
type='number'
85+
onChange={(e) => {
86+
setPwdOffest(e.target.value as any);
87+
}}
88+
inputProps={{
89+
style: { fontFamily: 'monospace' },
90+
}}
91+
sx={{ width: '200px', ml: 'auto' }}
92+
/>
93+
<Container maxWidth='lg'>
94+
<Box sx={{ my: 4 }}>
95+
<Grid container spacing={2} alignItems='center'>
96+
<Grid item xs={12} md={5}>
97+
<TextField
98+
fullWidth
99+
label='文本'
100+
placeholder='请输入要加密的文本'
101+
value={text}
102+
onChange={onTextChange}
103+
variant='outlined'
104+
multiline
105+
rows={10}
106+
inputProps={{
107+
style: { fontFamily: 'monospace' },
108+
}}
109+
/>
110+
</Grid>
111+
<Grid
112+
item
113+
xs={12}
114+
md={2}
115+
container
116+
direction='column'
117+
alignItems='center'
118+
justifyContent='space-around'
119+
>
120+
<Button
121+
variant='contained'
122+
onClick={encodeToMorse}
123+
startIcon={<SendIcon />}
124+
sx={{ mb: 2 }}
125+
>
126+
加密
127+
</Button>
128+
<Button
129+
variant='contained'
130+
onClick={decodeFromMorse}
131+
startIcon={<TranslateIcon />}
132+
>
133+
解密
134+
</Button>
135+
</Grid>
136+
<Grid item xs={12} md={5}>
137+
<TextField
138+
fullWidth
139+
label='凯撒密码'
140+
placeholder='请输入要解密的凯撒密码'
141+
value={pwd}
142+
onChange={onPwdChange}
143+
variant='outlined'
144+
multiline
145+
rows={10}
146+
inputProps={{
147+
style: { fontFamily: 'monospace' },
148+
}}
149+
/>
150+
</Grid>
151+
</Grid>
152+
</Box>
153+
</Container>
154+
</>
155+
</MainContent>
156+
);
157+
};
158+
159+
export default CaesarCodeTranslator;

src/utils/tools.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,12 @@ export const allTools: Tool[] = [
598598
key: [],
599599
subTitle: '乱序文字生成器可以打乱文字和段落顺序,保持数量和出现次数不变。',
600600
},
601+
{
602+
label: '凯撒密码在线加密解密',
603+
tags: [Tags.SECURITY],
604+
path: '/caesar',
605+
key: [],
606+
subTitle:
607+
'凯撒密码最早由古罗马军事统帅盖乌斯·尤利乌斯·凯撒在军队中用来传递加密信息,故称凯撒密码。此为一种位移加密手段,只对26个(大小写)字母进行位移加密,规则相当简单,容易被破解',
608+
},
601609
];

0 commit comments

Comments
 (0)