|
s11ss
银牌会员
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『楼 主』:
[原创]::::::::::::::::Base64加密::::::::::::::::
@echo off
::::::::Base64 Encode {s11ss 2007-12-1}::::::::
setlocal
echo 请输入要转换的字符串(可以有汉字):
set/p s=
set vbs=%tmp%\t.vbs
if exist %vbs% goto :str2hex
set t=^>^>%vbs% echo
%t%s=wscript.arguments(0)
%t%h=""
%t%for i=1 to len(s)
%t% h=h^&cstr(hex(asc(mid(s,i,1))))
%t%next
%t%wscript.stdout.write(h)
:str2hex
for /f %%a in ('cscript//nologo %vbs% "%s%"') do set "hex=%%a"
set "base64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
setlocal enabledelayedexpansion
:hex2base64
if not defined hex goto :print
for /l %%a in (1,1,3) do (
set/a "n=2*(%%a-1)"
call set x=%%hex:~!n!,2%%
set "c%%a=0x!x!"
if "!x!" equ "" set c%%a=0
)
set hex=%hex:~6%
set/a ec1=%c1%">>"2
set/a ec2=%c1%"<<"4"|"%c2%">>"4
set/a ec3=%c2%"<<"2"|"%c3%">>"6
set/a ec4=%c3%
for /l %%a in (1,1,4) do set/a ec%%a=!ec%%a!"&"63
if %c2% equ 0 set ec3=64
if %c3% equ 0 set ec4=64
for /l %%a in (1,1,4) do call set "es=%%es%%%%base64:~!ec%%a!,1%%"
goto :hex2base64
:print
echo.
echo.%es%
pause [ Last edited by s11ss on 2007-12-1 at 04:10 PM ]
此帖被 +12 点积分 点击查看详情 评分人:【 zh159 】 | 分数: +10 | 时间:2007-12-1 16:19 | 评分人:【 ysc 】 | 分数: +2 | 时间:2007-12-1 18:22 |
|
|
|
2007-12-1 14:31 |
|
|
vkill
金牌会员
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
|
2007-12-1 15:39 |
|
|
s11ss
银牌会员
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『第
3 楼』:
Quote: | Originally posted by vkill at 2007-12-1 03:39 PM:
进来看了下不是纯 p 的 |
|
要得到字符串的十六进制编码,如果字符串中不含汉字的话,用纯批处理实现还不算麻烦,但如果包含汉字那就太不好办了,字典不知道要多大。
|
|
2007-12-1 16:05 |
|
|
zh159
金牌会员
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第
4 楼』:
收藏
|
|
|
2007-12-1 16:19 |
|
|
knoppix7
银牌会员
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第
5 楼』:
考虑DEBUG?(貌似debug启动速度.....)
|
|
2007-12-1 17:00 |
|
|
fastslz
铂金会员
DOS一根葱
积分 5493
发帖 2315
注册 2006-5-1 来自 上海
状态 离线
|
『第
6 楼』:
我也收藏了一个
::code by 0401
::chenall 修改于 2007.01.25
@echo off
setlocal
setlocal enabledelayedexpansion
set B64CODE=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
set str=
set/p "str=请输入需要编码的字符串:"
if not defined str exit/b
call :enc
call :dec
exit/b
:enc
rem 两个比较的文件名为:[.str] [.cmp]
rem 将字符串放到文件中等待比较(fc)并求字符串长度
set/p=%str%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo 原始字符串长度为: %strlen% 字节
set str_len=%strlen%
rem 生成fc用来与字符串比较的文件
del _*.cmp 2>nul
for /l %%l in (1,1,%strlen%) do (
>>_a.cmp set/p=a<nul
>>_b.cmp set/p=b<nul
)
fc/b _a.cmp .str|find ":">tmp.str
fc/b _b.cmp .str|find ":">>tmp.str
sort tmp.str>_a.cmp
rem 将比较后得到的字符串的16进制存储到变量
for /f "tokens=1,3" %%i in (_a.cmp) do (
if not "!n!"=="%%i" set strhex=!strhex!%%j
set n=%%i
)
::for /f "tokens=3" %%i in ('fc/b .cmp .str^|find ":"') do (set strhex=!strhex!%%i)
del *.cmp *.str 2>nul
echo 转换后的16进制为: %strhex%
:encloop
if not defined strhex goto :encbreak
set chr1=0x!strhex:~0,2!
set chr2=0x!strhex:~2,2!
set chr3=0x!strhex:~4,2!
set strhex=%strhex:~6%
for %%i in (chr1 chr2 chr3) do if "!%%i!"=="0x" set %%i=0
set/a enc1=%chr1%">>"2
set/a enc2=((%chr1%"&"3)"<<"4)"|"(%chr2%">>"4)
set/a enc3=((%chr2%"&"15)"<<"2)"|"(%chr3%">>"6)
set/a enc4=%chr3%"&"63
::echo 移位后得到的数据:%enc1% %enc2% %enc3% %enc4%
if %chr3% equ 0 if %chr2% equ 0 (set enc4=64& set enc3=64) else (set enc4=64)
::echo 移位后的数据编码为:!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
set encstr=!encstr!!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
::for %%i in (enc1 enc2 enc3 enc4) do (
:: if !%%i! lss 10 (set/p=0!%%i!<nul>>tmp.bb) else (set/p=!%%i!<nul>>tmp.bb)
::)
goto :encloop
:encbreak
echo 字符串[%str%]编码后的数据为[%encstr%]
::echo 字符串[%str%]编码后的数据为[%encstr%] >>tmp.aa
goto :eof
:dec
rem 并求编码字符串长度
set/p=%encstr%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo 编码后的字符串长度为: %strlen% 字节
del .str
rem 从编码的字符串encstr中求出编码时偏移的数量
set str=
for /l %%l in (0,1,%strlen%) do (
for /l %%m in (0,1,64) do (
if "!encstr:~%%l,1!"=="!B64CODE:~%%m,1!" (
if %%m lss 10 (
set str=!str!0%%m
) else (
set str=!str!%%m
)
)
)
)
set a=1&set b=4&set e=0
del debug.src 2>nul
:decloop
if not defined str goto :decbreak
set /a enc1=1!str:~0,2!-100
set /a enc2=1!str:~2,2!-100
set /a enc3=1!str:~4,2!-100
set /a enc4=1!str:~6,2!-100
set str=%str:~8%
for %%i in (enc1 enc2 enc3 enc3 enc4) do (
if !%%i! leq 0 set %%i=0
if !%%i! equ 64 set %%i=0
)
::echo 4个字节的偏移分别为: %enc1% %enc2% %enc3% %enc4%
set/a chr1=((%enc1%"<<"2)"|"(%enc2%">>"4))
set/a chr2=(((%enc2%"&"15)"<<"4)"|"(%enc3%">>"2))
set/a chr3=(((%enc3%"&"3)"<<"6)"|"%enc4%)
::echo 4个字节解码后再减去32得到的十进制为: %chr1% %chr2% %chr3%
call :_d2h chr1 %chr1%
call :_d2h chr2 %chr2%
call :_d2h chr3 %chr3%
::echo 4个字节解码后再减去32得到的十六进制为: %chr1% %chr2% %chr3%
::echo 解码后的数据为:!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
set /a ee=0x100+3*e
call :_d2h ee %ee%
>>debug.src echo e %ee% %chr1% %chr2% %chr3%
::set/p=%chr1%%chr2%%chr3%<NUL >>BB.TXT
set /a e+=1
goto :decloop
:decbreak
pause
set /a str_len+=1
>>debug.src echo rcx
>>debug.src echo %str_len%
>>debug.src echo n tmp.tmp
>>debug.src echo w
>>debug.src echo q
debug<debug.src
del debug.src
chcp 936>nul
echo 编码的数据[%encstr%]
echo.
set/p=解码后为:<nul
type tmp.tmp
del tmp.tmp>nul
echo.
pause
goto :eof
:_d2h
set hex=
set hexstr=0 1 2 3 4 5 6 7 8 9 A B C D E F
set d=0
for %%i in (%hexstr%) do (set d!d!=%%i&set/a d+=1)
set scanf=%2
set tscanf=
call :d2h
if not defined hex set hex=0
set %1=%hex%
goto :eof
:d2h
if %scanf% equ 0 goto :eof
set/a tscanf=%scanf%"&"15
set/a scanf">>="4
set hex=!d%tscanf%!!hex!
goto :d2h
|
第一高手 第二高手
我的小站
|
|
2007-12-1 17:22 |
|
|
s11ss
银牌会员
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『第
7 楼』:
Quote: | Originally posted by knoppix7 at 2007-12-1 05:00 PM:
考虑DEBUG?(貌似debug启动速度.....) |
|
多谢指点,我来研究一下先.......
|
|
2007-12-1 17:51 |
|
|
wjjchrdw
初级用户
积分 26
发帖 12
注册 2005-10-1
状态 离线
|
『第
8 楼』:
楼主强人!!用批处理能写成base64的加密算法。
|
|
2007-12-1 18:31 |
|
|
yinyuan
初级用户
积分 50
发帖 22
注册 2006-5-14
状态 离线
|
|
2007-12-1 19:13 |
|
|
knoppix7
银牌会员
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第
10 楼』:
记得debug 路径
路径要用8.3格式的.(直接copy CON c:\123.txt然后在debug c:\123.txt应该可以.
结束用CTRL+Z 回车)
然后还需要一个10转16进制的东西(文件大小.... )
|
|
2007-12-1 20:06 |
|
|
knoppix7
银牌会员
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第
11 楼』:
一个事例。不是BASE64的.
和DEBUG提取有关。给LZ了.
感觉有点垃圾....
@echo off&setlocal ENABLEDELAYEDEXPANSION
::DNA CODER [BAT] V=0.1 BY KPX
set NUM=1
set numstart=0
set str=0123456789ABCDEFG
for %%A IN (G C T A) DO (
for %%B IN (C A T G) DO (
call set H%%STR:~!numstart!,1%%=%%A%%B
set /a numstart=!numstart!+1
)
)
echo 文件路径不能含有中文,空格,路径要符合8.3规范(DEBUG.com限制)
set /p TRG=要加密的文件::
call :D2F %trg% filename
for /F "tokens=3 delims= " %%a IN ('dir /-c %trg%^|find "%filename%"') DO set /a size=%%a-1
call :d2hmain %size%
:jump1
echo D 100 %DEBUGSIZE%>HASHER.txt
echo Q>>HASHER.txt
debug %TRG% <HASHER.txt >test.txt
del HASHER.txt /q
chcp 936>nul
GRAFTABL 936>nul
echo 正在处理文件.......这可能需要很长时间............
FOR /F "usebackq tokens=1* delims=" %%a IN (`type test.txt^|more +2^|findstr [1-F]`) DO (
set "T1=%%a"
set "T1=!T1:-= !"
set "RUS!NUM!=!T1:~11,47!"
call :StartHash "%%RUS!NUM!%%"
)
set NUM=&set numstart=&set str=&set T1=
del test.txt /f /q
pause
exit
:StartHash
set T2=%~1
for %%A IN (%T2%) DO (
set T3=%%A
set T4=!T3:~0,1!
set T5=!T3:~1,1!
call set RUS=!RUS!%%H!T4!%%%%H!T5!%%
)
echo %RUS:~1%>>HASHED.txt
set RUS=
GOTO :EOF
:d2hmain
set num=%1
set TRGEV=%2
set str=0123456789ABCDEF
call :A %num%
set sizebyte=%T3%
set /a num=%NUM%+256
call :A %num%
set DEBUGSIZE=%T3%
GOTO jump1
:A
set T1=%1
SET T3=
set T2=
:B
set/A T2=T1%%16
SET T2=!str:~%t2%,1!
SET/A T1=%T1%/16
SET T3=%T2%%T3%
IF %T1%==0 GOTO :EOF
GOTO B
:D2F
set %2=%~nx1
GOTO :EOF [ Last edited by knoppix7 on 2007-12-1 at 08:11 PM ]
|
|
2007-12-1 20:09 |
|