title: Key does not exist when trying to decrypt using RSA
body: <p>So, I am creating a simple "web chat", using TcpClient and TcpListener class. I want all data sent to be encrpyted, and I am using AES encryption. So first I have to make sure AES key from server is securely sent to client. I am trying to achieve this, by encrypting AES key with RSA and then sending it to client and there decrypting it with RSA again.</p>
  
  <p>So first of all I created an RSACryptoServiceProvider on server and extracted public key. I sent public key to client and there created RSACryptoServiceProvider and imported that key. When I call Decrpyt method I get an key does not exist exception. This is my code:</p>
  
  <p>Server:</p>
  
  <p>This is sending public key to client.</p>
  
  <pre><code>RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  string privateXml = rsa.ToXmlString(true);
  string publicXml = rsa.ToXmlString(false);
  Byte[] pubKey = Encoding.UTF8.GetBytes(publicXml);
  clientStream.Write(pubKey, 0, pubKey.Length); 
  
  AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); // simetrično kriptiranje
  byte[] aesKey = aes.Key;
  byte[] encryptedRSA = rsa.Encrypt(aesKey, false);
  
  clientStream.Write(encryptedRSA, 0, encryptedRSA.Length);
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>Byte[] serverPublicKey = new Byte[1024];
  
  Int32 bytes1 = stream.Read(serverPublicKey, 0, serverPublicKey.Length); 
  string serverKey = Encoding.UTF8.GetString(serverPublicKey, 0, bytes1);
  serverKey = serverKey.Replace("\0", ""); 
  
  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  rsa.FromXmlString(serverKey);
  
  Byte[] bytes2 = new Byte[128];
  String aesKey = null;
  stream.Read(bytes2, 0, bytes2.Length);
  
  byte[] decryptedKey = rsa.Decrypt(bytes2, false);
  </code></pre>