[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]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

字典式的呢。。。
做起来很累人。。。
而且还不能分辨大小写。。。
第三方命令行工具编程
Http://Hi.Baidu.Com/Console_App

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

用替换法效率更高,缺点需要一个中介字符,本代码中用的是#号,也就是文本内不能含#号。
:
  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
复制代码
1

评分人数

    • batman: 但此法不适合汉字加密PB + 20
技术问题请到论坛发帖求助!

TOP

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

TOP

返回列表