下面的代码创建一个包含反向密码的哈希表。我正在尝试对哈希表预先形成一个查找,并返回结果。我正在尝试解密提供给我的XML文件中的内容,下面是我在powershell中创建的脚本。
$translation = [ordered]@{}
$alpha = 'A'..'Z'
For($i=0; $i -lt 26; $i++)
{
$translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}XML文件包含:
<?xml version="1.0" encoding="UTF-8"?>
<translate>
<caesar_cipher>
<offset>-7</offset>
<cipher>
RFGNCTAZITALFGB FG BZRRPBBOZIIV RFHEIPALGN AMP TQYTGRPQ EFXPCBMPII
TBBLNGHPGA
</cipher>
</caesar_cipher>
</translate>您知道如何在哈希表中预先形成一个查找来解密XML文件中的消息,然后输出到powershell吗?
发布于 2018-11-21 21:27:46
# The input XML.
$xml = @'
<?xml version="1.0" encoding="UTF-8"?>
<translate>
<caesar_cipher>
<offset>-7</offset>
<cipher>
RFGNCTAZITALFGB FG BZRRPBBOZIIV RFHEIPALGN AMP TQYTGRPQ EFXPCBMPII
TBBLNGHPGA
</cipher>
</caesar_cipher>
</translate>
'@
# Read the XML text into an XML DOM (XmlDocument).
$doc = [xml] $xml
# Extract the offset needed to build the translation hashtable.
$offset = $doc.translate.caesar_cipher.offset
# Build the translation hashtable, which maps a given [char] instance
# to a different [char] instance.
# (The hashtable doesn't strictly need to be *ordered*.)
$translation = [ordered] @{}
# NOTE: 'A'..'Z' only works in PowerShell *Core*.
# In *Windows PowerShell* you must use construct the array via *code points*,
# as shown in the next statement.
$alpha = [char[]] (65..90)
for($i=0; $i -lt 26; $i++) {
$translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}
# Add an entry to pass spaces through as-is.
$translation[[char] ' '] = [char] ' '
# Extract the text to be deciphered.
$cipherText = $doc.translate.caesar_cipher.cipher
# Translate the individual characters and reassemble them into a string.
-join $translation[[char[]] $cipherText]上述收益率:
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT 并祝贺你成功地让我们为你做家庭作业。
注意:
- For background information and caveats, see [this answer](https://stackoverflow.com/a/49213568/45375).
- If you need sophisticated _queries_ agains the XML DOM, use the [`Select-Xml` cmdlet](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml), which is [XPath](http://en.wikipedia.org/wiki/XPath)-based.
[char]实例视为[string]实例,但在哈希表查找的情况下则不会:哈希表查找必须使用与键的数据类型相同的数据类型,这就是为什么$cipherText被转换为[char[]]来查找[char]实例,以及为什么为上面的空格添加条目使用显式[char]强制转换来定义键。..的数字端点,而PowerShell Core也支持字符。发布于 2018-11-21 21:27:46
你的剧本有几个问题。首先,你这一代的密码数学是错误的。您想要从xml中提取偏移量。您还希望捕获散列中的空格字符,否则在执行查找时它将返回$null。这对于任何其他非alpha字符都是正确的,除非您已经定义了它们。一旦解决了这个问题,您所需要做的就是执行查找,并将字符重新组合在一起。您可以在字典中执行多个查找,方法是在PowerShell中传递一个数组,如下所示。
# initialize alpha and hash lookups
$alpha = 'A'..'Z'
$decipher = @{ ' ' = ' ' }
# load prerequisite variables
$xml = [xml]@'
<?xml version="1.0" encoding="UTF-8"?>
<translate>
<caesar_cipher>
<offset>-7</offset>
<cipher>
RFGNCTAZITALFGB FG BZRRPBBOZIIV RFHEIPALGN AMP TQYTGRPQ EFXPCBMPII TBBLNGHPGA
</cipher>
</caesar_cipher>
</translate>
'@
$offset = [int]$xml.translate.caesar_cipher.offset
$cipher = $xml.translate.caesar_cipher.cipher
# generate decipher table
0..($alpha.Length - 1) | % {$decipher["$($alpha[(-$_ + $offset) % $alpha.Length])"] = $alpha[$_]}
# perform lookups
-join $decipher[$cipher -split '']发布于 2018-11-21 21:15:23
正如TheIncorrigible1所建议的,您可以使用XPath表达式//cipher/text()来选择所需的文本节点。
$xml = [xml](Get-Content path\to\file.xml)
$Ciphers = $xml.SelectNodes('//cipher/text()').InnerText
foreach($Cipher in $Ciphers){
Write-Host "Cipher text:`n$Cipher" -ForegroundColor Magenta
Write-Host "Decrypted text:`n$(-join ($Cipher.ToCharArray() |ForEach-Object {
# if it's in the set A..Z, translate
if($_ -in $alpha){
$_ = $translation[$_]
}
$_
}))" -ForegroundColor Green
}https://stackoverflow.com/questions/53420312
复制相似问题