|
s11ss
银牌会员
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『楼 主』:
[原创]********Base64加密[纯P版]********
@echo off
::::::::Base64 Encode {s11ss 2007-12-1}::::::::
::思路:
:: 1.用debug的d命令获取字符串的16进制代码
:: a.计算输入的字符串的长度len(一个汉字是两个字节)
:: b.根据len,计算构造临时文件t1,里面存储debug命令d与q
:: c.构造临时文件t2,里面存储字符串,执行debug(参数为t1和t2)生成临时文件t3
:: d.读取t3的内容,获取字符串的16进制代码(分别处理代码最后一行和之前的行)
:: 2.根据base64方案把16进制代码编码(参照http://baike.baidu.com/view/469071.htm)
setlocal
:getstr
echo 请输入要转换的字符串(可以有汉字):
set/p s=
if "%s%" equ "" goto :getstr
set/a len=0,l=0
:addlen
call set "o=%%s:~%len%,1%%"
if not "%o%" equ "" (
set/a len+=1
if "%o%" gtr "Z" set/a l+=1
goto :addlen
)
set/a len+=%l%
::debug应用
set/a end=0x100+%len%-1
call :10to16 %end%
pushd %tmp%
>t1 call echo d100 %%r%end%%%
>>t1 echo q
>t2 echo.%s%
type t1|debug t2>t3
::获取字符串的16进制代码
set/a n=0
set "i=%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j%%k%%l%%m%%n%%o%%p"
for /f "delims=" %%a in ('more t3^|find ":"') do set/a lines+=1
set/a m=1
setlocal enabledelayedexpansion
for /f "tokens=2-17 delims=- " %%a in ('more t3^|find ":"') do (
if !m! equ %lines% goto :lastline
call set "hex=!hex!%i%"
set/a m+=1
)
:lastline
set/a lines-=1
set/a remain=(%len%-16*%lines%)*2
call set remain=%%i:~0,%remain%%%
for /f "tokens=2-17 delims=- " %%a in ('more t3^|find ":"^|more +%n%') do set "hex=%hex%%remain%"
::根据base64方案把16进制代码编码
set "base64=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
: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
popd
goto :eof
:10to16
set "h=0123456789ABCDEF"
set/a q=%1
:1c
set/a r=%q%%%16
set/a q=%q%/16
call set r=%%h:~%r%,1%%
call set r%1=%r%%%r%1%%
if not %q% equ 0 goto :1c
此帖被 +4 点积分 点击查看详情 评分人:【 vkill 】 | 分数: +4 | 时间:2007-12-2 03:03 |
|
|
|
2007-12-1 23:49 |
|
|
zh159
金牌会员
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第
2 楼』:
修改后不用dubug的(用fc/b):
@echo off
::::::::Base64 Encode {s11ss 2007-12-1,modify zh159 2007-12-2}::::::::
setlocal
echo 请输入要转换的字符串(可以有汉字):
set/p input=
set str=%input%
::计算字符串长度{modify zh159}
:addlen
set "str=%str:~1%"
set/a len+=1
if not "%str%" == "" goto addlen
::利用 fc/b 获取16进制代码{modify zh159}
for /l %%n in (1,1,%len%) do call set "str=%%str%%``"
>tmp.txt set/p=%str%<nul
>temp.txt set/p=%input%<nul
for /f "tokens=3" %%i in ('fc/b tmp.txt temp.txt^|findstr "0000"') do call set "hex=%%hex%%%%i"
del temp.txt
del tmp.txt
:str2hex
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
此帖被 +4 点积分 点击查看详情 评分人:【 vkill 】 | 分数: +4 | 时间:2007-12-2 03:03 |
|
|
|
|
2007-12-2 01:21 |
|
|
vkill
金牌会员
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
|
2007-12-2 03:08 |
|
|
vkill
金牌会员
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第
4 楼』:
用 fsutil + fc 更“科学”些,呵呵,以前lx 提到过的
@echo off
::::::::Base64 Encode {s11ss 2007-12-1,modify zh159 2007-12-2}::::::::
setlocal
echo 请输入要转换的字符串(可以有汉字):
set/p input=
set str=%input%
::利用 fc/b 获取16进制代码{modify zh159}
>temp.txt set/p=%input%<nul
call :test temp.txt
::利用fsutil创建一个一样大小的文件
fsutil file createnew tmp.txt %size% >nul 2>nul
for /f "tokens=3" %%i in ('fc/b tmp.txt temp.txt^|findstr "^0000"') do call set "hex=%%hex%%%%i"
del temp.txt
del tmp.txt
:str2hex
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
goto :eof
:test
set "size=%~z1"
goto :eof [ Last edited by vkill on 2007-12-2 at 03:32 AM ]
|
|
2007-12-2 03:29 |
|
|
vkill
金牌会员
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第
5 楼』:
fc 比较文本文件可以,要是比较exe文件就出错了
|
|
2007-12-2 06:19 |
|
|
zh159
金牌会员
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第
6 楼』:
直接用一句 for 来获取文件大小
@echo off
::::::::Base64 Encode {s11ss 2007-12-1,modify zh159 2007-12-2}::::::::
setlocal
echo 请输入要转换的字符串(可以有汉字):
set/p input=
set str=%input%
::利用 fc/b 获取16进制代码{modify zh159}
>temp.txt set/p=%input%<nul
for /f %%i in ("temp.txt") do set "size=%%~zi"
fsutil file createnew tmp.txt %size% >nul 2>nul
for /f "tokens=3" %%i in ('fc/b tmp.txt temp.txt^|findstr "0000"') do call set "hex=%%hex%%%%i"
del temp.txt
del tmp.txt
:str2hex
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
|
|
|
2007-12-2 10:15 |
|
|
chishingchan
银牌会员
积分 1282
发帖 538
注册 2002-11-2
状态 离线
|
|
2008-5-1 09:33 |
|
|
provem
初级用户
积分 92
发帖 82
注册 2007-11-13
状态 离线
|
『第
8 楼』:
我相用来加密文件,但是速度太慢了,谁有好的方案吗
|
|
2008-12-17 22:38 |
|
|
lxmxn
版主
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第
9 楼』:
Quote: | Originally posted by provem at 2008-12-17 22:38:
我相用来加密文件,但是速度太慢了,谁有好的方案吗 |
|
试试 OpenSSL 。
用base64编码你的文件
C:\> openssl base64 -in infile -out outfile
解码
C:\> openssl base64 -in outfile -out newfile
|
|
2008-12-18 09:29 |
|