0

In function aes_encrypt() I want to pass only first 16 elements of an array not whole array. I am new to C#. I am getting an error that "Cannot Convert from byte to byte []"

byte[][] packet = new byte[12][];
private static byte[] key_dec_pswd = new byte[16];
for (int loop = 0; loop < noofbyte; loop++)
{
   packet[addr / 512][addr % 512 + 4 + loop] = Convert.ToByte(RawDataLine.Substring((9 + (loop * 2)), 2), 16);
}
int j = packet[i].Length;
byte[] tmpPacket = new byte[j];

for (int k = 0; k < j; k++)
{
   tmpPacket[k] = packet[i][k];
}

aes_encrypt(tmpPacket[16], key_dec_pswd); //Getting Error here. Cannot Convert from byte to byte []

public static void aes_encrypt(byte[] chlng_byte, byte[] key)
{
   aes128_encrypt(chlng_byte, key);
}
2
  • 1
    What is the datatype of "key_dec_pswd"? it's not explicit in your code Commented Mar 17, 2022 at 14:23
  • private static byte[] key_dec_pswd = new byte[16]; Commented Mar 17, 2022 at 14:41

1 Answer 1

2

Change

aes_encrypt(tmpPacket[16], key_dec_pswd);

to

aes_encrypt(tmpPacket, key_dec_pswd);

tmpPacket[16] passes only byte 16 into the function instead of the intended array and thus the error.

You can also simplify the byte array copy from:

int j = packet[i].Length;
byte[] tmpPacket = new byte[j];

for (int k = 0; k < j; k++)
{
   tmpPacket[k] = packet[i][k];
}

to:

var tmpPacket = packet[i].ToArray();

This does the allocation and copy all in one.

Sign up to request clarification or add additional context in comments.

1 Comment

The error is gone. How do I increase the tmpPacket pointer by 16 bytes. The function aes_encrypt encrypts only 16 bytes. After first 16 bytes encryption, how do I move to next 16 bytes and so on. The total length of packet[i] is 518 bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.