title: Unable to Decrypt data on second computer
body: <p>I have two applications, Server and the Client, one running from one machine, and the other from a second machine, the server is passing data using a WebSocket connection, the data is encrypted before is sent to the Client, the data makes it to the Client application correctly but I'm trying to Decrypt it using the same secure method, and Secret Key, but I won't work, it only decrypts it when both apps are run from the same computer. Does any one have any idea why it works when they are run from the same machine, but not when running them from separate machines?</p>
  
  <p>Both Server and Client application use this same Secure Method.</p>
  
  <pre><code>using System.Security.Cryptography;
  
  // ENCRYPT
  
  static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("MY SECRET KEY HERE");
  
  public static string EncryptString(System.Security.SecureString input)
  {
      byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
          System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
          entropy,
          System.Security.Cryptography.DataProtectionScope.CurrentUser);
      return Convert.ToBase64String(encryptedData);
  }
  
  public static SecureString DecryptString(string encryptedData)
  {
      try
      {
          byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
              Convert.FromBase64String(encryptedData),
              entropy,
              System.Security.Cryptography.DataProtectionScope.CurrentUser);
          return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
      }
      catch
      {
          return new SecureString();
      }
  }
  
  public static SecureString ToSecureString(string input)
  {
      SecureString secure = new SecureString();
      foreach (char c in input)
      {
          secure.AppendChar(c);
      }
      secure.MakeReadOnly();
      return secure;
  }
  
  public static string ToInsecureString(SecureString input)
  {
      string returnValue = string.Empty;
      IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
      try
      {
          returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
      }
      finally
      {
          System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
      }
      return returnValue;
  }
  
  // ENCRYPT ENDS
  </code></pre>
  
  <p><strong>To Encrypt data on the Server I use:</strong></p>
  
  <pre><code>string encryptedMessage = EncryptString(ToSecureString("Data to Encrypt Here"));
  </code></pre>
  
  <p><strong>To Decrypt data on the Client I use</strong>:</p>
  
  <pre><code>SecureString data1 = DecryptString(dataEncryptedReceived);
  IntPtr stringPointerData1 = Marshal.SecureStringToBSTR(data1);
  string normalStringData1 = Marshal.PtrToStringBSTR(stringPointerData1);
  Marshal.ZeroFreeBSTR(stringPointerData1);
  </code></pre>
  
  <p>Again, this all works fine ONLY when I use both Server and Client applications from the same computer, but I try to use them separate, Server on one machine, and Client on another it won't Decrypt the data, even though the Client receives the encrypted data successfully.</p>
  
  <p>Please help!</p>
  
  <p>Thanks.</p>