I have a byte[] which I need to encode in Base64 and return as SecureString. My current code is as follows:
string privateString = Convert.ToBase64String(byteArray);
SecureString result = new SecureString();
foreach (char c in privateString)
{
result.AppendChar(c);
}
// wipe the byte array...
The problem is that calling Convert.ToBase64String is not secure as it creates a managed string which I can't destroy.
Is there a secure way of doing this?
byteArrayin memory somewhere containing the unencrypted contents, subject to being moved around in memory by GC and/or leaked through snapshots, already means there is little to no point involvingSecureString. It is extremely difficult to make any effective use of and you typically shouldn't bother, as the offered security is illusory.byte[]can be more useful than having astring, though; at least you can overwrite it when you're done (without needing to resort tounsafehacks). But yeah; totally agree onSecureStringetcbyte[]"? Unless it was created ex nihilo or from a crypto RNG call that made sure to track all of its buffers it's very likely to involve another path somewhere where memory was copied. Narrow is the gate toSecureStringand wide is the path to insecure copies of data... (And that's before we consider the path out ofSecureStringof course, which is the main reason this thing doesn't really work.)