如何在 PowerShell 中将哈希字符串转换为字节数组? [英] How to convert a hash string to byte array in PowerShell?
问题描述
当我的脚本运行时,我读取了一个哈希值,我想将它写入注册表.我发现以下命令可以做到:
New-ItemProperty $RegPath -Name $AttrName -PropertyType Binary -Value $byteArray
我还找到了如何使用 PowerShell 设置二进制注册表值 (REG_BINARY)?.
然而,所有答案都假设字符串的形式为:
"50,33,01,00,00,00,00,00,...."
但我只能以以下形式读取我的哈希:
F5442930B1778ED31A....."
我不知道如何将其转换为字节数组,其值为 F5、44 等.
vonPryz 明智地建议简单地将哈希直接存储为注册表中的字符串(REG_SZ
).
如果您确实想将数据存储为 REG_BINARY
类型,即作为 字节 的数组,则必须在字符串表示之间来回转换.>
要将转换为[byte[]]
数组(使用缩短的示例哈希字符串):
PS>[byte[]] -split ('F54429' -replace '..', '0x$&')245 # 第一个字节:0xF5 的十进制表示68 # 第二个字节:0x44 的十进制表示41#...
以上是 PowerShell 对结果数组的默认输出表示[byte[]] (0xf5, 0x44, 0x29)
.
要从转换[byte[]]
数组(返回字符串;PSv4+ 语法):
PS>-join ([byte[]] (0xf5, 0x44, 0x29)).ForEach('ToString', 'X2')F54429
.ForEach('ToString', 'X2')
相当于调用 .ToString('X2')
- 即请求一个 hex 表示 left-0
-padded 到 2 位数字 - 在每个数组元素上并收集结果字符串.-join
然后通过直接连接将这些字符串连接成单个字符串.
综合起来:
# 示例哈希字符串.$hashString = 'F54429'# 将哈希字符串转换为字节数组.$hashByteArray = [byte[]] ($hashString -replace '..', '0x$&,' -split ',' -ne '')# 从字节数组创建一个 REG_BINARY 注册表值.Set-ItemProperty -LiteralPath HKCU:\ -Name tmp -Type Binary -Value $hashByteArray# 从注册表读回字节数组 (PSv5+)$hashByteArray2 = Get-ItemPropertyValue -LiteralPath HKCU:\ -Name tmp# 将其转换回字符串.$hashString2 = -join $hashByteArray2.ForEach('ToString', 'X2')# (清理.)Remove-ItemProperty -LiteralPath HKCU:\ -Name tmp
When my scripts run, I read a hash, and I would like to write it to the registry. I figured out that the following command will do it:
New-ItemProperty $RegPath -Name $AttrName -PropertyType Binary -Value $byteArray
I also found the How to set a binary registry value (REG_BINARY) with PowerShell?.
However all answers suppose that the string is in form of:
"50,33,01,00,00,00,00,00,...."
but I only can read my hash in the following form:
"F5442930B1778ED31A....."
I can not figure out, how can I convert this to a byte array, with values F5, 44 etc.
vonPryz sensibly suggests simply storing the hash directly as a string (REG_SZ
) in the registry.
If you really want to store the data as type REG_BINARY
, i.e., as an array of bytes, you must convert back and forth between the string representation.
To convert to a [byte[]]
array (using a shortened sample hash string):
PS> [byte[]] -split ('F54429' -replace '..', '0x$& ')
245 # 1st byte: decimal representation of 0xF5
68 # 2nd byte: decimal representation of 0x44
41 # ...
The above is PowerShell's default output representation of resulting array [byte[]] (0xf5, 0x44, 0x29)
.
To convert from a [byte[]]
array (back to a string; PSv4+ syntax):
PS> -join ([byte[]] (0xf5, 0x44, 0x29)).ForEach('ToString', 'X2')
F54429
.ForEach('ToString', 'X2')
is the equivalent of calling .ToString('X2')
- i.e., requesting a hex representation left-0
-padded to 2 digits - on each array element and collecting the resulting string. -join
then joins these strings to a single string by direct concatenation.
To put it all together:
# Sample hash string.
$hashString = 'F54429'
# Convert the hash string to a byte array.
$hashByteArray = [byte[]] ($hashString -replace '..', '0x$&,' -split ',' -ne '')
# Create a REG_BINARY registry value from the byte array.
Set-ItemProperty -LiteralPath HKCU:\ -Name tmp -Type Binary -Value $hashByteArray
# Read the byte array back from the registry (PSv5+)
$hashByteArray2 = Get-ItemPropertyValue -LiteralPath HKCU:\ -Name tmp
# Convert it back to a string.
$hashString2 = -join $hashByteArray2.ForEach('ToString', 'X2')
# (Clean up.)
Remove-ItemProperty -LiteralPath HKCU:\ -Name tmp
这篇关于如何在 PowerShell 中将哈希字符串转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!