[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

【练习-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分,视情况加分。
***共同提高***

提示:

做本题需要用到乱序,即将字母a-z乱序后再进行赋值,不多说了,只是帮大家打开一下思路了。
***共同提高***

TOP

看来大家是怕麻烦,先公布下本人的解吧(除了逐字符法没想到合适的办法,效率上有问题):
  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 编辑 ]
***共同提高***

TOP

返回列表