将 SecureString 转换回纯文本时出现 PowerShell 错误 [英] PowerShell error when converting a SecureString back to plain text

查看:31
本文介绍了将 SecureString 转换回纯文本时出现 PowerShell 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 SecureString 转换回纯文本?

来自微软文档.

$secureString = ConvertTo-SecureString -String 'Example' -AsPlainText$secureString #'System.Security.SecureString'ConvertFrom-SecureString -SecureString $secureString -AsPlainText #'示例'

但是当我尝试以下操作时:

<代码>>>$secureString = ConvertTo-SecureString -String 'Example' -AsPlainText -Force>>$secureString #'System.Security.SecureString'>>ConvertFrom-SecureString -SecureString $secureString -AsPlainText #'示例'

我收到此错误:

ConvertFrom-SecureString :找不到与参数名称AsPlainText"匹配的参数.在行:3 字符:54+ ... ertFrom-SecureString -SecureString $secureString -AsPlainText # '考试 ...+ ~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [ConvertFrom-SecureString], ParameterBindingException+ FullQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand

当我在没有 -AsPlainText 的情况下运行这些命令时,它返回:

<预> <代码> 01000000d08c9ddf0115d1118c7a00c04fc297eb0100000063db09d0a5a25b4d92484ee7d5911cf90000000002000000000003660000c00000001000000098fcbc0d5e009078dd7a1dfe9f24d3380000000004800000a00000001000000027a7f841c320bc17b5e4febbcd3e49551000000005a133e9f864340d9d7b2473939156ef14000000f889bf5c0fc4799a5aaf435ebb15cd0920f24575

这是 Microsoft 问题还是 PowerShell 5.1 不支持此问题

PowerShell 版本

Major Minor Build Revision----- ----- ----- --------5 1 18362 628

解决方案

PowerShell 5.1 不支持.ConvertFrom-SecureString 直到 PowerShell Core 7+ 才支持 -AsPlainText 参数.

<小时>

如果要从 PowerShell 7 之前的纯文本 SecureString 中获取纯文本字符串,请使用以下方法:

$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)$plaintext = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)

$plaintext 将是从您传入的 SecureString 转换而来的明文字符串.

How do I convert a SecureString back to plain text?

From Example 4 of the Microsoft Documentation.

$secureString = ConvertTo-SecureString -String 'Example' -AsPlainText
$secureString # 'System.Security.SecureString'
ConvertFrom-SecureString -SecureString $secureString -AsPlainText # 'Example'

But when I try the following:

>> $secureString = ConvertTo-SecureString -String 'Example' -AsPlainText -Force
>> $secureString # 'System.Security.SecureString'
>> ConvertFrom-SecureString -SecureString $secureString -AsPlainText # 'Example'

I get this error:

ConvertFrom-SecureString : A parameter cannot be found that matches parameter name 'AsPlainText'.
At line:3 char:54
+ ... ertFrom-SecureString -SecureString $secureString -AsPlainText # 'Exam ...
+                                                      ~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertFrom-SecureString], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand

When I run those commands without -AsPlainText it returns:

01000000d08c9ddf0115d1118c7a00c04fc297eb0100000063db09d0a5a25b4d92484ee7d5911cf90000000002000000000003660000c00000001000000098fcbc0d5e009078dd7a1dfe9f24d3380000000004800000a00000001000000027a7f841c320bc17b5e4febbcd3e49551000000005a133e9f864340d9d7b2473939156ef14000000f889bf5c0fc4799a5aaf435ebb15cd0920f24575

Is this a Microsoft issue or is this not supported in PowerShell 5.1

PowerShell Version

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  628

解决方案

It's not supported in PowerShell 5.1. ConvertFrom-SecureString doesn't support the -AsPlainText parameter until PowerShell Core 7+.


If you want to get the plain text string from a plain text SecureString in PowerShell prior to 7, use the following methods:

$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
$plaintext = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)

$plaintext will be your plaintext string converted from the SecureString you passed in.

这篇关于将 SecureString 转换回纯文本时出现 PowerShell 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆