"Filling is invalid and cannot be deleted" when decrypting using Rijndael

I am trying to decrypt a string encrypted using the Rijndael algorithm. In this type of encryption, the addition with "#" to the right of the key and IV is used if they are less than 16 characters long. The decryption string is obtained from the Webservice, which sends it, and the key for me is in XML SOAP format. IV is the MAC address of my machine (which the server uses as IV to encrypt the string). When I try to decrypt the resulting string, my program crashes in this instruction:

while ((num5 = stream3.ReadByte()) != -1)

and he gives me this error: "Filling is unacceptable and cannot be deleted." I searched for this error on MSDN and it says that it happens when the IV used for encryption is different from the IV used for decryption, but, I repeat, IV is MacAddress, and every time it is the same.

This is the source code for the Encrypt and Decrypt functions:

public static string Decrypt(string strInputString, string strKeyString, string myIV)
{
        if ((strInputString == null) || (strInputString.Length == 0))
        {
            return strInputString;
        }
        try
        {
            int num5;
            int keySize = 0x100;
            int blockSize = 0x100;
            int length = keySize / 0x10;
            if (strKeyString.Length > length)
            {
                strKeyString = strKeyString.Substring(0, length);
            }
            if (strKeyString.Length < length)
            {
                strKeyString = strKeyString.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(strKeyString);
            if (myIV.Length > length)
            {
                myIV = myIV.Substring(0, length);
            }
            if (myIV.Length < length)
            {
                myIV = myIV.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(myIV);
            byte[] bytes = Encoding.Unicode.GetBytes(strKeyString);
            byte[] rgbIV = Encoding.Unicode.GetBytes(myIV);
            RijndaelManaged managed = new RijndaelManaged {
                BlockSize = blockSize,
                KeySize = keySize
            };
            MemoryStream stream = new MemoryStream();
            for (int i = 0; i < strInputString.Length; i += 2)
            {
                stream.WriteByte(byte.Parse(strInputString.Substring(i, 2), NumberStyles.AllowHexSpecifier));
            }
            stream.Position = 0L;
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream3 = new CryptoStream(stream, managed.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
            while ((num5 = stream3.ReadByte()) != -1)
            {
                stream2.WriteByte((byte) num5);
            }
            stream3.Close();
            stream2.Close();
            stream.Close();
            byte[] buffer3 = stream2.ToArray();
            return Encoding.Unicode.GetString(buffer3);
        }
        catch (Exception exception)
        {
            Log.Error(exception.Message);
        }
}

public static string Encrypt(string strInputString, string strKeyString, string myIV)
{
        if ((strInputString == null) || (strInputString.Length == 0))
        {
            return strInputString;
        }
        try
        {
            int num4;
            int keySize = 0x100;
            int blockSize = 0x100;
            int length = keySize / 0x10;
            if (strKeyString.Length > length)
            {
                strKeyString = strKeyString.Substring(0, length);
            }
            if (strKeyString.Length < length)
            {
                strKeyString = strKeyString.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(strKeyString);
            if (myIV.Length > length)
            {
                myIV = myIV.Substring(0, length);
            }
            if (myIV.Length < length)
            {
                myIV = myIV.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(myIV);
            byte[] bytes = Encoding.Unicode.GetBytes(strKeyString);
            byte[] rgbIV = Encoding.Unicode.GetBytes(myIV);
            string str = "";
            RijndaelManaged managed = new RijndaelManaged {
                BlockSize = blockSize,
                KeySize = keySize
            };
            MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(strInputString));
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream3 = new CryptoStream(stream2, managed.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
            while ((num4 = stream.ReadByte()) != -1)
            {
                stream3.WriteByte((byte) num4);
            }
            stream3.Close();
            stream2.Close();
            stream.Close();
            foreach (byte num5 in stream2.ToArray())
            {
                str = str + num5.ToString("X2");
            }
            return str;
        }
        catch (Exception exception)
        {
            Log.Error(exception.Message);
        }
    }

}

+3
source share
4 answers

Works well for me with the test code below - are you sure that you are transmitting an encrypted string for decryption?

static void Main(string[] args)
    {

        string strInputString = "test";
        string strKeyString = "test123";
        string myIV = GetMacAddress();

        string encryptedString = Encrypt(strInputString, strKeyString, myIV);
        string decryptedString = Decrypt(encryptedString, strKeyString, myIV);

    }

    public static string Decrypt(string strInputString, string strKeyString, string myIV)
    {
        if ((strInputString == null) || (strInputString.Length == 0))
        {
            return strInputString;
        }

            int num5;
            int keySize = 0x100;
            int blockSize = 0x100;
            int length = keySize / 0x10;
            if (strKeyString.Length > length)
            {
                strKeyString = strKeyString.Substring(0, length);
            }
            if (strKeyString.Length < length)
            {
                strKeyString = strKeyString.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(strKeyString);
            if (myIV.Length > length)
            {
                myIV = myIV.Substring(0, length);
            }
            if (myIV.Length < length)
            {
                myIV = myIV.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(myIV);
            byte[] bytes = Encoding.Unicode.GetBytes(strKeyString);
            byte[] rgbIV = Encoding.Unicode.GetBytes(myIV);
            RijndaelManaged managed = new RijndaelManaged
            {
                BlockSize = blockSize,
                KeySize = keySize
            };
            MemoryStream stream = new MemoryStream();
            for (int i = 0; i < strInputString.Length; i += 2)
            {
                stream.WriteByte(byte.Parse(strInputString.Substring(i, 2), NumberStyles.AllowHexSpecifier));
            }
            stream.Position = 0L;
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream3 = new CryptoStream(stream, managed.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
            while ((num5 = stream3.ReadByte()) != -1)
            {
                stream2.WriteByte((byte)num5);
            }
            stream3.Close();
            stream2.Close();
            stream.Close();
            byte[] buffer3 = stream2.ToArray();
            return Encoding.Unicode.GetString(buffer3);

    }

    public static string Encrypt(string strInputString, string strKeyString, string myIV)
    {
        if ((strInputString == null) || (strInputString.Length == 0))
        {
            return strInputString;
        }           
            int num4;
            int keySize = 0x100;
            int blockSize = 0x100;
            int length = keySize / 0x10;
            if (strKeyString.Length > length)
            {
                strKeyString = strKeyString.Substring(0, length);
            }
            if (strKeyString.Length < length)
            {
                strKeyString = strKeyString.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(strKeyString);
            if (myIV.Length > length)
            {
                myIV = myIV.Substring(0, length);
            }
            if (myIV.Length < length)
            {
                myIV = myIV.PadRight(length, '#');
            }
            Encoding.Unicode.GetBytes(myIV);
            byte[] bytes = Encoding.Unicode.GetBytes(strKeyString);
            byte[] rgbIV = Encoding.Unicode.GetBytes(myIV);
            string str = "";
            RijndaelManaged managed = new RijndaelManaged
            {
                BlockSize = blockSize,
                KeySize = keySize
            };
            MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(strInputString));
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream3 = new CryptoStream(stream2, managed.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
            while ((num4 = stream.ReadByte()) != -1)
            {
                stream3.WriteByte((byte)num4);
            }
            stream3.Close();
            stream2.Close();
            stream.Close();
            foreach (byte num5 in stream2.ToArray())
            {
                str = str + num5.ToString("X2");
            }
            return str;

    }

    private static string GetMacAddress()
    {
        string macAddresses = "";

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }
        return macAddresses;
    }
0
source

The padding is probably Pkcs # 5. Instead of #, pad with a byte value, which is the number of bytes for the pad.

, 5 , 0505050505, 2 , 0202.

0

, IV. . , , , , .

, OP, - Decrypt(). IV, , . , IV MAC- , IV MAC- . - , 16 .

0

​​ , ?

... , , () . / , , . , ... . , - (8 , 16 ..).

, (, IV.

0

All Articles