1

In proxy.exe I am creating a secure string the following way:

public SecureString GetSecureEncryptionKey()
    {
        string strPassword = "8charPwd";
        SecureString secureStr = new SecureString();
        if (strPassword.Length > 0)
        {
            foreach (var c in strPassword.ToCharArray()) secureStr.AppendChar(c);
        }
        return secureStr;
    }

Then in main.exe I am decrypting it using this function:

public string convertToUNSecureString(SecureString secstrPassword)
    {
        IntPtr unmanagedString = IntPtr.Zero;
        try
        {
            unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstrPassword);
            return Marshal.PtrToStringUni(unmanagedString);
        }
        finally
        {
            Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
        }
    }

The issue is that the returned string is empty, unless I encrypt the initial string within main.exe, then the returned decrypted string is indeed "8charPwd". Why is this happening? Is SecureString encryption bound to the executable?

IneedHelp
  • 1,630
  • 1
  • 27
  • 58
  • One thing I'd like to point out is that you're making the secure string completely useless by hard-coding `string strPassword = "8charPwd";`. That's like locking the door to your house and hanging the keys right next to it. – rory.ap Feb 14 '15 at 13:58
  • 1
    Thanks for pointing that out, but it's useless to point it out because the application that contains the actual string is server-side/decompile-secured and it's meant to run less than a second, enough to send the secure string to the main application. My problem is that the secureString doesn't get decrypted as it should. – IneedHelp Feb 14 '15 at 14:01
  • 2
    General purpose of SecureString is storing passwords *in memory* encrypted. So if one makes a memory dump, it's more difficult to find out where the password is. SecureString stores data inside one process' address space. So I'm curious how you pass SecureString between processes? – Alex K. Feb 14 '15 at 14:05
  • @AlexK using WCF (netPipe) – IneedHelp Feb 14 '15 at 14:05
  • @IneedHelp that is extremely vague and does not address what Alex wants to know. – usr Feb 14 '15 at 14:53
  • If NetNamedPipeBinding WCF is vague to someone, then that someone wouldn't be able to provide an answer to my question anyway. – IneedHelp Feb 14 '15 at 14:54
  • 1
    Wow, you really make people not want to help you. Drop the baditude and maybe people will be more willing to take their own time to help you. – rory.ap Feb 14 '15 at 18:23
  • 1
    I just don't like it when someone automatically assumes I'm an idiot based on false premises. And I asked a question, I expect pertinent answers, not side suggestions that have nothing to do with the raised issue. – IneedHelp Feb 14 '15 at 18:53

1 Answers1

6

The purpose of SecureString is to keep strings safety inside your application memory(keep the string secure in RAM) SecureString object is not a serialize-able. You cannot transfer an instance between applications.

SecureString encrypt the string by using RtlEncryptMemory (WINAPI) with the flag:"0" (only the same process can decrypt the content). RtlEncryptMemory API

if you don't want to expose the password(at any time) in the RAM, you can create a simple obfuscation(or encryption) logic, and then transfer the content.

Edit:

I found 2 old questions that might be helpful for you:

When would I need a SecureString in .NET?

Wcf-Authentication and Logging

Community
  • 1
  • 1
Old Fox
  • 8,629
  • 4
  • 34
  • 52
  • Ok, so I guess this answers my question. A SecureString can only be decrypted by the same process that created it. The problem is that even if I pass an encrypted string (encrypted by myself), when I decrypt it, it still gets stored in the memory, even if I just pass it directly as a parameter without saving it to any variable. – IneedHelp Feb 14 '15 at 15:18
  • @IneedHelp This is not the easiest way but this is the first way I thought to solve the problem (because I gave something similar as test) you can create an [Enigma machine](http://en.wikipedia.org/wiki/Enigma_machine) and then the server insert each character into instance of SecureString – Old Fox Feb 14 '15 at 15:48
  • the problem I'm facing now is that I can't actually use the SecureString in a context where string is needed... – IneedHelp Feb 14 '15 at 16:26
  • can you give me an example for this problem? – Old Fox Feb 14 '15 at 16:48
  • I'm basically trying to use a string in a secure way, and I thought I could temporarily transform a secureString to an actual string in a decryption operation that requires a keyPass, but apparently the decrypted string always shows up in memory.. – IneedHelp Feb 14 '15 at 17:01
  • I'm not completely in your mind, but you can used "tokens" instead of strings. all tokens will be temporary, then the data you are trying to protect won't be load into the RAM. – Old Fox Feb 14 '15 at 20:19
  • I am not familiar with this concept, would you please elaborate a bit? – IneedHelp Feb 14 '15 at 22:14
  • the concept is very similar to [http session](http://en.wikipedia.org/wiki/Session_%28computer_science%29) . we got the message: " Please avoid extended discussions in comments". so lets stop this convention. if you want to ask about your new problem, please open new question(with more details and specification) and the community will find a solution. – Old Fox Feb 15 '15 at 06:34