我已经找到了很多方法在Windows上使用命令行对整个文件进行base64编码,但我似乎找不到使用命令行实用程序批量编码“字符串”的简单方法。
如何做到这一点,例如在批处理文件中使用?
答案 0 :(得分:17)
这是一个PowerShell单行程序,您可以从cmd控制台运行,该控制台将Base64编码为字符串。
powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"Hello world!\"))"
它可能不如npocmaka的解决方案快,但你可以set a console macro with it。
doskey btoa=powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"$*\"))"
doskey atob=powershell "[Text.Encoding]::UTF8.GetString([convert]::FromBase64String(\"$*\"))"
btoa Hello world!
btoa This is fun.
btoa wheeeeee!
atob SGVsbG8gd29ybGQh
请注意doskey
在批处理脚本中不起作用 - 仅限控制台。如果您想在批处理脚本中使用它,请创建一个函数。
@echo off
setlocal
call :btoa b64[0] "Hello world!"
call :btoa b64[1] "This is fun."
call :btoa b64[2] "wheeeeee!"
call :atob b64[3] SGVsbG8gd29ybGQh
set b64
goto :EOF
:btoa <var_to_set> <str>
for /f "delims=" %%I in (
'powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"%~2\"))"'
) do set "%~1=%%I"
goto :EOF
:atob <var_to_set> <str>
for /f "delims=" %%I in (
'powershell "[Text.Encoding]::UTF8.GetString([convert]::FromBase64String(\"%~2\"))"'
) do set "%~1=%%I"
goto :EOF
或者,如果您更喜欢批处理+ JScript混合:
@if (@CodeSection==@Batch) @then
@echo off & setlocal
call :btoa b64[0] "Hello world!"
call :btoa b64[1] "This is fun."
call :btoa b64[2] "wheeeeee!"
call :atob b64[3] SGVsbG8gd29ybGQh
set b64
goto :EOF
:btoa <var_to_set> <str>
:atob <var_to_set> <str>
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" %0 "%~2"') do set "%~1=%%I"
goto :EOF
@end // end batch / begin JScript hybrid code
var htmlfile = WSH.CreateObject('htmlfile');
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=10" />');
WSH.Echo(htmlfile.parentWindow[WSH.Arguments(0).substr(1)](WSH.Arguments(1)));
编辑:@Hackoo的批处理+ VBScript混合:
<!-- : batch portion
@echo off & setlocal
call :btoa b64[0] "Hello world!"
call :btoa b64[1] "This is fun."
call :btoa b64[2] "wheeeeee!"
call :atob b64[3] SGVsbG8gd29ybGQh
set b64
goto :EOF
:btoa <var_to_set> <str>
:atob <var_to_set> <str>
for /f "delims=" %%I in ('cscript /nologo "%~f0?.wsf" %0 "%~2"') do set "%~1=%%I"
goto :EOF
: VBScript -->
<job>
<script language="VBScript">
Set htmlfile = WSH.CreateObject("htmlfile")
htmlfile.write("<meta http-equiv='x-ua-compatible' content='IE=10' />")
if WSH.Arguments(0) = ":btoa" then
WScript.Echo htmlfile.parentWindow.btoa(WSH.Arguments(1))
else
WScript.Echo htmlfile.parentWindow.atob(WSH.Arguments(1))
end if
</script>
</job>
答案 1 :(得分:9)
根据对该问题的评论,您可以使用certutil。例如,
certutil -encode raw.txt encoded.txt
或
certutil -f -encode raw.txt encoded.txt
-f
表示“强制覆盖”。否则,如果输出文件(上面的encoded.txt)已经存在,您将收到错误。
但是,这会将输出格式化为encoded.txt文件,就像它是证书PEM文件一样,包含BEGIN和END行,以及最大字符处的分割线。因此,您需要在批处理场景中进行进一步处理,如果字符串很长则需要一些额外的工作。
答案 2 :(得分:6)
This script可以解码/编码来自XP及以上版本的每台机器上的base64字符串,而无需安装.net或Internet Explorer 10 / 11.它甚至可以处理特殊的javascript转义符号:
// result is IkhlbGxvIg==
base64.bat -encode "\u0022Hello\u0022" -eval yes
// result is SGVsbG8=
base64.bat -encode "Hello"
这个接受一个参数 - 你要编码为base 64的字符串并打印结果(但至少需要安装Internet Explorer 10):
@echo off
setlocal
set "string=%~1"
::echo %string%^|mshta.exe "%~f0"
for /f "delims=" %%# in ('echo %string%^|mshta.exe "%~f0"') do (
set b64=%%#
)
set b64
endlocal&exit /b %errorlevel%
<HTA:Application
ShowInTaskbar = no
WindowsState=Minimize
SysMenu=No
ShowInTaskbar=No
Caption=No
Border=Thin
>
<meta http-equiv="x-ua-compatible" content="ie=10" />
<script language="javascript" type="text/javascript">
window.visible=false;
window.resizeTo(1,1);
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
var fso2= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
var string=fso2.ReadLine();
var encodedString = btoa(string);
fso.Write(encodedString);
window.close();
</script>
答案 3 :(得分:2)
如果您已安装Windows的OpenSSL,则可以使用此代码对字符串“ Hello”进行编码:
echo | set /p="Hello" | openssl base64
| set /p=
用于禁止回显通常输出的换行符。
这将产生与bash中相同的结果:
echo -n 'Hello' | openssl base64
输出:
SGVsbG8=
答案 4 :(得分:1)
请注意,
doskey
在批处理脚本中无效,仅在控制台中有效。如果要在批处理脚本中使用它,请创建一个函数
或使用macro:
@echo off
====SETLOCAL DisableDelayedExpansion EnableExtensions
REM Initalize
set ^"LF=^
%===EXPANDS TO NOTHING===%
"
::\n is an escaped LF + caret for line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
REM MACRO
set ^"$b64.encode=FOR %%$ in (%%$ MainMacro) do if "%%$" == "MainMacro" (%\n%
^>nul %__APPDIR__%certutil.exe -f -encodehex args.tmp proc.tmp 0x40000001%\n%
type proc.tmp%\n%
echo(%\n%
del args.tmp proc.tmp%\n%
) 2^>nul ELSE ^<nul ^>args.tmp set/p="
REM EXAMPLES
%$b64.encode%"=I WILL FAIL (string cannot start with =)"
%$b64.encode%^" leading spaces/tabs will be stripped%\n%
but other characters are%\n%
OK!%\n%
;%%~dp$^&^|"""""""
字符串不能以<SPACE> <TAB> <0xFF> =
开头
因为SET /P
用于write without trailing CRLF。
@dbenham 提到了undocumented verbs of CERTUTIL
。 CryptBinaryToStringA
函数的类型0x40000001
被记录为:
请勿在编码后的字符串后添加任何换行符。默认行为是使用回车/换行(CR / LF)对(0x0D / 0x0A)表示新行。
Windows Server 2003和Windows XP:不支持此值。
答案 5 :(得分:0)
这可以(在技术上)完全在批处理中完成,方法是从批处理中创建加密和解密VBS脚本,并可以使用您希望加密的变量来调用它们。在文件路径中添加第二个理由,您可以存储加密的数据以供以后解密/使用。
注意: -需要超时以在执行和清理之间提供足够的时间 下一个实例发生之前的脚本。
-在“解密程序”中,是否选择重新加密文件是可选的。如果您坚持将数据分配给变量后自动重新加密文件,则需要确保不要再次对该文件进行加密,否则数据将无法恢复。
::: Prior to CALLING Encrypter.bat:
Set yourfilepath=myfilepath
Set Cryptme=Name of Variable to be En/De crypted
Set Varname=DataForEncryption
::: CALL "Encrypter.bat" %yourfilepath% %Cryptme% %varname%
'Encrypter.bat'
@ECHO OFF
Set myfilepath=%~1
Set crypting=%~2
Set encrypt=%~3
<"%myfilepath%" (
Set /p encrypt=
)
(
ECHO Dim objFSO 'File System Object
ECHO Set objFSO = CreateObject("Scripting.FileSystemObject"^)
ECHO Dim objTS 'Text Stream Object
ECHO Const ForWriting = 2
ECHO Set objTS = objFSO.OpenTextFile("%myfilepath%", ForWriting, True^)
ECHO objTS.Write(encode("%encrypt%"^)^)
ECHO wscript.sleep "1000"
ECHO function encode(s^)
ECHO For i = 1 To Len(s^)
ECHO newtxt = Mid( s, i, 1^)
ECHO newtxt = Chr(Asc(newtxt^) + 3^)
ECHO coded = coded + (newtxt^)
ECHO Next
ECHO encode = coded
ECHO End function
ECHO objTS.Close(^)
ECHO Set bjFSO = Nothing 'Destroy the object.
ECHO Set objTS = Nothing 'Destroy the object.
) >encrypter.vbs
START encrypter.vbs
TIMEOUT 2 >nul
DEL /Q encrypter.vbs
GOTO :EOF
当您准备调用“ Decrypter.bat”时
Set Cryptme=Name of Variable to be assigned after decrypting stored data.
Set yourfilepath=The filepath you stored the encrypted Data to.
CALL "decrypt.bat" %yourfilepath% %Cryptme%
'Decrypter.bat'
@ECHO OFF
Set myfilepath=%~1
Set crypting=%~2
<"%myfilepath%" (
Set /p decrypt=
)
(
ECHO Dim objFSO 'File System Object
ECHO Set objFSO = CreateObject("Scripting.FileSystemObject"^)
ECHO Dim objTS 'Text Stream Object
ECHO Const ForWriting = 2
ECHO Set objTS = objFSO.OpenTextFile("%myfilepath%", ForWriting, True^)
ECHO objTS.Write(encode("%decrypt%"^)^)
ECHO wscript.sleep "1000"
ECHO function encode(s^)
ECHO For i = 1 To Len(s^)
ECHO newtxt = Mid( s, i, 1^)
ECHO newtxt = Chr(Asc(newtxt^) - 3^)
ECHO coded = coded + (newtxt^)
ECHO Next
ECHO encode = coded
ECHO End function
ECHO objTS.Close(^)
ECHO Set bjFSO = Nothing 'Destroy the object.
ECHO Set objTS = Nothing 'Destroy the object.
) >decrypter.vbs
START decrypter.vbs
timeout 2 >nul
DEL /Q decrypter.vbs
::: Assign decrypted data as value to the Variable used when Calling.
<"%myfilepath%" (
Set /p %crypting%=
)
::: Re-encrypt the data after assigning to variable (optional)
CALL "Encrypter.bat" %yourfilepath% %Cryptme% %varname%
GOTO :EOF
答案 6 :(得分:0)
使用Powershell的最佳,可靠...。但是,文本的长度有一些限制。
Unicode
@echo off
set "string=Everything will be fine"
for /f "tokens=* delims=" %%i in ('powershell [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("""%string%"""^)^)') do set "encoded=%%i"
echo %encoded%
UTF8
@echo off
set "string=Everything will be fine"
for /f "tokens=* delims=" %%i in ('powershell [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("""%string%"""^)^)') do set "encoded=%%i"
echo %encoded%