title: c# Convert.To/FromBase64String confusion
body: <p>Assuming I have this Method.</p>
  
  <pre><code>private static void Example(string data)
      {
          Console.WriteLine("Initial : {0}", data);
  
          data = data.PadRight(data.Length + 1, '0'); 
          Console.WriteLine("Step 1 : {0}", data);
  
          data = data.PadRight(data.Length + 4 - data.Length % 4, '=');
          Console.WriteLine("Step 2 : {0}", data);
  
          byte[] byteArray = Convert.FromBase64String(data);
          string newData = Convert.ToBase64String(byteArray);
              Console.WriteLine("Step 3 : {0}", newData);
      }
  </code></pre>
  
  <p>I expect the output given the input string "1" to be as follows</p>
  
  <pre><code>Initial : 1
  Step 1 : 10
  Step 2 : 10==
  Step 3 : 10==
  </code></pre>
  
  <p>Instead the output is this.</p>
  
  <pre><code>Initial : 1
  Step 1 : 10
  Step 2 : 10==
  Step 3 : 1w==
  </code></pre>
  
  <p>And I have no idea why. I would expect the output to be the same as the input but it isn't.
  I have tried replacing</p>
  
  <pre><code>data = data.PadRight(data.Length + 1, '0'); 
  </code></pre>
  
  <p>with</p>
  
  <pre><code>data = data + "0";
  </code></pre>
  
  <p>It appears with longer input strings too, for example strings with a length of 5 or 9. It works fine if I add "=" but then I exceed my padding limit with <code>Convert.FromBase64String()</code></p>
  
  <p>So my question is really what is going on and how can I get my expected output,?
  What am <strong><em>I doing wrong</em></strong>?</p>
  
  <p>Edit: For those confused as to why I'm using bas64 it is related to this <a href="https://stackoverflow.com/questions/25222881/php-decrypting-data-with-rsa-private-key-solved">PHP decrypting data with RSA Private Key</a></p>