How to convert String to SecureString?
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.