首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hashtable查找

Hashtable查找
EN

Stack Overflow用户
提问于 2018-11-21 20:53:32
回答 3查看 429关注 0票数 0

下面的代码创建一个包含反向密码的哈希表。我正在尝试对哈希表预先形成一个查找,并返回结果。我正在尝试解密提供给我的XML文件中的内容,下面是我在powershell中创建的脚本。

代码语言:javascript
复制
$translation = [ordered]@{}
$alpha = 'A'..'Z'
For($i=0; $i -lt 26; $i++)
{
 $translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}

XML文件包含:

代码语言:javascript
复制
<?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吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-21 21:27:46

代码语言:javascript
复制
# 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]

上述收益率:

代码语言:javascript
复制
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT    

并祝贺你成功地让我们为你做家庭作业。

注意:

  • PowerShell自动提供方便的点表示法 to ,向下钻取到XML 。
代码语言:javascript
复制
- 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.

  • Pitfall:PowerShell通常会根据需要自动将[char]实例视为[string]实例,但在哈希表查找的情况下则不会:哈希表查找必须使用与键的数据类型相同的数据类型,这就是为什么$cipherText被转换为[char[]]来查找[char]实例,以及为什么为上面的空格添加条目使用显式[char]强制转换来定义键。
  • Windows PowerShell只支持范围运算符..的数字端点,而PowerShell Core也支持字符。
票数 3
EN

Stack Overflow用户

发布于 2018-11-21 21:27:46

你的剧本有几个问题。首先,你这一代的密码数学是错误的。您想要从xml中提取偏移量。您还希望捕获散列中的空格字符,否则在执行查找时它将返回$null。这对于任何其他非alpha字符都是正确的,除非您已经定义了它们。一旦解决了这个问题,您所需要做的就是执行查找,并将字符重新组合在一起。您可以在字典中执行多个查找,方法是在PowerShell中传递一个数组,如下所示。

代码语言:javascript
复制
# 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 '']
票数 1
EN

Stack Overflow用户

发布于 2018-11-21 21:15:23

正如TheIncorrigible1所建议的,您可以使用XPath表达式//cipher/text()来选择所需的文本节点。

代码语言:javascript
复制
$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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53420312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档