Board logo

标题: [加密解密] Base64+码雏形 [打印本页]

作者: happy886rr    时间: 2016-10-8 21:46     标题: Base64+码雏形

本帖最后由 happy886rr 于 2016-10-8 22:11 编辑

  我根据Base64的构造方式,改良了一种新的编码。这种编码方式与base64具有一样的编码增长率,只是编码的方式略有区别,是一种模拟的base256码,只是可见字符无法达到256种,因此为可见字符构建了引码。引码的取值为0x00、0x01、0x10、0x11,由4个引码与64个可见字符组合成256种不同情况,即组合为一个字节。在编码时,将8位的前两位单独取出作为引码,余下6位直接转化为数据吗,同时每3个引码(也就是凑够6位)合成为一个base64+码。这样引码和数据码都转化为了base+码。

  这种编码,速度会比base64更快,而且形成的引码可以再次加密,演化为加密引码。对于相同的数据码,不同的引码能产生不同的文件,即一个数据码根据不同的引码可以解密为不同的图片,base64+初版只提供基本编码功能,不提供水印、加密引等新特性。以后会发上C语言版本,直接读取二进制流编码。,并提供一个base64+编码的第三方。
  base64+编码体积与base等同。但base64+具有再压缩功能和极高的安全特性。如果分发给用户低等级的引文,只能解码低等级信息,而且破解引码的难度几乎为0.同时base64+的引码只有数据码的30%大小,且引码和数据码具有非常高的可再压缩特性,即压缩特性不降。

  基本的概念构思就是这样的,后期我会完善到可以使用的成熟阶段。base64+编码的优势就是:编解码极为迅速,高安全性,高压缩性,引码数据相分离。我用批处理实现了基本的原型,没想到批处理编解码也这么利索,稍后送上C版本。

编码实现
  1. @echo off
  2. ::**************Happy's Base64+**********
  3. REM 使用说明:
  4. REM Base64+编码,由“引码”+“数据”构成
  5. REM “引码”是数据的解密引文,安全性极高
  6. REM “引码”和“数据”中间用“+”号 隔开
  7. ::**************编码测试*****************
  8. set "原文=589E583EC18895DF88B450831DB8975FC8B0031F68B003D910000C077433D8D0000C0725BBE01000000C704240800000031D289542404E89C0D000083F801747A85C0740EC7042408000000FFD0BBFFFFFFFF89D88B75FC8B5DF889EC5DC204003D940000C074C2774A3D930000C074B489D88B75FC8B5DF889EC5DC20400903D050000C0745B3D1D0000C075C5C704240400000031F689742404E8380D000083F801746A85C074AAC7042404000000FFD0EB9A3D960000C0EBD1C7042408000000B80100000089442404E8080D000085F60F8476FFFFFFE88B090000E96CFFFFFFC704240B00000031C089442404E8E40C000083F801743085C00F8452FFFFFFC704240B000000FFD0E93FFFFFFFC7042404000000B901000000894C2404E8B40C0000E925FFFFFFC704240B000000B80100000089442404E89A0C0000E90BFFFFFF8DB6000000008DBC27000000005589E55383EC24C7042400104000E8050D000083EC04E8FD070000E8F8080000C745F8000000008D45F889442410A104204000C70424044040008944240C8D45F489442408B80040400089442404E84D0C0000A11040400085C07464A3142040008B15F450400085D20F85A100000083FAE0741FA11040400089442404A1F45040008B4030890424E80B0C00008B15F450400083FAC07428A11040400089442404A1F45040008B4050890424E8E70B0000EB0D90909090909090909090909090E8CB0B00008B15142040008910E81607000083E4F0E8EE060000E8A10B00008B0089442408A10040400089442404A104404000890424E89E03000089C3E8760B0000891C24E8060C00008DB60000000089442404A1F45040008B4010890424E8740B00008B15F4504000E940FFFFFF905589E583EC08C7042401000000FF15E8504000E8B8FEFFFF908DB426000000005589E583EC08C7042402000000FF15E8504000E898FEFFFF908DB42600000000558B0D0451400089E55DFFE18D742600558B0DF850400089E55DFFE1909090905589E55DE93F080000909090909090905589E58B4508C700000000008B4508C64004005DC35589E583EC088B45088845FF807DFF3F7609C745F83D000000EB110FB645FF8B15002040000FBE04028945F88B45F8C9C35589E583EC1C8B45188945FC8B450C8945F88B450C0345108945F48B45080FB640048845F38B45088B008945E8837DE8010F8483000000837DE801720F837DE8020F84E5000000E9690100008B45F83B45F475238B55080FB645F38842048B4508C700000000008B55FC8B451829D08945ECE94B0100008B45F889C28D45F8FF000FB6028845F20FB645F225FC000000C1F8028845F30FB645F3890424E82DFFFFFF88C28B451888108D4518FF000FB645F22403C0E0048845F38B45F83B45F475238B55080FB645F3884"
  9. ::**************Base64+码表**************
  10. set "CODE_SKIN=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]
  11. ::**************Base64+编码器************
  12. setlocal enabledelayedexpansion
  13. set "LINE=!原文!000000"
  14. for /l %%j in (0,1,8000) do (
  15. set "STR=!LINE:~0,6!"
  16. set/a "A0=(0x!STR:~0,2!)&0x3F, A1=(0x!STR:~2,2!)&0x3F, A2=(0x!STR:~4,2!)&0x3F, N0=(0x!STR:~0,2!>>6)&0x3, N1=(0x!STR:~2,2!>>6)&0x3, N2=(0x!STR:~4,2!>>6)&0x3, N=N0*16+N1*4+N2" 2>NUL
  17. for /f "tokens=1,2,3,4" %%a in ("!A0! !A1! !A2! !N!") do (
  18. set "OUT=!OUT!!CODE_SKIN:~%%a,1!!CODE_SKIN:~%%b,1!!CODE_SKIN:~%%c,1!"
  19. set "QU=!QU!!CODE_SKIN:~%%d,1!"
  20. )
  21. set "LINE=!LINE:~6!"
  22. if "!LINE:0=!"=="" (
  23. echo Base64+密文:
  24. set/p=!QU!+!OUT!&exit
  25. )
  26. )
复制代码
解码实现
  1. @echo off
  2. ::**************Happy's Base64+**********
  3. REM 使用说明:
  4. REM Base64+编码,由“引码”+“数据”构成
  5. REM “引码”是数据的解密引文,安全性极高
  6. REM “引码”和“数据”中间用“+”号 隔开
  7. ::**************解码测试*****************
  8. set "密文=ZOumKeBjAE8AsAVgAlbwIZHVgMv[p7rxNARsAWp7rxDQG8BVUAZWsCGRlUDK0CFYCAJWsF1]qC]1YAJWcCETT9WAy]1UCALWcJ]VgIAlawr]QD0AZDVQCcDbQqBYA2lBAVQJfWWgCVvAAEUQJQE0AIcAJUQIJawlAIYAJUQIJZgsAAAAqCUCJQEtCIIlgAlRAlsCJgtnDQCVECCWYJQI]GTlQDYCvzUAZOVANgK]NQBtAnzUbgJ8AGe4AABmZACZExk6ac]ZbAjXkA9JpoZPmnnmnElm0ZJomONADgzjUCgJpUm1IZkAJ5onoCaNs0k1HAIk1Jb]iaNs1EBkmlSbUg+YeY[BIVfI0QDd4XfIwDfowDZQAMH0zYQAMHl7gQAAMwCAAAADdoVCAOJAQAI]A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(#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,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,0,1,2,3,4,5,6,7,8,9,[,]) do (
  14. set/a "CODE+=1,UQ_%%A=CODE,SUF=CODE&0xF,CACHE=CODE>>4,PRE0=CACHE,PRE1=4+CACHE,PRE2=8+CACHE,PRE3=12+CACHE"
  15. for %%N in (0,1,2,3) do (
  16. for /f "tokens=1,2" %%a in ("!PRE%%N! !SUF!") do (
  17. set "BASE%%N_%%A=!SKIN:~%%a,1!!SKIN:~%%b,1!"
  18. )
  19. )
  20. if !CODE! lss 26 (
  21. set "CASE=%%A"
  22. set "%%A=!CASE!"
  23. )
  24. )
  25. ::**************Base64+解码器************
  26. for /f "tokens=1,2 delims=+" %%a in ("!密文!") do (
  27. set "QU=%%a"
  28. set LINE=%%b###"
  29. )
  30. for /l %%j in (0,1,1000) do (
  31. set "PRE=!QU:~%%j,1!"
  32. for %%Q in (!PRE!) do (
  33. if "#%%Q"=="!#%%Q!" (
  34. set "PRE=#!PRE!"
  35. )
  36. )
  37. set "STR=!LINE:~0,3!"
  38. set/a "N2=UQ_!PRE!&0x3,N1=(UQ_!PRE!>>2)&0x3,N0=(UQ_!PRE!>>4)&0x3"
  39. for %%i in (0,1,2) do (
  40. for /f "tokens=1,2" %%a in ("!N%%i! !STR:~%%i,1!") do (
  41. if "%%b"=="#" (
  42. echo 二进制明文:
  43. set/p=!OUT!&exit
  44. )
  45. if "#%%b"=="!#%%b!" (
  46. set "OUT=!OUT!!BASE%%a_#%%b!"
  47. ) else (
  48. set "OUT=!OUT!!BASE%%a_%%b!"
  49. )
  50. )
  51. )
  52. set "LINE=!LINE:~3!"
  53. )
复制代码





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