Batch file 凯撒密码CMD批,包括特殊字符、数字、小写和大写字符

Batch file 凯撒密码CMD批,包括特殊字符、数字、小写和大写字符,batch-file,encryption,Batch File,Encryption,我有 我的问题是,我需要扩展它并添加对数字、特殊字符(./-:etc)和大写字符的支持 @echo off setlocal enabledelayedexpansion set Alphabet=abcdefghijklmnopqrstuvwxyz set oText=http://randomwebsite.com/rand/206/index.html set offset=11 call :ENCRYPT %offset% "%oText%" echo KeySet=%KeySet%

我有

我的问题是,我需要扩展它并添加对数字、特殊字符(./-:etc)和大写字符的支持

@echo off
setlocal enabledelayedexpansion
set Alphabet=abcdefghijklmnopqrstuvwxyz
set oText=http://randomwebsite.com/rand/206/index.html
set offset=11

call :ENCRYPT %offset% "%oText%"
echo KeySet=%KeySet%
set eText=%outText%
call :DECRYPT %offset% "%eText%"
set dText=%outText%
echo Original text:  %oText%
echo Encrpted text:  %eText%
echo Decrypted text: %dText%
goto :EOF

:ENCRYPT
call :GETKEYSET %1
set _from=%Alphabet%
set _to=%KeySet%
set outText=%~2
for /l %%a in (0, 1, 25) do call :STUFFIT %%a
for /l %%a in (0, 1, 25) do call :PROCESS %%a  
goto :EOF

:DECRYPT
call :GETKEYSET %1
set _from=%KeySet%
set _to=%Alphabet%
set outText=%~2
for /l %%a in (0, 1, 25) do call :STUFFIT %%a
for /l %%a in (0, 1, 25) do call :PROCESS %%a 
goto :EOF

:STUFFIT
set fromChar=!_from:~%1,1!
set outText=!outText:%fromChar%=_%fromChar%!
goto :EOF

:PROCESS
set fromChar=!_from:~%1,1!
set toChar=!_to:~%1,1!
set outText=!outText:_%fromChar%=%toChar%!
goto :EOF

:GETKEYSET
set /a idx=%1 %% 26
set KeySet=!Alphabet:~%idx%!
if %idx%==0 goto :EOF
set KeySet=%KeySet%!Alphabet:~0,%idx%!

有什么想法吗?谢谢

好吧,我在检查了你的代码后的第一个想法是把它放进垃圾桶,然后再做一次

原始代码是各种风格的奇妙组合;一方面,加密/解密方法非常简单,并且严格遵循“手动”方法,因此程序效率非常低。另一方面,编程风格令人困惑:在某些情况下使用子程序参数,但子程序结果是通过全局变量返回的,而且变量名似乎是专门为混淆而选择的

大写字母的问题是它们必须与小写字母分开,因此要转换的字符串中的字符必须逐个处理

编辑:修改代码是为了管理带感叹号的字符串

@echo off
setlocal EnableDelayedExpansion

set "Alphabet=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set "lowCase=abcdefghijklmnopqrstuvwxyz"

set offset=11

set "oText=something^! is really^! great"
set maxLen=80

call :ENCRYPT %offset% "!oText!" eText=
call :DECRYPT %offset% "!eText!" dText=
echo Original text:  !oText!
echo Encrypted text: !eText!
echo Decrypted text: !dText!
goto :EOF


:ENCRYPT offset "input string" outVar=
setlocal DisableDelayedExpansion
set "inString=%~2"
setlocal EnableDelayedExpansion

for /L %%i in (0,1,61) do (
   set /A "i=(%%i + %1) %% 62"
   set c["!Alphabet:~%%i,1!"]=!i!
)
goto CompleteEncryptDecrypt


:DECRYPT offset "input string" outVar=
setlocal DisableDelayedExpansion
set "inString=%~2"
setlocal EnableDelayedExpansion

for /L %%i in (0,1,61) do (
   set /A "i=%%i - %1"
   if !i! lss 0 set /A i+=62
   set c["!Alphabet:~%%i,1!"]=!i!
)


:CompleteEncryptDecrypt

set "outVar="
for /L %%i in (0,1,%maxLen%) do (
   set "char=!inString:~%%i,1!"
   if defined char (
      for /F "delims=" %%c in ("!char!") do (
         if defined c["%%c"] (
            set j=!c["%%c"]!
            if "!lowCase:%%c=%%c!" neq "%lowCase%" set /A "j-=26"
            for /F %%j in ("!j!") do set "outVar=!outVar!!Alphabet:~%%j,1!"
         ) else (
            set "outVar=!outVar!!char!"
         )
      )
   )
)

( 
endlocal 
for /F "delims=" %%a in ("%outVar:!=^!%") do endlocal & set "%3=%%a"
)
exit /B
输出示例:

Original text:  something! is really! great
Encrypted text: 3zxp4styr! t3 2plww9! r2pl4
Decrypted text: something! is really! great

该程序中要做的一点是使用一个子例程获取字符串长度,而不是定义
set maxLen=80
行,然后删除相应的
if
命令。

将所需字符添加到
Alphabet
变量中,并增加/L循环的
中的值?@SomethingDark在这种情况下解密的输出是,如果我设法获得正确的字符串,它仍然只有小字符。这很好,谢谢,但还有一个问题,在我的原始字符串中是例如“something!isreally!great”,但它只适用于“something great”是的,但是您的原始代码也有这个问题<代码>:/
请参见我答案中的编辑!