If I do the following in a class, is my password cached/discoverable in memory?
public class ConnectionInfo
{
private SecureString _password;
public string UserName;
public string Password
{
get
{
IntPtr valuePtr = IntPtr.Zero;
try
{
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(_password);
return Marshal.PtrToStringUni(valuePtr);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
}
}
set
{
_password = new SecureString();
foreach (char c in value)
{
_password.AppendChar(c);
}
}
}
}
In other words, if I use it like this
ConnectionInfo connectionInfo = new Models.DomainInfo();
connectionInfo.Password = "Password1";
and later use it with a directoryEntry
DirectoryEntry entry = new DirectoryEntry("LDAP://Domain.com", $"Domain\\{connectionInfo.UserName}", connectionInfo.Password);
is the cleartext password cached via the property Password? (I am not referring to any leaks that might occur via DirectoryEntry etc., only the Property)
Password is stored in web/app.config and retrieved via this
staticKey = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(staticKey, staticIV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
Is the encryption strong enough?