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.
The answer to "(b) perform a conversion of a class object who's class is sealed" is more complex.
What you can do is create your own struct
, eg called EncryptedString that has a SecureString
as a private
field. You are then free to add any SecureString
<-> string
conversion and comparison operators and methods to that struct
to meet your needs. That isn't strictly speaking perform[ing] a conversion of a class object, instead it is composing a new type
to meet your needs. 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".
Now for the disclaimer:
The whole point of SecureString
is that, unlike string
, it is secure. 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.
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
.