From 68779bdb102e01a3e302ff9d0aa3eb4356d0c1cd Mon Sep 17 00:00:00 2001
From: awesomeYG <993631441@qq.com>
Date: Sun, 18 Feb 2024 18:32:19 +0800
Subject: [PATCH] fix: caesar pwd
---
src/components/MainContent/index.tsx | 2 +
src/pages/caesar.tsx | 159 +++++++++++++++++++++++++++
src/utils/tools.ts | 8 ++
3 files changed, 169 insertions(+)
create mode 100644 src/pages/caesar.tsx
diff --git a/src/components/MainContent/index.tsx b/src/components/MainContent/index.tsx
index 9a9adb5..17a5694 100644
--- a/src/components/MainContent/index.tsx
+++ b/src/components/MainContent/index.tsx
@@ -147,6 +147,7 @@ const MainContent: React.FC<{
direction='row'
alignItems='center'
justifyContent='space-between'
+ spacing={1}
sx={{
position: 'relative',
width: '100%',
@@ -175,6 +176,7 @@ const MainContent: React.FC<{
sx={{
borderRadius: '4px',
height: '24px',
+ minWidth: '80px',
}}
variant='outlined'
startIcon={}
diff --git a/src/pages/caesar.tsx b/src/pages/caesar.tsx
new file mode 100644
index 0000000..cf2dc08
--- /dev/null
+++ b/src/pages/caesar.tsx
@@ -0,0 +1,159 @@
+import React, { useState } from 'react';
+import { Container, Grid, TextField, Button, Box } from '@mui/material';
+import SendIcon from '@mui/icons-material/Send';
+import TranslateIcon from '@mui/icons-material/Translate';
+import MainContent from '@/components/MainContent';
+import alertActions from '@/components/Alert';
+
+const encode = (text: string, pwdOffest: number) => {
+ if (pwdOffest >= 26) pwdOffest = pwdOffest % 26;
+ return Array.from(text)
+ .map((item) => {
+ const count = item.charCodeAt(0);
+ if ((count >= 97 && count <= 122) || (count >= 65 && count <= 90)) {
+ if (
+ count + pwdOffest > 122 ||
+ (count <= 90 && count + pwdOffest > 90)
+ ) {
+ return String.fromCharCode(count + pwdOffest - 26);
+ }
+ return String.fromCharCode(count + pwdOffest);
+ }
+ return item;
+ })
+ .join('');
+};
+const decode = (pwd: string, pwdOffest: number) => {
+ if (pwdOffest > 25) pwdOffest = pwdOffest % 25;
+ return Array.from(pwd)
+ .map((item) => {
+ const count = item.charCodeAt(0);
+ if ((count >= 97 && count <= 122) || (count >= 65 && count <= 90)) {
+ if (count - pwdOffest < 65 || (count >= 97 && count - pwdOffest < 97)) {
+ return String.fromCharCode(count - pwdOffest + 26);
+ }
+ return String.fromCharCode(count - pwdOffest);
+ }
+ return item;
+ })
+ .join('');
+};
+const CaesarCodeTranslator = () => {
+ const [text, setText] = useState('');
+ const [pwd, setPwd] = useState('');
+ const [pwdOffest, setPwdOffest] = useState(3);
+
+ const onTextChange = (event: React.ChangeEvent) => {
+ setText(event.target.value);
+ };
+
+ const onPwdChange = (event: React.ChangeEvent) => {
+ setPwd(event.target.value);
+ };
+
+ const encodeToMorse = () => {
+ try {
+ const encoded = encode(text, pwdOffest);
+ setPwd(encoded);
+ } catch (error) {
+ const err = error as Error;
+ console.log(err);
+ alertActions.error('加密失败!');
+ }
+ };
+
+ const decodeFromMorse = () => {
+ try {
+ const decoded = decode(pwd, pwdOffest);
+ setText(decoded);
+ } catch (error) {
+ const err = error as Error;
+ console.log(err);
+ alertActions.error('解密失败!');
+ }
+ };
+
+ return (
+
+ <>
+ {
+ setPwdOffest(e.target.value as any);
+ }}
+ inputProps={{
+ style: { fontFamily: 'monospace' },
+ }}
+ sx={{ width: '200px', ml: 'auto' }}
+ />
+
+
+
+
+
+
+
+ }
+ sx={{ mb: 2 }}
+ >
+ 加密
+
+ }
+ >
+ 解密
+
+
+
+
+
+
+
+
+ >
+
+ );
+};
+
+export default CaesarCodeTranslator;
diff --git a/src/utils/tools.ts b/src/utils/tools.ts
index 36765e1..fefc5b3 100644
--- a/src/utils/tools.ts
+++ b/src/utils/tools.ts
@@ -598,4 +598,12 @@ export const allTools: Tool[] = [
key: [],
subTitle: '乱序文字生成器可以打乱文字和段落顺序,保持数量和出现次数不变。',
},
+ {
+ label: '凯撒密码在线加密解密',
+ tags: [Tags.SECURITY],
+ path: '/caesar',
+ key: [],
+ subTitle:
+ '凯撒密码最早由古罗马军事统帅盖乌斯·尤利乌斯·凯撒在军队中用来传递加密信息,故称凯撒密码。此为一种位移加密手段,只对26个(大小写)字母进行位移加密,规则相当简单,容易被破解',
+ },
];