标题: 【练习-044】批处理对文本内容加密 [打印本页]
作者: batman 时间: 2009-4-17 20:55 标题: 【练习-044】批处理对文本内容加密
&&有文本a.txt内容如下:- The Boston Evening Transcript
-
- THE READERS of the Boston Evening Transcript
- Sway in the wind like a field of ripe corn.
-
- When evening quickens faintly in the street,
- Wakening the appetites of life in some
- And to others bringing the Boston Evening Transcript,
- I mount the steps and ring the bell, turning
- Wearily, as one would turn to nod good-bye to Rochefoucauld,
- If the street were time and he at the end of the street,
- And I say, “Cousin Harriet, here is the Boston Evening Transcript.
复制代码
要求使用批处理将原文的英文字母进行加密输出到b.txt,同时根据b.txt中的内容还原原文内容,并追加输出到b.txt,如下:- 密文
- srq bfxsfl qvqloli stmlxetoas
-
- srq tqmwqtx fc srq bfxsfl qvqloli stmlxetoas
- xpmg ol srq polw dozq m coqdw fc toaq eftl.
-
- prql qvqloli kjoezqlx cmolsdg ol srq xstqqs,
- pmzqloli srq maaqsosqx fc docq ol xfuq
- mlw sf fsrqtx btolioli srq bfxsfl qvqloli stmlxetoas,
- o ufjls srq xsqax mlw toli srq bqdd, sjtloli
- pqmtodg, mx flq pfjdw sjtl sf lfw iffw-bgq sf tferqcfjemjdw,
- oc srq xstqqs pqtq souq mlw rq ms srq qlw fc srq xstqqs,
- mlw o xmg, “efjxol rmttoqs, rqtq ox srq bfxsfl qvqloli stmlxetoas.
- 原文
- the boston evening transcript
-
- the readers of the boston evening transcript
- sway in the wind like a field of ripe corn.
-
- when evening quickens faintly in the street,
- wakening the appetites of life in some
- and to others bringing the boston evening transcript,
- i mount the steps and ring the bell, turning
- wearily, as one would turn to nod good-bye to rochefoucauld,
- if the street were time and he at the end of the street,
- and i say, “cousin harriet, here is the boston evening transcript.
复制代码
本题满分20分,视情况加分。
作者: batman 时间: 2009-4-17 21:22 标题: 提示:
做本题需要用到乱序,即将字母a-z乱序后再进行赋值,不多说了,只是帮大家打开一下思路了。
作者: defanive 时间: 2009-4-17 22:07
字典式的呢。。。
做起来很累人。。。
而且还不能分辨大小写。。。
作者: batman 时间: 2009-4-19 18:40
看来大家是怕麻烦,先公布下本人的解吧(除了逐字符法没想到合适的办法,效率上有问题):- @echo off&setlocal enabledelayedexpansion
- echo 密文:>b.txt
- set "code=a b c d e f g h i j k l m n o p q r s t u v w x y z"
- for %%a in (%code%) do set /a n+=1&set "_!n!=%%a"&set ".!n!=%%a"&rem 生成正常序列
- for /l %%a in (1,1,26) do (
- set /a num=!random!%%26+1
- call,set "temp=%%_!num!%%"
- set "_!num!=!_%%a!"&set "_%%a=!temp!"
- )&rem 乱序
- for /l %%a in (1,1,26) do set "!_%%a!=!.%%a!"&set "_!.%%a!=!_%%a!"
- for /f "delims=" %%a in (a.txt) do set "str=%%a"&call :lp
- echo 原文:>>b.txt
- for /f "skip=1 delims=" %%a in (b.txt) do if "%%a" neq "原文:" set "str=%%a"&call :lp ok
- start b.txt&goto :eof
- :lp
- if "%1" equ "ok" set "ok=_"
- :loop
- set "flag="
- if /i "%str:~,1%" geq "a" if /i "%str:~,1%" leq "z" set "flag=a"
- if defined flag (
- set /p=!%ok%%str:~,1%!<nul>>b.txt
- ) else (
- set /p=!str:~,1!<nul>>b.txt
- )
- set "str=%str:~1%"
- if defined str goto loop
- echo.>>b.txt
复制代码
[ 本帖最后由 batman 于 2009-4-20 10:38 编辑 ]
作者: 随风 时间: 2009-4-19 21:04
用替换法效率更高,缺点需要一个中介字符,本代码中用的是#号,也就是文本内不能含#号。
:- @echo off&setlocal enabledelayedexpansion
- ::创建随机字典
- set "code= abcdefghijklmnopqrstuvwxyz"
- set "code2=a b c d e f g h i j k l m n o p q r s t u v w x y z"
- set /a dd=27
- set wjm=a.txt
- for /l %%a in (13 -1 1) do (
- set /a dd-=1,ww=!random!%%dd+1
- for %%i in (!ww!) do set "aa=!code:~%%i,1!"
- for %%i in (!aa!) do set "code=!code:%%i=!"
- set /a dd-=1,ww=!random!%%dd+1
- for %%i in (!ww!) do set "bb=!code:~%%i,1!"
- for %%i in (!bb!) do set "code=!code:%%i=!"
- set .!aa!=!bb!&set ".!bb!=!aa!"
- )
- set num=a&echo.密文>c.txt
- :loop 加密、解密为同一代码。
- for /f "%skip% delims=" %%a in (%wjm%) do (
- if not defined num echo 原文>>c.txt
- set num=%%a&setlocal
- for %%i in (!code2!) do (
- if not defined ..%%i (
- set num=!num:%%i=#!
- for %%j in (!.%%i!) do set num=!num:%%j=%%i!&set num=!num:#=%%j!
- set ..%%i=a&set ..!.%%i!=a
- )
- )
- >>c.txt echo.!num!
- endlocal
- )
- set "num="&echo.>>c.txt
- if "!wjm!"=="a.txt" set wjm=c.txt&set "skip=skip=1"&goto loop
- start c.txt
复制代码
作者: canyuexiaolang 时间: 2009-8-28 15:33
如果给出
A代表什么
B代表什么
……
Z代表什么
在用变量延迟代换一下
(— —)还象有点笨啊……
比管理员的差远啦
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |