本帖最后由 0000 于 2017-2-2 15:53 编辑
批处理文本文件加解密器(带密码)
注:加密很长的中文文件时可能会不稳定。
算法有点长,请耐心浏览
已补上2楼的坑,请放心使用
加密:
- @echo off
- title 纯文本加解密工具1.0(加密)
- rem 退格符
- rem 标记感叹号
- set exc=!
- rem 标记引号
- set quo="
- rem 标记caret号
- set car=^^
- setlocal ENABLEDELAYEDEXPANSION
- rem 标记大写字母
- set CAP=ABCDEFGHIJKLMNOPQRSTUVWXYZ
- for /l %%n in (0,1,25) do (set CAP_!CAP:~%%n,1!=!CAP:~%%n,1!)
-
- echo 纯文本加解密工具1.0(加密) BY 0000
- :setfile
- set /p file=文件名称:
- if "!file!"=="" (echo 无效的文件名&goto setfile)
- set file=!file:"=!
- if "!file!"=="" (echo 无效的文件名&goto setfile)
- echo "!file!"|findstr /R /C:"[*?]">nul&&(echo 无效的文件名&goto setfile)
- set file_1=!file:~-1!
- if "!file_1!"=="/" (echo 无效的文件名&goto setfile)
- if "!file_1!"=="\" (echo 无效的文件名&goto setfile)
- dir /a-d "!file!">nul 2>nul||(echo 无效的文件名&goto setfile)
- dir /a-d "!file!\">nul 2>nul&&(echo 无效的文件名&goto setfile)
- :setpswd
- call :inputpswd 6 *
- rem 获取密码内容
- set /a pswd_a=1%pswd:~0,3%-900
- set /a pswd_b=1%pswd:~3,3%-900
-
- call :getfile2
-
- set /p =分析中. . . *****/*****( 0%%)<nul
- call :getline "!file!"
-
- set ln_n=0
- for /f "usebackq tokens=* eol= delims=" %%L in ($tmp$.txt) do (
- set /a ln_n=!ln_n!+1
- set ln_!ln_n!=%%L
- )
- del $tmp$.txt
- rem 去引号
- for /l %%n in (1,1,%ln_n%) do (
- set "ln_%%n=!ln_%%n:~1,-1!"
- set "ln_%%n=!ln_%%n:""="!"
- )
-
-
- set _n=1
- set ln=!ln_1!
- set chars=
- set chars_n=0
-
- :getchars
- if %_n% gtr %ln_n% goto getc0
- if "!ln!"=="" goto getc1
- set lna=!ln:~0,1!
- set ln=!ln:~1!
- rem 先考虑引号,感叹号,等号和冒号
- if "!lna!" == "!quo!" (
- rem 判断字母是否存在
- if not "!char_qu!"=="-1" (
- set chars=!chars!!quo!
- set /a chars_n=!chars_n!+1
- rem 字母已存在标志
- set char_qu=-1
- )
- goto getchars
- )
- if "!lna!"=="!exc!" (
- if not "!char_ex!"=="-1" (
- set chars=!chars!!exc!
- set /a chars_n=!chars_n!+1
- set char_ex=-1
- )
- goto getchars
- )
- if "!lna!"=="=" (
- if not "!char_eq!"=="-1" (
- set chars=!chars!=
- set /a chars_n=!chars_n!+1
- set char_eq=-1
- )
- goto getchars
- )
- if "!lna!"==":" (
- if not "!char_as!"=="-1" (
- set chars=!chars!:
- set /a chars_n=!chars_n!+1
- set char_as=-1
- )
- goto getchars
- )
- rem 大写字母判断
- if "!CAP_%lna%!"=="%lna%" (
- if not "!charCAP_%lna%!"=="-1" (
- set chars=!chars!!lna!
- set /a chars_n=!chars_n!+1
- set charCAP_!lna!=-1
- )
- ) else (
- if not "!char_%lna%!"=="-1" (
- set chars=!chars!!lna!
- set /a chars_n=!chars_n!+1
- set char_!lna!=-1
- )
- )
- goto getchars
-
- :getc1
- set sh1= %_n%
- set sh2= %ln_n%
- set /a sh3=%_n%*100/%ln_n%
- set sh3= %sh3%
- set /p =分析中. . . %sh1:~-5%/%sh2:~-5%(%sh3:~-2%%%)<nul
- set /a _n=%_n%+1
- set ln=!ln_%_n%!
- goto getchars
-
- :getc0
-
-
- set /p =正在加密. . . 0/%sh2:~-5%( 0%%)<nul
- set _n=%chars_n%
-
- rem 打乱字符表
- :randchars
- if %_n% lss 0 goto ran0
- set /a k=%random%*%_n%/32768
- set /a k1=%k%+1
- set chars=!chars:~0,%k%!!chars:~%k1%!!chars:~%k%,1!
- set /a _n=%_n%-1
- goto randchars
- :ran0
-
-
- set _n=%chars_n%
- rem 标记字符位置
- :setcharnum
- set /a _n=%_n%-1
- if %_n% lss 0 goto setcn0
- set _c=!chars:~%_n%,1!
- rem 先考虑引号,感叹号,等号和冒号
- if "!_c!" == "!quo!" (
- set char_qu=!_n!
- goto setcharnum
- )
- if "!_c!"=="!exc!" (
- set char_ex=!_n!
- goto setcharnum
- )
- if "!_c!"=="=" (
- set char_eq=!_n!
- goto setcharnum
- )
- if "!_c!"==":" (
- set char_as=!_n!
- goto setcharnum
- )
- rem 大写字母判断
- if "!CAP_%_c%!"=="%_c%" (
- set charCAP_!_c!=!_n!
- ) else (
- set char_!_c!=!_n!
- )
- goto setcharnum
- :setcn0
-
- rem 随机产生字符平移参数
- set /a move_max=%chars_n%/2
- if %move_max% gtr 30 set move_max=30
- :setmove
- set /a move_a=%move_max%*%random%/32767+1
- set /a move_b=%move_max%*%random%/32767+1
- if %random% lss 16384 set /a move_a=-%move_a%
- if %random% lss 16384 set /a move_b=-%move_b%
- rem 通过复数运算得到密码数[ (字符平移参数a + 字符平移参数b * i) * ((密码前三位 + 100) + (密码后三位 + 100) * i) = 密码数]
- call :complex %move_a% %move_b% * %pswd_a% %pswd_b% seed_a seed_b
- call :complex %seed_a% %seed_b% / %pswd_a% %pswd_b% a b
- rem 检查运算是否正确
- if not "%a%"=="%move_a%" goto setmove
- if not "%b%"=="%move_b%" goto setmove
- rem 检查字符平移参数是否互质(保证密码唯一性)
- if %a% lss 0 set /a a=-%a%
- if %b% lss 0 set /a b=-%b%
- :setm1
- if %a% gtr %b% (set /a a=%a% %% %b%) else (set /a b=%b% %% %a%)
- set /a ab=%a%*%b%
- if "%ab%"=="0" (
- set /a a_b=%a%+%b%
- if not "!a_b!"=="1" goto setmove
- ) else (
- goto setm1
- )
-
-
- set _n=1
- set ln=!ln_1!
- set enc=
- set enc_n=0
- set /a move=-%move_a%
- (
- echo TXT_ENCRIPT_1.0 BY 0000
- echo %seed_a%_%seed_b%
- echo;!chars!
- )>"!file2!"
-
- :encriptline
- if %_n% gtr %ln_n% goto enc0
- if "!ln!"=="" goto enc1
- set lna=!ln:~0,1!
- set ln=!ln:~1!
- rem 先考虑引号,感叹号,等号和冒号
- if "!lna!"=="!quo!" (call :setenc "!char_qu!"&goto encriptline)
- if "!lna!"=="!exc!" (call :setenc "!char_ex!"&goto encriptline)
- if "!lna!"=="=" (call :setenc "!char_eq!"&goto encriptline)
- if "!lna!"==":" (call :setenc "!char_as!"&goto encriptline)
- rem 大写字母判断
- if "!CAP_%lna%!"=="%lna%" (call :setenc "!charCAP_%lna%!") else (call :setenc "!char_%lna%!")
- goto encriptline
- :enc1
- call :setenc "-1"
- set sh1= %_n%
- set sh2= %ln_n%
- set /a sh3=%_n%*100/ln_n
- set sh3= %sh3%
- set /p =正在加密. . . %sh1:~-5%/%sh2:~-5%(%sh3:~-2%%%)<nul
- set /a _n=%_n%+1
- set ln=!ln_%_n%!
- goto encriptline
-
- :enc0
- echo;%enc%>>"!file2!"
- echo 加密成功!
- echo 已保存到 !file2! !exc!
- echo ay|choice>nul
- pause
- goto end
-
- rem Functions
-
- :getline
- rem 禁止延迟变量
- setlocal DISABLEDELAYEDEXPANSION
- (
- for /f "usebackq tokens=* eol= delims=" %%L in (%1) do (
- set _L=%%L
- rem 加引号
- call set _L="%%_L:"=""%%"
- call set _L=%%_L:!=!exc!%%
- call :getecho
- )
- )>$tmp$.txt
- goto end
-
- :getecho
- echo;%_L:^=!car!%
- goto end
-
- :setenc
- set m=%1
- set m=!m:~1,-1!
- if "!m!"=="-1" goto sete1
- set /a m=%m%+%move%
- if %m% lss 0 set /a m=%m%+%chars_n%
- if %m% geq %chars_n% set /a m=%m%-%chars_n%
- :sete1
- set enc=!enc!%m%_
- set /a enc_n=%enc_n%+1
- if "%enc_n%"=="8" (
- echo;!enc!>>"!file2!"
- set enc=
- set enc_n=0
- set /a move=!move!-!move_b!
- if !move! lss 0 set /a move=!move!+!chars_n!
- if !move! gtr !chars_n! set /a move=!move!-!chars_n!
- )
- goto end
-
- :complex
- rem complex a实 a虚 符号 b实 b虚 实变量名 虚变量名
- if "%3"=="*" (
- set /a comp_x=%1*%4-%2*%5
- set /a comp_y=%1*%5+%2*%4
- set /a comp_k=1
- )
- if "%3"=="/" (
- set /a comp_x=%2*%5+%1*%4
- set /a comp_y=%2*%4-%1*%5
- set /a comp_k=%4*%4+%5*%5
- )
- set /a %6=%comp_x%/%comp_k%
- set /a %7=%comp_y%/%comp_k%
- goto end
-
- :inputpswd
- set pswd=
- set pswdkey=
- set pswdblank=
- set pswdstar=
- set /a pswdn1=%1-1
- set /p =密码(6位数字):<nul
- for /l %%n in (1,1,%1) do (set pswdblank= !pswdblank!)
- :inp1
- if not "%pswd%"=="" if not "!pswd:~%pswdn1%,1!"=="" (
- echo;
- goto end
- )
- for /f "tokens=*" %%f in ('xcopy /w . . 2^>nul') do (
- set pswdkey=%%f
- goto inp2
- )
- :inp2
- set pswdkey=!pswdkey:~-1!
- if "!pswdkey!"=="" (
- if not "!pswd!"=="" (
- set pswd=!pswd:~0,-1!
- set pswdstar=!pswdstar:~1,-1!
- )
- )
- for /l %%n in (0,1,9) do (
- if "!pswdkey!"=="%%n" (
- set pswd=!pswd!!pswdkey!
- set pswdstar=%2!pswdstar!
- )
- )
- set /p =密码(6位数字):!pswdblank!!pswdstar!<nul
- goto inp1
-
- :getfile2
- setlocal DISABLEDELAYEDEXPANSION
- for %%F in ("%file%") do (set "file2=%%~dpnF_Encript%%~xF")
- set "file2=%file2:!=!exc!%"
- endlocal&set "file2=%file2%"
- goto :end
-
- :end
复制代码
解密
- @echo off
- title 纯文本加解密工具1.0(解密)
- rem 退格符
- setlocal ENABLEDELAYEDEXPANSION
-
- echo 纯文本加解密工具1.0(解密) BY 0000
- :setfile
- set /p file=文件名称:
- if "!file!"=="" (echo 无效的文件名&goto setfile)
- set file=!file:"=!
- if "!file!"=="" (echo 无效的文件名&goto setfile)
- echo "!file!"|findstr /R /C:"[*?]">nul&&(echo 无效的文件名&goto setfile)
- set file_1=!file:~-1!
- if "!file_1!"=="/" (echo 无效的文件名&goto setfile)
- if "!file_1!"=="\" (echo 无效的文件名&goto setfile)
- dir /a-d "!file!">nul 2>nul||(echo 无效的文件名&goto setfile)
- dir /a-d "!file!\">nul 2>nul&&(echo 无效的文件名&goto setfile)
- for /f "usebackq tokens=* delims=" %%t in ("!file!") do (
- if not "%%t"=="TXT_ENCRIPT_1.0 BY 0000" (
- echo 该文件未被加密
- goto setfile
- ) else (
- rem 直接跳出for循环
- goto setpswd
- )
- )
- :setpswd
- call :inputpswd 6 *
- rem 获取密码内容
- set /a pswd_a=1%pswd:~0,3%-900
- set /a pswd_b=1%pswd:~3,3%-900
-
- call :getfile2
-
- for /f "usebackq skip=1 tokens=1,2 delims=_" %%a in ("!file!") do (
- set seed_a=%%a
- set seed_b=%%b
- goto setmove
- )
- :setmove
- rem 通过复数运算得到字符平移参数[ (密码数a + 密码数b * i) * ((密码前三位 + 100) + (密码后三位 + 100) * i) = 字符平移参数]
- call :complex %seed_a% %seed_b% / %pswd_a% %pswd_b% move_a move_b
- call :complex %move_a% %move_b% * %pswd_a% %pswd_b% a b
- rem 检查运算是否正确
- if not "%a%"=="%seed_a%" (echo 密码不正确&goto setpswd)
- if not "%b%"=="%seed_b%" (echo 密码不正确&goto setpswd)
-
- rem 检查字符平移参数是否互质
- set a=%move_a%
- set b=%move_b%
- if %a% lss 0 set /a a=-%a%
- if %b% lss 0 set /a b=-%b%
- :setm1
- if %a% gtr %b% (set /a a=%a% %% %b%) else (set /a b=%b% %% %a%)
- set /a ab=%a%*%b%
- if "%ab%"=="0" (
- set /a a_b=%a%+%b%
- if not "!a_b!"=="1" (echo 密码不正确&goto setpswd)
- ) else (
- goto setm1
- )
-
- set /p =正在解密. . . *****/*****( 0%%)<nul
- copy nul "!file2!" >nul
- call :getchar
- set /p chars=<$tmp$.txt
- del $tmp$.txt
- set _n=0
- set ln_n=0
- for /f "usebackq skip=3 delims= eol=" %%a in ("!file!") do (set /a ln_n=!ln_n!+1)
- set dec=
- set chars=!chars:~%move_a%!!chars:~0,%move_a%!
- for /f "usebackq skip=3 tokens=1,2,3,4,5,6,7,8 delims=_" %%a in ("!file!") do (
- call :setdec %%a
- call :setdec %%b
- call :setdec %%c
- call :setdec %%d
- call :setdec %%e
- call :setdec %%f
- call :setdec %%g
- call :setdec %%h
- set sh1= !_n!
- set sh2= !ln_n!
- set /a sh3=!_n!*100/!ln_n!
- set sh3= !sh3!
- set /p =正在解密. . . !sh1:~-5!/!sh2:~-5!(!sh3:~-2!%%^)<nul
- set /a _n=!_n!+1
- set chars=!chars:~%move_b%!!chars:~0,%move_b%!
- )
- echo;!dec!>>"!file2!"
- echo 解密成功!
- echo 已保存到 !file2! ^^!
- echo ay|choice>nul
- start notepad "!file2!"
- pause
- goto end
-
-
- rem Functions
-
-
- :setdec
- if "%1"=="" goto end
- if "%1"=="-1" goto setd1
- set dec=!dec!!!chars:~%1,1!
- goto end
- :setd1
- echo;!dec!>>"!file2!"
- set dec=
- goto end
-
- :getchar
- rem 禁止延迟变量
- setlocal DISABLEDELAYEDEXPANSION
- for /f "usebackq skip=2 tokens=* delims= eol=" %%t in ("%file%") do (
- echo;%%t>$tmp$.txt
- goto end
- )
- goto end
-
- :complex
- rem complex a实 a虚 符号 b实 b虚 实变量名 虚变量名
- if "%3"=="*" (
- set /a comp_x=%1*%4-%2*%5
- set /a comp_y=%1*%5+%2*%4
- set /a comp_k=1
- )
- if "%3"=="/" (
- set /a comp_x=%2*%5+%1*%4
- set /a comp_y=%2*%4-%1*%5
- set /a comp_k=%4*%4+%5*%5
- )
- set /a %6=%comp_x%/%comp_k%
- set /a %7=%comp_y%/%comp_k%
- goto end
-
- :inputpswd
- set pswd=
- set pswdkey=
- set pswdblank=
- set pswdstar=
- set /a pswdn1=%1-1
- set /p =密码(6位数字):<nul
- for /l %%n in (1,1,%1) do (set pswdblank= !pswdblank!)
- :inp1
- if not "%pswd%"=="" if not "!pswd:~%pswdn1%,1!"=="" (
- echo;
- goto end
- )
- for /f "tokens=*" %%f in ('xcopy /w . . 2^>nul') do (
- set pswdkey=%%f
- goto inp2
- )
- :inp2
- set pswdkey=!pswdkey:~-1!
- if "!pswdkey!"=="" (
- if not "!pswd!"=="" (
- set pswd=!pswd:~0,-1!
- set pswdstar=!pswdstar:~1,-1!
- )
- )
- for /l %%n in (0,1,9) do (
- if "!pswdkey!"=="%%n" (
- set pswd=!pswd!!pswdkey!
- set pswdstar=%2!pswdstar!
- )
- )
- set /p =密码(6位数字):!pswdblank!!pswdstar!<nul
- goto inp1
-
- :getfile2
- setlocal DISABLEDELAYEDEXPANSION
- for %%F in ("%file%") do (set "file2=%%~dpnF_Decript%%~xF")
- set "file2=%file2:!=!exc!%"
- endlocal&set "file2=%file2%"
- goto :end
-
- :end
复制代码
有建议或意见请在下方提出! |