我有this code。
我的问题是,我需要扩展它并添加对数字,特殊字符(./-:等)和大写字符的支持
@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%!
请问有什么想法吗?感谢
答案 0 :(得分:1)
好吧,我在审核完您的代码之后的第一个想法就是将它放入垃圾桶并重新制作 ;-)
原始代码是一种奇特的风格组合;一方面,加密/解密方法非常简陋,并严格遵循"手册"方法,所以程序效率很低。另一方面,编程风格令人困惑:在某些情况下使用子程序参数,但子程序结果是通过全局变量返回的,变量名称似乎是专门用来混淆的!
大写字母的问题在于它们必须在单独的基础上与小写字母分开,因此要转换的字符串中的字符必须逐个处理。
编辑:修改了代码以管理带有感叹号的字符串。
@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
命令。