As has already been said, a question about security is rarely simple. However, if you need to have the data in memory without it being visible, do not use any temporary/fixed variables for storage before encryption.
On the face of it, it involves a slightly more specific problem, which is how memory management is done in .NET. So, if you use a string variable, it will be stored until GC decides it is no longer useful and you have no control over when it will be moved out.
There are several articles on this, try this:
http://www.codeproject.com/Articles/38069/Memory-Management-in-NET
Also, why do you need to start the string in a plain text variable? If you are going to use it, you could at least use it in a Securestring, see this link:
https://msdn.microsoft.com/pt-br/library/system.security.securestring(v=vs.110). aspx
But, there also involves how safe a Securestring is :)
The biggest problem is how you capture the string and how your encryption works. But at some point, it will pass through memory so it can be encrypted.
You could use unmanaged code to better control the time it remains in memory, but unmanaged code has other security issues, many of them linked to memory management or program execution flow.
There is no silver bullet. Even a char, typed on the keyboard, has to go through a buffer, just try to ensure that the location where you place the plain text, exists in the shortest possible time and is never stored in a long duration scope.
See the difference in the link code example for Securestring:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
public class Example
{
public static void Main()
{
// Instantiate the secure string.
SecureString securePwd = new SecureString();
ConsoleKeyInfo key;
Console.Write("Enter password: ");
do {
key = Console.ReadKey(true);
// Ignore any key out of range.
if (((int) key.Key) >= 65 && ((int) key.Key <= 90)) {
// Append the character to the password.
securePwd.AppendChar(key.KeyChar);
Console.Write("*");
}
// Exit if Enter key is pressed.
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
try {
Process.Start("Notepad.exe", "MyUser", securePwd, "MYDOMAIN");
}
catch (Win32Exception e) {
Console.WriteLine(e.Message);
}
finally {
securePwd.Dispose();
}
}
}
The main difference is that it does not save a plain text string to then pass to the encryption, it already adds char to char the encrypted string.
Another thing he does, is that he overwrites each char to each new char typed, thus only the Securestring contains in memory the whole sequence, but, the current char continues in memory for as long as the scope and the GC allow.
This particular case of analysis, at first glance, seems only to serve for a char-to-char input by the console, however, you should remember that it can be applied to the read stream of any byte-a-byte, not forgetting of course the hidden buffers in the implementation of most standard Apis.
Also, if the machine is compromised, even this approach falls easily on a Keylogger. :(
And what do you intend? Remove the content from the memory as soon as you encrypt?
– Intruso
When I encrypt the value of "strCriptografar" remains in memory, visible.
– Ronan Silva
The problem I understand, I’m trying to understand if you need to keep the original variable or if you can only save the encrypted one.
– Intruso
This amount I need it, I want to save session information so that they can not be accessed as you saw the disassemblers. This information should be kept in memory and necessary for the application. Sorry if I am not explaining it properly...
– Ronan Silva