[玩转系统] PowerShell Base64 | PowerShell Base64 | PowerShell Base64 PowerShell Base64 示例
作者:精品下载站 日期:2024-12-14 04:48:47 浏览:16 分类:玩电脑
PowerShell Base64 | PowerShell Base64 | PowerShell Base64 PowerShell Base64 示例
PowerShell Base64 简介
PowerShell Base64 是一种用于编码和解码数据的技术或机制。为了防止数据受到恶意软件攻击,编码和解码非常重要。 Base64编码和解码是一种流行的数据加密和解密方法。顾名思义,Base64 字符串将有 64 个字符。它是大写字母、小写字母和数字的组合,末尾最多有两个等号。
语法:
下面是Base64编码和解码的语法:
编码:
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("TextToEncode"))
解码:
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String(texttobedecoded'))
PowerShell Base64 示例
下面给出了 PowerShell Base64 的示例:
例子#1
代码:
编码:
$input = ‘text to be encoded’
$By = [System.Text.Encoding]::Unicode.GetBytes($input)
$output =[Convert]::ToBase64String($By)
$output
输出:
解码:
$input = “vikivikivikivikivikivyapvyapvyapvyapvyapnandnandnandnandnandviki”
$output = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($input))
$output
输出:
例子#2
对文件进行编码和解码。
代码:
clear-host
Write-Host "Welcome to the example of powershell base64 encoding and decoding" -ForegroundColor Green
Write-Host "Encoding of a text file"
# Encoding a file content
$inputfile = "C:\Vignesh\Names.txt"
$fc = get-content $inputfile
$By = [System.Text.Encoding]::UTF8.GetBytes($fc)
$etext = [System.Convert]::ToBase64String($By)
Write-Host "ENCODED text file content is " $etext -ForegroundColor Green
# Decoding a file content
Write-Host "Decoding the above converted text"
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($etext)) | Out-File -Encoding "ASCII" c:\vignesh\decoded.txt
$outfile123 = get-content c:\vignesh\decoded.txt
Write-Host "Decoded text is as follows" -ForegroundColor Green
Write-Host "DECODED: " $outfile123
输出:
例子#3
代码:
clear-host
Write-Host "Demo of Encoding and decoding of an exe file" -ForegroundColor Green
Write-Host "Encoding of an exe file" -ForegroundColor Green
# Encode
$fp = "C:\Vignesh\helloworld.exe"
$encf = [System.IO.File]::ReadAllBytes($fp);
# returns the base64 string
$b64str = [System.Convert]::ToBase64String($encf);
Write-Host "After encoding" -ForegroundColor Green
$b64str
# Decode
#function to decode
function Convert-stob {
[CmdletBinding()]
param (
[string] $estr
, [string] $fp = (‘{0}\{1}’ -f $env:TEMP, [System.Guid]::NewGuid().ToString())
)
try {
if ($estr.Length -ge 1) {
Write-Host "After decoding of exe" -ForegroundColor Green
# decodes the base64 string
$barr = [System.Convert]::FromBase64String($estr);
[System.IO.File]::WriteAllBytes($fp, $barr);
Write-Host $barr
}
}
catch {
}
Write-Output -InputObject (Get-Item -Path $fp);
}
$DecodedFile = Convert-stob -estr $b64str -fp C:\Vignesh\helloworld.exe
输出:
例子#4
解码图像文件。
代码:
Write-Host "Encoding an image file"
#function to encode to base64
function ctob64
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript({ Test-Path -Path $_ })]
[String]$fp
)
Write-Host "Encoding the image file" -ForegroundColor Green
[System.convert]::ToBase64String((Get-Content -Path $fp -Encoding Byte))
}
#decode function of an image file
function cfb64
{
[CmdletBinding()]
param (
[parameter(Mandatory = $true, ValueFromPipeline)]
[String]$str
)
try
{
Write-Host "Decoding image file" -ForegroundColor Green
[System.Text.Encoding]::Default.GetString([System.Convert]::FromBase64String($str))
}
catch
{
Write-Host "Error occurred" -ForegroundColor Red
$Error[0].Exception.Message
}
}
输出:
例子#5
代码:
function Testhex
{
[CmdletBinding()] Param
(
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[Alias("FN","FP")] $pa,
[Int] $wd = 16,
[Int] $co = -1,
[String] $ph = ".",
[Switch] $noo,
[Switch] $not
)
Write-Host "Demo of encoding to hex values" -ForegroundColor Green
$lc = 0
get-content $pa -encoding by -readcount $wd -totalcount $co |
foreach-object `
{
$pad = $txt = $null
$bys = $_
foreach ($by in $bys)`
{
$byh = [String]::Format("{0:X}", $by)
$pad += $byh.PadLeft(2,"0") + " "
}
if ($pad.length -lt $width * 5)
{ $pad = $pad.PadRight($width * 5," ") }
foreach ($by in $bys)`
{
if ( [Char]::IsLetterOrDigit($by) -or
[Char]::IsPunctuation($by) -or
[Char]::IsSymbol($by) )
{ $txt += [Char] $by }
else
{ $txt += $placeholder }
}
$ottt = [String]::Format("{0:X}", $lc)
$ottt = $ottt.PadLeft(8,"0") + "h:"
$lc += $width # Increment lc.
if (-not $noo) { $pad = "$ottt $pad" }
if (-not $not) { $pad = $pad + $txt }
Write-Host "Hex decimal values" -ForegroundColor Green
$pad
}
}
Testhex C:\Vignesh\helloworld.exe
输出:
例子#6
代码:
function encodecsv{
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string]
$fn
)
Write-Host "encoding a csv file using base64" -ForegroundColor Green
if (Test-Path "$fn"){
$ct = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes([System.IO.File]::ReadAllText("$fn")))
Write-Host "encoded output is below" -ForegroundColor Green
return $fn, $ct
}
throw "File not found $fn"
}
#Example
encodecsv C:\Vignesh\Sample.csv
输出:
例子#7
代码:
Add-Type -AssemblyName System.Web
Write-Host "Demo of encoding and decoding urls using base64"
Write-Host "Example of encoding url"
$testurl="https://test.test.com/test/TestSite1/Shared/"
Write-Host "url before encoding " $testurl -ForegroundColor Green
#The below code is used to encode the URL
Write-Host "encoding url...."
$utoe = $testurl
$res = [System.Web.HttpUtility]::UrlEncode($utoe)
Write-Host "after encoding,the url is" $res -ForegroundColor Green
#decoding url.
$utod = $res
Write-Host "Decoding the above encoded url" -ForegroundColor Green
$dcu = [System.Web.HttpUtility]::UrlDecode($utod)
Write-Host "after decoding, the url is " $dcu -ForegroundColor Green
#Decode URL code ends here.
输出:
例子#8
代码:
Write-Host "Demo of encoding a pdf file" -ForegroundColor Green
Write-Host "Read the pdf" -ForegroundColor Green
Write-Host "After encoding" -ForegroundColor Green
$pdffile = Get-Content "C:\Vignesh\Vyapini Birth Certificate.pdf"
$bytestest = [System.Text.Encoding]::ASCII.GetBytes($pdffile)
$base641 =[Convert]::ToBase64String($bytestest)
输出:
结论
因此,本文详细介绍了 PowerShell 中的 Base64 编码技术。它展示了各种文件类型、字符串、pdf 文件、csv 文件等编码和解码的示例。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag