Convert String to SecureString

前端 未结 13 1931
粉色の甜心
粉色の甜心 2020-12-12 15:43

How to convert String to SecureString?

13条回答
  •  孤城傲影
    2020-12-12 16:33

    If you would like to compress the conversion of a string to a SecureString into a LINQ statement you can express it as follows:

    var plain  = "The quick brown fox jumps over the lazy dog";
    var secure = plain
                 .ToCharArray()
                 .Aggregate( new SecureString()
                           , (s, c) => { s.AppendChar(c); return s; }
                           , (s)    => { s.MakeReadOnly(); return s; }
                           );
    

    However, keep in mind that using LINQ does not improve the security of this solution. It suffers from the same flaw as any conversion from string to SecureString. As long as the original string remains in memory the data is vulnerable.

    That being said, what the above statement can offer is keeping together the creation of the SecureString, its initialization with data and finally locking it from modification.

提交回复
热议问题