Powershell:直接写入十六进制值?

Powershell:直接写入十六进制值?

我正在尝试创建一个“秘密”密钥。我的理论是写出不可打印的十六进制代码(例如0x02, 0x09, 0x1E...),其中有 12 个这样的代码。然后将它们保存到一个“假”文件中,例如“.sss”(在 Windows 上),该文件应该是不可读的 - 精明的程序员可以接受。如果打开该文件,它将显示“”

我尝试过多种组合,例如使用双引号和单引号,使用“\x02”、“O:x02”或“Ox02”,但它总是输出精确的字符串。我尝试了“Format-Hex”……但它只是输出一个显示字符串的表格,以及“0”、“x”、“0”和“2”的相应十六进制代码

使用我的文本编辑器(具有十六进制编辑器),我可以输入 02 09 1D 1E 等,当切换回文本模式时显示“ ”

使用 Powershell 是否能做到这一点?下面是我的测试脚本

$myPath = Split-Path $PSScriptRoot
$out_file="$myPath\.sss"
$file="0:x020:x090:x1D0:x1E0:x1F\x1c\x81\x8F\x8D\x8F\x9\x9d\xA0"
$file>"$myPath\.sss";
write-host("file to $myPath\.sss")

(后一行只是为了验证保存的位置)我的理论我可以输入类似的内容02 1E 1C 81 8D A0,将其存储在虚假文件中,然后将其加载到程序的另一部分以检查它是否与嵌入在 javascript/文本文件开头的类似代码匹配。

希望这能得到批准,因为我的上一篇帖子被删除了:“这不是剧本写作服务”

这是为了让运行我的其他程序的每台计算机都“独一无二”,这样脚本就不会在其他 PC 上使用。最初,我使用的代码是从注册表读取“DigitalProductId”,但 A) 现在从注册表返回一个空条目,或 B) 显示为“BBBBB BBBBB BBBBB BBBBB BBBBB”


我要感谢那个向我指出方向的人[char]0x09。以下是可能对其他人有所帮助的结果代码。

$myPath = Split-Path $PSScriptRoot

## This part write out all the invisible codes to a file.
$file=[char]0x02+[char]0x09+[char]0x1D+[char]0x1E+[char]0x1F+[char]0x1C+[char]0x81+[char]0X8F+[char]0x8D+[char]0x8F+[char]0x90+[char]0x9d+[char]0xA0
$file|Out-File -NoNewLine "$myPath\.sss";

## The file is saved in the format "FE FF 02 00 09 00 1D 00 ..."
## This part reads the file in again as bytes
[byte[]]$bytes = Get-Content $myPath\.sss -Encoding Byte
$hexString = ($bytes|ForEach-Object ToString X2) -join ''
$tot=$bytes.Count
$tot=($tot*2)-4; 
# Since we start 4 bytes in, we must end 4 bytes early to avoid reading past end of file.
# We double $tot as there are two numbers per array cell

## It skips the BOM at the start (FEFF), and then skips over the "00" entries by moving 4 bytes along, and then taking next two bytes as substring
for ($i=4; $i -le $tot; $i=$i+4 ) {
$output+=$hexString.Substring($i,2)
}

write-host($output);
## In my case, I would use any 3x [char]0x?? to create a unique 6 digit number (1,716 combinations) or 4x (17,160 combinations)

## If users find this file, they will see ".ssa", and Windows will not know how to display the file
## For the few that do, they will see "      " entry
## If people copy the other app to another PC WITHOUT this secret file, it will refuse to run

答案1

您可以使用[char]0xCode语法,例如

[char]0x02
[char]0x09
[char]0x1D

相关内容