Can not decrypt correctly in PowerShell 7.0.3 in CloudShell and Linux
See original GitHub issueSteps to reproduce
PS /home/xiaogang> [int]$timeInt = $(Get-Date -UFormat '%s')
PS /home/xiaogang> $passwd = "bl0ckCh@!n$timeInt)" | ConvertTo-SecureString -AsPlainText -Force
PS /home/xiaogang> $psTxt = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($passwd))
PS /home/xiaogang> $psTxt
b
Expected behavior
PS C:\Users\xidi> [int]$timeInt = $(Get-Date -UFormat '%s')
PS C:\Users\xidi> $passwd = 'bl0ckCh@!n$timeInt)' | ConvertTo-SecureString -AsPlainText -Force
PS C:\Users\xidi> $psTxt = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($passwd))
PS C:\Users\xidi> $psTxt
bl0ckCh@!n$timeInt)
Actual behavior
b is return instead of bl0ckCh@!n$timeInt)
Environment data
Issue only happens in PowerShell 7.0.3 in CloudShell and Ubuntu 18.04, But in PowerShell 7.0.3, 7.0.0, 6.2.4 and Windows PowerShell in my local Windows machine, it is OK.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Troubleshooting AWS CloudShell
Solution: Install an updated version of Edge browser from the Microsoft site . (back to top). Arrow keys not working correctly in PowerShell....
Read more >PowerShell differences on non-Windows platforms
This article summarizes the differences between PowerShell on Windows and PowerShell on non-Windows platforms.
Read more >Cannot Encrypt/Decrypt String in Powershell
I am trying to encrypt & decrypt a string with ConvertTo-SecureString , output the string plaintext, and verify that the key works correctly...
Read more >Powershell doesn't find certain files
But in Powershell (version 7.0.3) certain .lnk files (which I do have on my $env:PATHEXT ) fail with a ResourceUnavailable: Program '[name]' ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks to @SeeminglyScience I now know what the difference between
SecureStringToBSTRandSecureStringToGlobalAllocUnicodeis. The former encodes the length of the string in 4 bytes just before the pointer. This is important if your string contains a null character as any of the other methods would see that as the end of the string.It also turns out that the
GetNetworkCredential()method also fails when dealing with null bytes in the string so I would take back my recommendation, even for such a rare edge case. Ultimately you should be usingSecureStringToBSTRandPtrToStringBSTRlike soGood info here, but this has indeed come up several times. Here’s the previous list of issues: https://github.com/PowerShell/PowerShell/issues?q=is%3Aissue+ptrtostringauto+is%3Aclosed+NOT+%2Fhome%2Fchythu%2Ftemp