Hashtable Lookup
The code below creates a hash table containing a reversed cipher. I am trying to preform a lookup on the hash table, and return the results. I am trying to decrypt the content inside of an XML file provided to me, with the script i've created below in powershell.
$translation = [ordered]@{}
$alpha = 'A'..'Z'
For($i=0; $i -lt 26; $i++)
{
$translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}
The XML file contains:
<?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>
Any idea how to preform a lookup in the hash table to decrypt the message within the XML file, outputting to powershell?
powershell hashtable caesar-cipher
add a comment |
The code below creates a hash table containing a reversed cipher. I am trying to preform a lookup on the hash table, and return the results. I am trying to decrypt the content inside of an XML file provided to me, with the script i've created below in powershell.
$translation = [ordered]@{}
$alpha = 'A'..'Z'
For($i=0; $i -lt 26; $i++)
{
$translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}
The XML file contains:
<?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>
Any idea how to preform a lookup in the hash table to decrypt the message within the XML file, outputting to powershell?
powershell hashtable caesar-cipher
Duplicate of the question here
– TheIncorrigible1
Nov 21 '18 at 21:47
4
Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted.
– K.Dᴀᴠɪs
Nov 21 '18 at 22:17
add a comment |
The code below creates a hash table containing a reversed cipher. I am trying to preform a lookup on the hash table, and return the results. I am trying to decrypt the content inside of an XML file provided to me, with the script i've created below in powershell.
$translation = [ordered]@{}
$alpha = 'A'..'Z'
For($i=0; $i -lt 26; $i++)
{
$translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}
The XML file contains:
<?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>
Any idea how to preform a lookup in the hash table to decrypt the message within the XML file, outputting to powershell?
powershell hashtable caesar-cipher
The code below creates a hash table containing a reversed cipher. I am trying to preform a lookup on the hash table, and return the results. I am trying to decrypt the content inside of an XML file provided to me, with the script i've created below in powershell.
$translation = [ordered]@{}
$alpha = 'A'..'Z'
For($i=0; $i -lt 26; $i++)
{
$translation[$alpha[((((-$i + $offset) % 26) + 26) % 26)]] = $alpha[$i]
}
The XML file contains:
<?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>
Any idea how to preform a lookup in the hash table to decrypt the message within the XML file, outputting to powershell?
powershell hashtable caesar-cipher
powershell hashtable caesar-cipher
edited Nov 21 '18 at 22:35
mklement0
128k20241270
128k20241270
asked Nov 21 '18 at 20:53
bear443bear443
83
83
Duplicate of the question here
– TheIncorrigible1
Nov 21 '18 at 21:47
4
Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted.
– K.Dᴀᴠɪs
Nov 21 '18 at 22:17
add a comment |
Duplicate of the question here
– TheIncorrigible1
Nov 21 '18 at 21:47
4
Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted.
– K.Dᴀᴠɪs
Nov 21 '18 at 22:17
Duplicate of the question here
– TheIncorrigible1
Nov 21 '18 at 21:47
Duplicate of the question here
– TheIncorrigible1
Nov 21 '18 at 21:47
4
4
Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted.
– K.Dᴀᴠɪs
Nov 21 '18 at 22:17
Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted.
– K.Dᴀᴠɪs
Nov 21 '18 at 22:17
add a comment |
3 Answers
3
active
oldest
votes
# 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]
The above yields:
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT
And congratulations on successfully making us do your homework for you.
Note:
PowerShell automatically provides convenient dot notation (
.
) to drill down into XML documents.
- For background information and caveats, see this answer.
- If you need sophisticated queries agains the XML DOM, use the
Select-Xml
cmdlet, which is XPath-based.
Pitfall: PowerShell often automatically treats
[char]
instances as[string]
instances as needed, but not in the case of hashtable lookups: hashtable lookups must use the same data type as the key's data type, which is why$cipherText
is cast to[char]
to look up[char]
instances and why adding the entry for spaces above uses an explicit[char]
cast to define the key.Windows PowerShell only supports numerical endpoints for
..
, the range operator, whereas PowerShell Core also supports characters.
1
Nice, didn't realize you could[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.
– mklement0
Nov 21 '18 at 22:04
@TheIncorrigible1: Yes, it's effectively the same as$string.ToCharArray()
, but I prefer[char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.
– mklement0
Nov 21 '18 at 22:52
add a comment |
You have a couple issues with your script. First, your generation of the cipher's math is wrong. You want to pull the offset from the xml
. You also want to catch the space character in your hash or it'll return $null
when the lookup is performed. This is true of any other non-alpha characters unless you have them defined. Once this is addressed, all you need to do is perform the lookup and join the characters back together. You can perform multiple lookups in a dictionary by passing an array in PowerShell exemplified below.
# 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 '']
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
add a comment |
As TheIncorrigible1 suggests, you can use the XPath expression //cipher/text()
to select the desired text nodes.
$xml = [xml](Get-Content pathtofile.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
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420312%2fhashtable-lookup%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
# 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]
The above yields:
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT
And congratulations on successfully making us do your homework for you.
Note:
PowerShell automatically provides convenient dot notation (
.
) to drill down into XML documents.
- For background information and caveats, see this answer.
- If you need sophisticated queries agains the XML DOM, use the
Select-Xml
cmdlet, which is XPath-based.
Pitfall: PowerShell often automatically treats
[char]
instances as[string]
instances as needed, but not in the case of hashtable lookups: hashtable lookups must use the same data type as the key's data type, which is why$cipherText
is cast to[char]
to look up[char]
instances and why adding the entry for spaces above uses an explicit[char]
cast to define the key.Windows PowerShell only supports numerical endpoints for
..
, the range operator, whereas PowerShell Core also supports characters.
1
Nice, didn't realize you could[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.
– mklement0
Nov 21 '18 at 22:04
@TheIncorrigible1: Yes, it's effectively the same as$string.ToCharArray()
, but I prefer[char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.
– mklement0
Nov 21 '18 at 22:52
add a comment |
# 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]
The above yields:
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT
And congratulations on successfully making us do your homework for you.
Note:
PowerShell automatically provides convenient dot notation (
.
) to drill down into XML documents.
- For background information and caveats, see this answer.
- If you need sophisticated queries agains the XML DOM, use the
Select-Xml
cmdlet, which is XPath-based.
Pitfall: PowerShell often automatically treats
[char]
instances as[string]
instances as needed, but not in the case of hashtable lookups: hashtable lookups must use the same data type as the key's data type, which is why$cipherText
is cast to[char]
to look up[char]
instances and why adding the entry for spaces above uses an explicit[char]
cast to define the key.Windows PowerShell only supports numerical endpoints for
..
, the range operator, whereas PowerShell Core also supports characters.
1
Nice, didn't realize you could[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.
– mklement0
Nov 21 '18 at 22:04
@TheIncorrigible1: Yes, it's effectively the same as$string.ToCharArray()
, but I prefer[char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.
– mklement0
Nov 21 '18 at 22:52
add a comment |
# 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]
The above yields:
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT
And congratulations on successfully making us do your homework for you.
Note:
PowerShell automatically provides convenient dot notation (
.
) to drill down into XML documents.
- For background information and caveats, see this answer.
- If you need sophisticated queries agains the XML DOM, use the
Select-Xml
cmdlet, which is XPath-based.
Pitfall: PowerShell often automatically treats
[char]
instances as[string]
instances as needed, but not in the case of hashtable lookups: hashtable lookups must use the same data type as the key's data type, which is why$cipherText
is cast to[char]
to look up[char]
instances and why adding the entry for spaces above uses an explicit[char]
cast to define the key.Windows PowerShell only supports numerical endpoints for
..
, the range operator, whereas PowerShell Core also supports characters.
# 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]
The above yields:
CONGRATULATIONS ON SUCCESSFULLY COMPLETING THE ADVANCED POWERSHELL ASSIGNMENT
And congratulations on successfully making us do your homework for you.
Note:
PowerShell automatically provides convenient dot notation (
.
) to drill down into XML documents.
- For background information and caveats, see this answer.
- If you need sophisticated queries agains the XML DOM, use the
Select-Xml
cmdlet, which is XPath-based.
Pitfall: PowerShell often automatically treats
[char]
instances as[string]
instances as needed, but not in the case of hashtable lookups: hashtable lookups must use the same data type as the key's data type, which is why$cipherText
is cast to[char]
to look up[char]
instances and why adding the entry for spaces above uses an explicit[char]
cast to define the key.Windows PowerShell only supports numerical endpoints for
..
, the range operator, whereas PowerShell Core also supports characters.
edited Nov 21 '18 at 22:57
answered Nov 21 '18 at 21:27
mklement0mklement0
128k20241270
128k20241270
1
Nice, didn't realize you could[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.
– mklement0
Nov 21 '18 at 22:04
@TheIncorrigible1: Yes, it's effectively the same as$string.ToCharArray()
, but I prefer[char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.
– mklement0
Nov 21 '18 at 22:52
add a comment |
1
Nice, didn't realize you could[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.
– mklement0
Nov 21 '18 at 22:04
@TheIncorrigible1: Yes, it's effectively the same as$string.ToCharArray()
, but I prefer[char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.
– mklement0
Nov 21 '18 at 22:52
1
1
Nice, didn't realize you could
[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
Nice, didn't realize you could
[char]$string
– TheIncorrigible1
Nov 21 '18 at 21:48
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with
..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.– mklement0
Nov 21 '18 at 22:04
@bear443: Sounds like you're using Windows PowerShell, not PowerShell Core. Only the latter supports using characters with
..
to create ranges of characters. See my update for how to do it in Windows PowerShell. The updated code should now run on Windows PowerShell and PowerShell Core alike.– mklement0
Nov 21 '18 at 22:04
@TheIncorrigible1: Yes, it's effectively the same as
$string.ToCharArray()
, but I prefer [char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.– mklement0
Nov 21 '18 at 22:52
@TheIncorrigible1: Yes, it's effectively the same as
$string.ToCharArray()
, but I prefer [char]
, because it is more PowerShell-idiomatic. Performance-wise, there's virtually no difference.– mklement0
Nov 21 '18 at 22:52
add a comment |
You have a couple issues with your script. First, your generation of the cipher's math is wrong. You want to pull the offset from the xml
. You also want to catch the space character in your hash or it'll return $null
when the lookup is performed. This is true of any other non-alpha characters unless you have them defined. Once this is addressed, all you need to do is perform the lookup and join the characters back together. You can perform multiple lookups in a dictionary by passing an array in PowerShell exemplified below.
# 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 '']
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
add a comment |
You have a couple issues with your script. First, your generation of the cipher's math is wrong. You want to pull the offset from the xml
. You also want to catch the space character in your hash or it'll return $null
when the lookup is performed. This is true of any other non-alpha characters unless you have them defined. Once this is addressed, all you need to do is perform the lookup and join the characters back together. You can perform multiple lookups in a dictionary by passing an array in PowerShell exemplified below.
# 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 '']
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
add a comment |
You have a couple issues with your script. First, your generation of the cipher's math is wrong. You want to pull the offset from the xml
. You also want to catch the space character in your hash or it'll return $null
when the lookup is performed. This is true of any other non-alpha characters unless you have them defined. Once this is addressed, all you need to do is perform the lookup and join the characters back together. You can perform multiple lookups in a dictionary by passing an array in PowerShell exemplified below.
# 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 '']
You have a couple issues with your script. First, your generation of the cipher's math is wrong. You want to pull the offset from the xml
. You also want to catch the space character in your hash or it'll return $null
when the lookup is performed. This is true of any other non-alpha characters unless you have them defined. Once this is addressed, all you need to do is perform the lookup and join the characters back together. You can perform multiple lookups in a dictionary by passing an array in PowerShell exemplified below.
# 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 '']
edited Nov 21 '18 at 21:57
answered Nov 21 '18 at 21:27
TheIncorrigible1TheIncorrigible1
9,59331334
9,59331334
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
add a comment |
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
I am getting this null error frequently. Is there a way to use this in a script like mklement0 commented?
– bear443
Nov 21 '18 at 21:44
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
@bear443 I used this without any error. Sounds like user-error.
– TheIncorrigible1
Nov 21 '18 at 21:46
add a comment |
As TheIncorrigible1 suggests, you can use the XPath expression //cipher/text()
to select the desired text nodes.
$xml = [xml](Get-Content pathtofile.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
}
add a comment |
As TheIncorrigible1 suggests, you can use the XPath expression //cipher/text()
to select the desired text nodes.
$xml = [xml](Get-Content pathtofile.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
}
add a comment |
As TheIncorrigible1 suggests, you can use the XPath expression //cipher/text()
to select the desired text nodes.
$xml = [xml](Get-Content pathtofile.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
}
As TheIncorrigible1 suggests, you can use the XPath expression //cipher/text()
to select the desired text nodes.
$xml = [xml](Get-Content pathtofile.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
}
edited Nov 21 '18 at 22:54
mklement0
128k20241270
128k20241270
answered Nov 21 '18 at 21:15
Mathias R. JessenMathias R. Jessen
56.9k458103
56.9k458103
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420312%2fhashtable-lookup%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Duplicate of the question here
– TheIncorrigible1
Nov 21 '18 at 21:47
4
Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted.
– K.Dᴀᴠɪs
Nov 21 '18 at 22:17