Home > Mobile >  C# SymmetricAlgorithm cannot explicitly call operator or accessor
C# SymmetricAlgorithm cannot explicitly call operator or accessor

Time:01-30

Hey i trying decrypt byte to string, but something go wrong i got some errors like source.cs(101,55): error CS0571: 'SymmetricAlgorithm.KeySize.set': cannot explicitly call operator or accessor source.cs(102,34): error CS0571: 'SymmetricAlgorithm.BlockSize.set': cannot explicitly call operator or accessor source.cs(104,97): error CS0571: 'SymmetricAlgorithm.KeySize.get': cannot explicitly call operator or accessor

i want decrypt 'test' static byte

my script

                private static readonly byte[] saltBytes = new byte[16]
                {
                    255,
                    64,
                    191,
                    111,
                    23,
                    3,
                    113,
                    119,
                    231,
                    121,
                    252,
                    112,
                    79,
                    32,
                    114,
                    156
                };

                private static readonly byte[] cryptKey = new byte[38]
                {
                    104,
                    116,
                    116,
                    112,
                    115,
                    58,
                    47,
                    47,
                    103,
                    105,
                    116,
                    104,
                    117,
                    98,
                    46,
                    99,
                    111,
                    109,
                    47,
                    76,
                    105,
                    109,
                    101,
                    114,
                    66,
                    111,
                    121,
                    47,
                    83,
                    116,
                    111,
                    114,
                    109,
                    75,
                    105,
                    116,
                    116,
                    121
                };
public static string test = Decrypt(new byte[32]
                {
                    169,
                    182,
                    79,
                    179,
                    252,
                    54,
                    138,
                    148,
                    167,
                    99,
                    216,
                    216,
                    199,
                    219,
                    10,
                    249,
                    131,
                    166,
                    170,
                    145,
                    237,
                    248,
                    142,
                    78,
                    196,
                    137,
                    101,
                    62,
                    142,
                    107,
                    245,
                    134
                });
                public static string Decrypt(byte[] bytesToBeDecrypted)
                {

                    byte[] bytes = null;
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        RijndaelManaged val = new RijndaelManaged();
                        try
                        {
                            ((SymmetricAlgorithm)val).set_KeySize(256);
                            ((SymmetricAlgorithm)val).set_BlockSize(128);
                            Rfc2898DeriveBytes val2 = new Rfc2898DeriveBytes(cryptKey, saltBytes, 1000);
                            ((SymmetricAlgorithm)val).set_Key(((DeriveBytes)val2).GetBytes(((SymmetricAlgorithm)val).get_KeySize() / 8));
                            ((SymmetricAlgorithm)val).set_IV(((DeriveBytes)val2).GetBytes(((SymmetricAlgorithm)val).get_BlockSize() / 8));
                            ((SymmetricAlgorithm)val).set_Mode((CipherMode)1);
                            CryptoStream val3 = new CryptoStream((Stream)memoryStream, ((SymmetricAlgorithm)val).CreateDecryptor(), (CryptoStreamMode)1);
                            try
                            {
                                ((Stream)(object)val3).Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                                ((Stream)(object)val3).Close();
                            }
                            finally
                            {
                                ((IDisposable)val3)?.Dispose();
                            }
                            bytes = memoryStream.ToArray();
                        }
                        finally
                        {
                            ((IDisposable)val)?.Dispose();
                        }
                    }
                    return Encoding.UTF8.GetString(bytes);
                }

CodePudding user response:

Basically, property methods get_% or set_% is hided and secured by BindingFlags.NonPublic | BindingFlags.Instance. Seems be like mixing code from Reflection. It is not necessary, use standard assignment operators/public methods.

And... do not implicit cast RijndaelManaged to SymmetricAlgorithm multiple times - these are derived classes. It will works any case, but may hit performance.

public static string Decrypt(byte[] bytesToBeDecrypted)
{
  byte[] bytes = Array.Empty<byte>();
  using (MemoryStream ms = new MemoryStream())
  {
    using (RijndaelManaged val = new RijndaelManaged())
    {
      val.KeySize = 256;
      val.BlockSize = 128;
      Rfc2898DeriveBytes val2 = new Rfc2898DeriveBytes(cryptKey, saltBytes, 1000);
      val.Key = val2.GetBytes(val.KeySize / 8);
      val.IV = val2.GetBytes(val.BlockSize / 8);
      val.Mode = CipherMode.CBC;
      using (CryptoStream val3 = new CryptoStream(ms, val.CreateDecryptor(), CryptoStreamMode.Write))
      {
        val3.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
      }
    }
    bytes = ms.ToArray();
  }
  return Encoding.UTF8.GetString(bytes);
}

CodePudding user response:

The error messages you're seeing indicate that you're trying to set the KeySize and BlockSize properties of the SymmetricAlgorithm class using the set_ prefix, which is not the correct syntax. Instead, you should use the regular dot notation to set the properties, like this:

val.KeySize = 256;
val.BlockSize = 128;

Additionally, the get_ prefix should be removed as well when trying to access the KeySize and BlockSize properties.

val2.GetBytes(val.KeySize / 8)
val2.GetBytes(val.BlockSize / 8)

Also, the Key and IV should be set using

val.Key = val2.GetBytes(val.KeySize / 8);
val.IV = val2.GetBytes(val.BlockSize / 8);

Once you've made these changes, the Decrypt method should work correctly and should be able to decrypt the byte array you've provided.

  •  Tags:  
  • c#
  • Related