0

I have issues to get the data decrypted and not sure what I do wrong as tried almost everything.

    private static byte[] AES_Encrypt(byte[] byteArray)
    {
        byte[] salt = GenerateRandomSalt();
        MemoryStream fsCrypt = new MemoryStream();
        byte[] ms = new byte[0];
        byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(keyName);
        RijndaelManaged AES = new RijndaelManaged();
        AES.KeySize = 256;
        AES.BlockSize = 128;
        AES.Padding = PaddingMode.None;
        var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Mode = CipherMode.CFB;
        fsCrypt.Write(salt, 0, salt.Length);
        CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
        MemoryStream fsIn = new MemoryStream(byteArray);
        byte[] buffer = new byte[1048576];
        int read;
        try
        {
            if (byteArray.Length <= buffer.Length)
                cs.Write(buffer, 0, buffer.Length);
            else
                while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
                {
                    cs.Write(buffer, 0, read);
                }
            ms = fsCrypt.ToArray();
            fsIn.Close();
        }
        catch (Exception ex)
        {

        }
        finally
        {
            try
            {
                cs.Close();
                fsCrypt.Close();
            }
            catch (Exception err)
            {

            }
        }

        string response = Encoding.UTF8.GetString(ms.ToArray());
        return ms.ToArray();
    }

    private static byte[] AES_Decrypt(byte[] byteArray)
    {
        byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(keyName);
        byte[] salt = new byte[32];
        byte[] ms = new byte[0];
        MemoryStream fsCrypt = new MemoryStream(byteArray);
        fsCrypt.Read(salt, 0, salt.Length);
        RijndaelManaged AES = new RijndaelManaged();
        AES.KeySize = 256;
        AES.BlockSize = 128;
        var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Padding = PaddingMode.None;
        AES.Mode = CipherMode.CFB;
        MemoryStream fsOut = new MemoryStream();
        CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
        int read;
        byte[] buffer = new byte[1048576];
        try
        {
            string response = Encoding.UTF8.GetString(byteArray);
            if (byteArray.Length <= buffer.Length)
            {
                while ((read = cs.Read(byteArray, 0, byteArray.Length)) > 0)
                {
                    fsOut.Read(byteArray, 0, read);
                }
                string vresponse = Encoding.UTF8.GetString(fsOut.ToArray());
            }
            else
                while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fsOut.Write(buffer, 0, read);
                }
        }
        catch (System.Security.Cryptography.CryptographicException ex_CryptographicException)
        {

        }
        catch (Exception ex)
        {

        }
        try
        {
            cs.Close();
        }
        catch (Exception ex)
        {

        }
        finally
        {
            fsOut.Close();
            //fsCrypt.Close();
        }
        return ms.ToArray();
    }

My data is sometimes shorter than the 1MB being used so I want to catch th shorter data to use less byte, however its for later concern.

The data I send to the decryptor is using the same static keys now and the inbound encrypted data is identical as the out type encrypted data.

If I put the PaddingMode.None; to PaddingMode.PKCS7 it gives me a mapping error, which comes as I assume in the shorter string than the buffer actually is, so as far my assumations this is correct, so I use for now 'none'.

I have looked to lots of links as:

https://stackoverflow.com/questions/31486028/decrypting-cryptostream-into-memorystream

https://stackoverflow.com/questions/31486028/decrypting-cryptostream-into-memorystream

https://stackoverflow.com/questions/8583112/padding-is-invalid-and-cannot-be-removed

[Encrypt to memory stream, pass it to file stream

[AES encryption on large files

The problem I cant figure out why the encrypted data wont decrypt as should. 100% sure I do something not correct but cant figure out what, any help would be appriciated.

I have changes some code and tried different options, now using this but on both option I get errors.

            using (var msi = new MemoryStream())
            {
                try
                {
                    using (var cs = new CryptoStream(msi, aesAlg.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytes, 0, bytes.Length); //Option 1, gives error, length is invalid of byte array

                        do //Option 2, give error, Padding is invalid and cannot be removed.
                        {
                            count = msi.Read(bytes, 0, blockSizeBytes);
                            offset += count;
                            cs.Write(data, 0, count);

                        }
                        while (count > 0);
                    }

                    var plainText = msi.ToArray();
                    var text = Encoding.Unicode.GetString(plainText);
                }
                catch (Exception e)
                {

                }
                //return text;
            }

I checked the data length and using Unicode encoding it is 4258 and the bytes are 8516, which should be from my point of view as using Unicode.

Not sure if using the right encoding as found this https://stackoverflow.com/a/10380166/3763117 and can do without but gives same lengthas unicode. little stucked on this any help with some samples would be appriciated.

user3763117
  • 317
  • 1
  • 5
  • 17

0 Answers0