title: Encode / Decode RealVNC password
body: <p>I'm trying to write C# application that can remotely change the RealVNC password on another box.</p>
  
  <p>What works currently is that I can pull a password from a box that has already been changed, store it as a hex string, and then send it to another box AND then change the password that way but I need to be able to change the password or randomize it on the fly.</p>
  
  <p>I'm having problems with creating the correct binary to place in the registry.</p>
  
  <p>I know the VNC key:</p>
  
  <pre><code>byte[] Key = { 23, 82, 107, 6, 35, 78, 88, 7 };
  </code></pre>
  
  <p>So using the above key and passing "1234" as the password to encrypt using the following code:</p>
  
  <pre><code>public static byte[] EncryptTextToMemory(string Data, byte[] Key)
  {
      try
      {
          MemoryStream mStream = new MemoryStream()
  
          DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
          desProvider.Mode = CipherMode.ECB;
          desProvider.Key = Key;
  
          CryptoStream cStream = new CryptoStream(mStream,
              desProvider.CreateEncryptor(),
              CryptoStreamMode.Write);
  
          byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);
  
          cStream.Write(toEncrypt, 0, toEncrypt.Length);
          cStream.FlushFinalBlock();
  
          byte[] ret = mStream.ToArray();
  
          cStream.Close();
          mStream.Close();
  
          return ret;
      }
      catch (CryptographicException ex)
      {
          MessageBox.Show("A Cryptographic error occurred: " + ex.Message);
          return null;
      }
  </code></pre>
  
  <p>After passing the returned byte array to <code>BitConverter.ToString</code>, I would expect to get the same hex values as stored in the registry of a password already set to 1234 with RealVNC itself, but I'm not.</p>