Search code examples
c#.net-coreencryptionaes

corrupted data AES C#


Quick heads up, i know this is a duplicate but the answers did not help me.

So im trying to make an AES and RSA encrypted connection, so i can encrypt very large stuff, However upon decrypting something with AES, I`m met with garbage in the beginning of the data, Here is my code (yes i tried hardcoding the IV):

                RSA rsa = RSA.Create();
                rsa.KeySize = 1024;
                Aes aes = Aes.Create();
                aes.IV = Encoding.UTF8.GetBytes("1234567890123456");
                byte[] sendBytes = null;
                byte[] buffer = new byte[1024];
                TcpClient client = new TcpClient();
                client.Connect(new IPEndPoint(IPAddress.Loopback, 900));
                NetworkStream ns = client.GetStream();
                ns.Write(Encoding.UTF8.GetBytes(rsa.ToXmlString(false) + "$"), 0, (rsa.ToXmlString(false) + "$").Length);
                ns.Read(buffer, 0, buffer.Length);
                MessageBox.Show(Encoding.UTF8.GetString(buffer));
                rsa.FromXmlString(Encoding.UTF8.GetString(buffer).Split('$')[0]);
                ns.Write(Encoding.UTF8.GetBytes(Convert.ToBase64String(rsa.Encrypt(aes.Key, RSAEncryptionPadding.Pkcs1)) + "$"), 0, (Convert.ToBase64String(rsa.Encrypt(aes.Key, RSAEncryptionPadding.Pkcs1)) + "$").Length);
                MessageBox.Show(Convert.ToBase64String(rsa.Encrypt(aes.Key, RSAEncryptionPadding.Pkcs1)));
                ICryptoTransform encrypt = aes.CreateEncryptor();
                sendBytes = Encoding.UTF8.GetBytes("*" + Convert.ToBase64String(encrypt.TransformFinalBlock(Encoding.UTF8.GetBytes(authResult.AccessToken), 0, authResult.AccessToken.Length)) + "|" + Convert.ToBase64String(aes.IV) + "$");
                ns.Write(sendBytes, 0, ("*" + Convert.ToBase64String(encrypt.TransformFinalBlock(Encoding.UTF8.GetBytes(authResult.AccessToken), 0, authResult.AccessToken.Length)) + "|" + Convert.ToBase64String(aes.IV) + "$").Length);
                sendBytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(encrypt.TransformFinalBlock(Encoding.UTF8.GetBytes(authResult.Account.Username), 0, authResult.Account.Username.Length)) + "$");
                ns.Write(sendBytes, 0, (Convert.ToBase64String(encrypt.TransformFinalBlock(Encoding.UTF8.GetBytes(authResult.Account.Username), 0, authResult.Account.Username.Length)) + "$").Length);
                ns.Dispose();
                client.Close();

This is the server:

                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom);
                    string dataFromClient = Encoding.UTF8.GetString(bytesFrom).Split("$")[0];
                    networkStream.Write(Encoding.UTF8.GetBytes(rsa.ToXmlString(false) + "$"));
                    rsa.FromXmlString(dataFromClient.Split("$")[0]);
                    int waste;
                    rsa.ImportRSAPrivateKey(key, out waste);
                    Console.WriteLine(rsa.KeySize.ToString());
                    networkStream.Read(bytesFrom);
                    Console.WriteLine(Encoding.UTF8.GetString(bytesFrom));
                    aes.Key = rsa.Decrypt(Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("$")[0]), RSAEncryptionPadding.Pkcs1);
                    ICryptoTransform decrypt = aes.CreateDecryptor();
                    networkStream.Read(bytesFrom);
                    aes.IV = Encoding.UTF8.GetBytes("1234567890123456");
                    dataFromClient = Encoding.UTF8.GetString(decrypt.TransformFinalBlock(Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("|")[0].Split("*")[1]), 0, Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("|")[0].Split("*")[1]).Length));
                    Console.WriteLine(Encoding.UTF8.GetString(decrypt.TransformFinalBlock(Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("|")[0].Split("*")[1]), 0, Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("|")[0].Split("*")[1]).Length)));
                    string info = await GetHttpContentWithToken("https://graph.microsoft.com/v1.0/me", dataFromClient);
                    Console.WriteLine(info);
                    Root a = JsonConvert.DeserializeObject<Root>(info);
                    networkStream.Read(bytesFrom);
                    dataFromClient = Encoding.UTF8.GetString(decrypt.TransformFinalBlock(Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("$")[0]), 0, Convert.FromBase64String(Encoding.UTF8.GetString(bytesFrom).Split("$")[0]).Length));

(yes im messing with microsoft accounts, you can just ignore everything except for the key exchange and AES decryption code.)


Solution

  • After taking a closer look, i realized i created the decryptor before the IV, sorry about that.