|
| 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; |
0 commit comments