0
votes

I am trying to create decrypt function for 32-byte hex string representing 16-byte data this is what I tried so far (I found a working JS example here: http://www.hanewin.net/encrypt/aes/aes-test.htm ):

public static string DecryptString(string code)
    {

        string text = hex2s("0102030405060708090a0b0c0d0e0f11");
        string key = hex2s("ed7c0c82daf513b81c32f6655e8fd4ee");

        //Expected result: 260fe45846fb26dbb1d28d8166d4a89f

        return BitConverter.ToString(Encoding.Default.GetBytes(decrypt(text, key)));
    }


    private static string hex2s(string hex)
    {
        var r = "";
        if (hex.IndexOf("0x") == 0 || hex.IndexOf("0X") == 0) hex = hex.Substring(2);

        if (hex.Length % 2 > 0) hex += '0';

        for (var i = 0; i < hex.Length; i += 2)
        {
            int thisCharCode = hex[i];
            char newCharCode = (char)(thisCharCode);
            r = r + newCharCode;
        }
        return r;
    }

    public static String decrypt(String imput, String key)
    {
        byte[] data = Convert.FromBase64String(imput);
        String decrypted;

        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Encoding.ASCII.GetBytes(key);
            rijAlg.Mode = CipherMode.ECB;
            rijAlg.BlockSize = 128;
            rijAlg.Padding = PaddingMode.Zeros;

            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
            using (MemoryStream msDecrypt = new MemoryStream(data))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        return decrypted;
    }

I am getting an error: "Length of the data to decrypt is invalid."

I will highly appreciated any suggestion.

1
Do you check what is the length of your data to decrypt? Is it multiplication of 16? Where does the error occur (which line)? - Ian
The data it is sent in a lowercase hex string format where each byte is 2 characters. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 - kleinohad
this is where the error occur decrypted = srDecrypt.ReadToEnd(); - kleinohad
For starters, I'd avoid using string everywhere. Also, you're using FromBase64String on a string that hasn't been generated by Base64 encoding. - Damien_The_Unbeliever
Never use ECB mode. It's deterministic and therefore not semantically secure. You should at the very least use a randomized mode like CBC or CTR. It is better to authenticate your ciphertexts so that attacks like a padding oracle attack are not possible. This can be done with authenticated modes like GCM or EAX, or with an encrypt-then-MAC scheme. - Artjom B.

1 Answers

0
votes

After reviewing your comments I found the solution:

public static string DecryptString(string code)
    {

        byte[] text = FromHex("0102030405060708090a0b0c0d0e0f11");
        byte[] key = FromHex("ed7c0c82daf513b81c32f6655e8fd4ee");

        //Expected result: 260fe45846fb26dbb1d28d8166d4a89f

        return BitConverter.ToString(Encoding.Default.GetBytes(decrypt(text, key)));
    }

    public static byte[] FromHex(string hex)
    {
        byte[] raw = new byte[hex.Length / 2];
        for (int i = 0; i < raw.Length; i++)
        {
            raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
        }
        return raw;
    } 


    public static String decrypt(byte[] input, byte[] key)
    {
        byte[] data = input;
        String decrypted;

        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = key;
            rijAlg.Mode = CipherMode.ECB;
            rijAlg.BlockSize = 128;
            rijAlg.Padding = PaddingMode.Zeros;

            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
            using (MemoryStream msDecrypt = new MemoryStream(data))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        return decrypted;
    }

Thanks for your help @Damien_The_Unbeliever and @Ian