C # UTF8 AES encoding problem

I am creating a TCP based client chat. I am trying to encrypt some data using AES (more security) I have an AES encryption class and it uses UTF-8 by default as the input and input encoding type. But for some reason, when I pass information through TCPClient (using UTF-8) and receive it from the other side, it throws an error:

`System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid.
   at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   at System.Security.Cryptography.CryptoStream.FlushFinalBlock()`

      

So, I recreated the problem without using a TCP client by simply taking the AES encrypted data and passing it through a UTF-8 encoding system that receives a byte array string, then re-receives the byte array using UTF-8 (basically the same as and online (no line))

This method works:

        string dataToEncrypt = "Hello World";
        byte[] key = Encryption.AesEncryption.GenerateKey(32);
        byte[] iv = Encryption.AesEncryption.GenerateKey(16);

        byte[] encrypted = Encryption.AesEncryption.EncryptString(dataToEncrypt, key, iv);

        string decrypted = Encryption.AesEncryption.DecryptedBytes(encrypted, key, iv);

      

The method doesn't work (throws an error from above)

        Encoding encoding = Encoding.UTF8;

        string dataToEncrypt = "Hello World";
        byte[] key = Encryption.AesEncryption.GenerateKey(32);
        byte[] iv = Encryption.AesEncryption.GenerateKey(16);

        byte[] encrypted = Encryption.AesEncryption.EncryptString(dataToEncrypt, key, iv);

        string encstring = encoding.GetString(encrypted);

        byte[] utf8encrypted = encoding.GetBytes(encstring);

        string decrypted = Encryption.AesEncryption.DecryptedBytes(utf8encrypted, key, iv);

      

What am I doing wrong?

This is my encryption class:

public sealed class AesEncryption
{
    private byte[] Key;

    public Encoding Encoder = Encoding.UTF8;

    public AesEncryption(byte[] key)
    {
        Key = key;
    }

    public byte[] Encrypt(string text, byte[] iv)
    {
        var bytes = Encoder.GetBytes(text);
        var rm = new RijndaelManaged();
        var encrypter = rm.CreateEncryptor(Key, iv);
        var ms = new MemoryStream();
        var cs = new CryptoStream(ms, encrypter, CryptoStreamMode.Write);
        cs.Write(bytes, 0, bytes.Length);
        cs.FlushFinalBlock();
        var output = ms.ToArray();
        cs.Close();
        ms.Close();
        return output;
    }

    public string Decrypt(byte[] encrypted, byte[] iv)
    {
        var ms = new MemoryStream();
        var cs = new CryptoStream(ms,
            new RijndaelManaged().CreateDecryptor(Key, iv),
            CryptoStreamMode.Write);
        cs.Write(encrypted, 0, encrypted.Length);
        cs.FlushFinalBlock();
        var output = ms.ToArray();
        cs.Close();
        ms.Close();
        return Encoder.GetString(output);
    }

    public static byte[] EncryptString(string text, byte[] key, byte[] iv)
    {
        var ec = new AesEncryption(key);
        return ec.Encrypt(text, iv);
    }

    public static string DecryptedBytes(byte[] encrypted, byte[] key, byte[] iv)
    {
        var ec = new AesEncryption(key);
        return ec.Decrypt(encrypted, iv);
    }

    public static byte[] GenerateKey(int length)
    {
        Random rnd = new Random();

        var chars = "1!2@3#4$5%6^7&8*9(0)-_=+qQwWeErRtTyYuUiIoOpP[{]}\\|aAsSdDfFgGhHjJkKlL;:'\"zZxXcCvVbBnNmM,<.>/?".ToCharArray();

        string randomizedKey = "";

        for (int i = 0; i < length; i++)
        {
            randomizedKey += chars[rnd.Next(0, chars.Length)];
        }
        return randomizedKey.ToByteArray();
    }
}

      

+3


source to share


1 answer


UTF-8 doesn't represent bytes perfectly. Short and simple answer: pass bytes, not a UTF-8 string. If you must have a string, encode it in Base64.



+1


source







All Articles