Board logo

标题: 【练习-044】批处理对文本内容加密 [打印本页]

作者: batman    时间: 2009-4-17 20:55     标题: 【练习-044】批处理对文本内容加密

&&有文本a.txt内容如下:
  1. The Boston Evening Transcript
  2.     
  3. THE READERS of the Boston Evening Transcript
  4. Sway in the wind like a field of ripe corn.
  5.     
  6. When evening quickens faintly in the street,
  7. Wakening the appetites of life in some
  8. And to others bringing the Boston Evening Transcript,
  9. I mount the steps and ring the bell, turning
  10. Wearily, as one would turn to nod good-bye to Rochefoucauld,
  11. If the street were time and he at the end of the street,
  12. And I say, “Cousin Harriet, here is the Boston Evening Transcript.
复制代码
要求使用批处理将原文的英文字母进行加密输出到b.txt,同时根据b.txt中的内容还原原文内容,并追加输出到b.txt,如下:
  1. 密文
  2. srq bfxsfl qvqloli stmlxetoas
  3.     
  4. srq tqmwqtx fc srq bfxsfl qvqloli stmlxetoas
  5. xpmg ol srq polw dozq m coqdw fc toaq eftl.
  6.     
  7. prql qvqloli kjoezqlx cmolsdg ol srq xstqqs,
  8. pmzqloli srq maaqsosqx fc docq ol xfuq
  9. mlw sf fsrqtx btolioli srq bfxsfl qvqloli stmlxetoas,
  10. o ufjls srq xsqax mlw toli srq bqdd, sjtloli
  11. pqmtodg, mx flq pfjdw sjtl sf lfw iffw-bgq sf tferqcfjemjdw,
  12. oc srq xstqqs pqtq souq mlw rq ms srq qlw fc srq xstqqs,
  13. mlw o xmg, “efjxol rmttoqs, rqtq ox srq bfxsfl qvqloli stmlxetoas.
  14. 原文
  15. the boston evening transcript
  16.     
  17. the readers of the boston evening transcript
  18. sway in the wind like a field of ripe corn.
  19.     
  20. when evening quickens faintly in the street,
  21. wakening the appetites of life in some
  22. and to others bringing the boston evening transcript,
  23. i mount the steps and ring the bell, turning
  24. wearily, as one would turn to nod good-bye to rochefoucauld,
  25. if the street were time and he at the end of the street,
  26. 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

看来大家是怕麻烦,先公布下本人的解吧(除了逐字符法没想到合适的办法,效率上有问题):
  1. @echo off&setlocal enabledelayedexpansion
  2. echo 密文:>b.txt
  3. 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"
  4. for %%a in (%code%) do set /a n+=1&set "_!n!=%%a"&set ".!n!=%%a"&rem 生成正常序列
  5. for /l %%a in (1,1,26) do (
  6.     set /a num=!random!%%26+1
  7.     call,set "temp=%%_!num!%%"
  8.     set "_!num!=!_%%a!"&set "_%%a=!temp!"
  9. )&rem 乱序
  10. for /l %%a in (1,1,26) do set "!_%%a!=!.%%a!"&set "_!.%%a!=!_%%a!"
  11. for /f "delims=" %%a in (a.txt) do set "str=%%a"&call :lp
  12. echo 原文:>>b.txt
  13. for /f "skip=1 delims=" %%a in (b.txt) do if "%%a" neq "原文:" set "str=%%a"&call :lp ok
  14. start b.txt&goto :eof
  15. :lp
  16. if "%1" equ "ok" set "ok=_"
  17. :loop
  18. set "flag="
  19. if /i "%str:~,1%" geq "a" if /i "%str:~,1%" leq "z" set "flag=a"
  20. if defined flag (
  21.    set /p=!%ok%%str:~,1%!<nul>>b.txt
  22.    ) else (
  23.    set /p=!str:~,1!<nul>>b.txt
  24. )
  25. set "str=%str:~1%"
  26. if defined str goto loop
  27. echo.>>b.txt
复制代码

[ 本帖最后由 batman 于 2009-4-20 10:38 编辑 ]
作者: 随风    时间: 2009-4-19 21:04

用替换法效率更高,缺点需要一个中介字符,本代码中用的是#号,也就是文本内不能含#号。
:
  1. @echo off&setlocal enabledelayedexpansion
  2. ::创建随机字典
  3. set "code= abcdefghijklmnopqrstuvwxyz"
  4. 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"
  5. set /a dd=27
  6. set wjm=a.txt
  7. for /l %%a in (13 -1 1) do (
  8.    set /a dd-=1,ww=!random!%%dd+1
  9.    for %%i in (!ww!) do set "aa=!code:~%%i,1!"
  10.    for %%i in (!aa!) do set "code=!code:%%i=!"
  11.    set /a dd-=1,ww=!random!%%dd+1
  12.    for %%i in (!ww!) do set "bb=!code:~%%i,1!"
  13.    for %%i in (!bb!) do set "code=!code:%%i=!"
  14.    set .!aa!=!bb!&set ".!bb!=!aa!"
  15. )
  16. set num=a&echo.密文>c.txt
  17. :loop 加密、解密为同一代码。
  18. for /f "%skip% delims=" %%a in (%wjm%) do (
  19.     if not defined num echo 原文>>c.txt
  20.     set num=%%a&setlocal
  21.     for %%i in (!code2!) do (
  22.        if not defined ..%%i (
  23.          set num=!num:%%i=#!
  24.          for %%j in (!.%%i!) do set num=!num:%%j=%%i!&set num=!num:#=%%j!
  25.          set ..%%i=a&set ..!.%%i!=a
  26.        )
  27.     )
  28.     >>c.txt echo.!num!
  29.     endlocal
  30. )
  31. set "num="&echo.>>c.txt
  32. if "!wjm!"=="a.txt" set wjm=c.txt&set "skip=skip=1"&goto loop
  33. start c.txt
复制代码

作者: canyuexiaolang    时间: 2009-8-28 15:33

如果给出
A代表什么
B代表什么
……
Z代表什么
在用变量延迟代换一下
  (— —)还象有点笨啊……
比管理员的差远啦




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2