简体   繁体   English

是否可以为SecureString创建显式或隐式运算符以进行字符串转换?

[英]Is creating an explicit or implicit operator for a SecureString to string conversion possible?

I have performed some research on a variety of conversion methods, and have arrived on the usage of explicit or implicit operators . 我已经对各种转换方法进行了一些研究,并且已经使用了explicitimplicit operators

Noting that System.Security.SecureString is a sealed class (it can not be inherited from), is it possible to write an explicit or implicit operator that is able to perform conversion between a System.Security.SecureString and a System.String (utilizing the methodology described here ). 注意到System.Security.SecureString是一个sealed类(它不能继承),是否可以编写一个能够在System.Security.SecureStringSystem.String之间进行转换的explicitimplicit operator (利用这里描述的方法)。

For instance, I do know you must write the operator in a (partial) class of the object being converted . 例如,我知道你必须在被转换对象的(部分)类中编写operator Therefore, I figured the best way to do this was to inherit the class, and write the necessary operators . 因此,我认为最好的方法是继承类,并编写必要的operators

Is it possible to a) perform a conversion using an explicit or implicit operator for a System.Security.SecureString to a System.String , b) perform a conversion of a class object who's class is sealed ? 是否有可能a)使用System.Security.SecureStringexplicitimplicit operator转换为System.String ,b)执行类sealed的类对象的转换?

If not, is there another method outside of utilizing a class method, that will perform the conversion? 如果没有,除了使用类方法之外还有另一种方法可以执行转换吗?

Please provide a pragmatic answer assuming I am a consumer of Microsoft's products. 假设我是微软产品的消费者,请提供实用的答案。

Noting that System.Security.SecureString is a sealed class, is it possible to write an explicit or implicit operator that is able to perform conversion between a System.Security.SecureString and a System.String ? 注意到System.Security.SecureString是一个密封类,是否可以编写一个能够在System.Security.SecureStringSystem.String之间执行转换的显式或隐式运算符?

Yes, if you are a member of the Base Class Library team you can do so. 是的, 如果您是基类库团队的成员,您可以这样做。 Of course, doing so is a bad idea. 当然,这样做是个坏主意。

If you are not a member of the BCL team then no. 如果您不是BCL团队的成员,那么没有。 You need to be able to change the source code of one of the two classes. 您需要能够更改两个类之一的源代码。

As has been discussed elsewhere, the simple answer to your original question is no, it's not possible to add an operator to SecureString to do anything as it's sealed. 正如其他地方所讨论的那样,对原始问题的简单回答是否定的,不可能在SecureString中添加运算符来执行任何密封操作。

The answer to "(b) perform a conversion of a class object who's class is sealed" is more complex. “(b)对班级密封的班级对象进行转换”的答案更为复杂。

What you can do is create your own struct , eg called EncryptedString that has a SecureString as a private field. 你可以做的是创建自己的struct ,例如称为EncryptedString,它将SecureString作为private字段。 You are then free to add any SecureString <-> string conversion and comparison operators and methods to that struct to meet your needs. 然后,您可以自由地将任何SecureString < - > string转换和比较运算符和方法添加到该struct以满足您的需求。 That isn't strictly speaking perform[ing] a conversion of a class object, instead it is composing a new type to meet your needs. 严格来说,这不是执行类对象的转换,而是组成一种新type来满足您的需求。 As the saying goes "Composition over inheritance" (see http://en.wikipedia.org/wiki/Composition_over_inheritance ), so actually this approach both meets your needs and is seen as OO "best practice". 俗话说“构图而不是继承”(参见http://en.wikipedia.org/wiki/Composition_over_inheritance ),实际上这种方法既满足了你的需求又被视为OO“最佳实践”。

Now for the disclaimer: 现在免责声明:

The whole point of SecureString is that, unlike string , it is secure. SecureString的重点在于,与string不同,它是安全的。 If you start converting a SecureString to a string (or allowing a SecureString to be created from a string ), you'll undermine the whole point of the class. 如果您开始将SecureString转换为string (或允许从string创建SecureString ),您将破坏该类的整个点。

So if you need to do comparisons, please use Marshal.SecureStringToBSTR() and Marshal.StringToBSTR() to convert the two to BSTR structures , compare them, wipe the memory and free it, or some similar mechanism to minimize the chances of compromising the security of SecureStrings . 因此,如果您需要进行比较,请使用Marshal.SecureStringToBSTR()Marshal.StringToBSTR()将两者转换为BSTR structures ,比较它们,擦除内存并释放它,或者使用一些类似的机制来最小化损害SecureStrings安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM