当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell - Base64 编码

作者:精品下载站 日期:2024-12-14 17:01:17 浏览:10 分类:玩电脑

PowerShell - Base64 编码


“[System.Convert]”类的“ToBase64String()”方法用于在 PowerShell 中使用 Base64 编码将二进制数据转换为基于文本的数据。

Base64 编码通过将数据转换为 Radix 64 表示形式来表示 ASCII 字符串格式的二进制数据。 Base64 编码是一组二进制到文本的编码方案。

当我们想要以 ASCII 字符串格式通过媒体或网络传输数据以使数据在传输过程中保持完整时,可以使用 Base 64 编码

在本文中,我们将讨论如何对图像、命令、zip 文件和 utf8 使用 PowerShell base64 编码。

让我们通过示例了解 PowerShell 中的 base64 编码

PowerShell Base64 编码文件内容

使用 PowerShell 脚本将文件作为输入,并使用 Get-Content cmdlet 读取文件的内容。

PowerShell 对文件内容进行 base64 编码,并将文件保存到 .64 文件格式的位置。

$FileName = "D:\Logs-FTP01\EncodeLogic.txt"

# Get content of the file
$FileContent = Get-Content $FileName

#Get the bytes of the file content with encode
$fileContentInBytes = [System.Text.Encoding]::UTF8.GetBytes($FileContent)

# Base64 Encode file 
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentInBytes)

# Save Base64 Enconde file with extension .b64
$fileContentEncoded | Set-content ($fileName + ".b64")

Write-Host $FileName + ".b64" + "File Encoded Successfully!"

在上面对文件进行 Base64 编码的 PowerShell 脚本中,

$FileName - 包含文件路径

$FileContent - 包含使用 Get-Content cmdlet 的文件内容

$fileContentInBytes - 包含有关文件内容的字节,使用 UTF8.GetBytes() 方法获取它

$fileContentEncoded - 使用 ToBase64String() 方法包含 base64 编码文件内容,该方法接受 $fileContentsInBytes 参数并将文件转换为 base64。

使用Set-Content cmdlet,将base64 编码的文件内容输出到指定位置的文件中。

酷提示:如何在 PowerShell 中查找大尺寸文件!

PowerShell Base64 编码字符串

让我们通过 PowerShell 中的 base64 编码字符串的示例来理解。

例如,如果您想对字符串进行 base64 编码 ,请使用以下脚本

$StringMsg = "PowerShell Base64 Encode Example"

# Gets the bytes of String
$StringBytes = [System.Text.Encoding]::Unicode.GetBytes($StringMsg)

# Encode string content to Base64 string
$EncodedString =[Convert]::ToBase64String($StringBytes)

Write-Host "Encode String: " $EncodedString

在上面将字符串转换为 base64 的 PowerShell 脚本中,

$StringMsg - 变量包含字符串内容

$StringBytes - 变量包含使用 Unicode.GetBytes() 给定字符串的字节

使用 ToBase64String() 方法,它将字节转换为 Base64 编码的字符串并将其打印在控制台上。

您可以使用FromBase64String()方法从base64编码的字符串进行转换。

$DecodedString = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedString))
Write-Output $DecodedString

在上面解码 Base64 字符串的 PowerShell 脚本中,

它使用[系统. Convert]::FromBase64String方法,接受base64编码字符串作为输入参数。

使用 [System.Text.Encode]::Unicode.GetString() 方法从 PowerShell 中的 Base64 字符串进行转换。

上述命令的输出是:

[玩转系统] PowerShell - Base64 编码

PowerShell Base64 编码图像

您可以轻松地将图像转换为base64编码的字符串,如下所示

[convert]::ToBase64String((Get-Content D:\Logs-FTP01\powershell-mkdir.png -Encoding byte)) 

在上面的脚本中,PowerShell Base64 Encoding 将图像转换为 Base64 字符串。

PowerShell Get-Content cmdlet 读取图像文件内容并将图像转换为 base64 字符串。

酷提示:如何在命令提示符下使用 net user 命令!

PowerShell Base64 编码 zip 文件

如果您想对 zip 文件进行 Base64 编码,请使用下面的 PowerShell 脚本

$zipfile = "D:\Logs-FTP01\EncodeLogic.zip"

$zipfilebytes = Get-Content $zipfile -Encoding byte
$zipfileBytesBase64 = [System.Convert]::ToBase64String($zipfilebytes)
$zipfileBytesBase64 | Out-File 'D:\Logs-FTP01\EncodeLogic.txt'

在上面的 PowerShell 脚本中,

$zipFile - 包含 zip 文件路径

$zipfilebytes - 包含字节信息,Get-Content cmdlet 读取 zip 文件内容

$zipfileBytesBase64 - 变量包含 zip 文件的 Base64 编码字符串。

Base64 将 zip 文件内容编码为指定路径的 .txt 文件。

酷提示:如何在 PowerShell 中列出目录中的文件!

结论

在上面的文章中,我解释了有关图像、字符串、文件内容的 PowerShell Base64 编码或 zip 文件的 Base64 编码。

在PowerShell中使用[System.Convert].ToBase64String,您可以将文件转换为base64,并将字符串转换为base64编码的字符串。

您可以阅读有关使用 PowerShell mkdir 创建新目录或使用 New-Item cmdlet 创建目录(如果不存在)并将多个参数传递给函数以创建目录的更多信息。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯