-1

I am developing an Envelope encryption library where envelope structure is combination of byte arrays.

keyAlias length , keyAlias , IVBytes, cipherBytes = 
[int][keyAlias][IV][cipherBytes]

As I am storing first 4 bytes of int to retrieve the length of keyAlias from envelope bytes, looking for correct handling of int to bytes and back to int.

Assumptions : I have decide to always store int bytes in Little-Endian format in envelope bytes.

Does below method for int to bytes correct. I have no way to test it as not having access to Big-Endian machine.

[Fact]
public void IntToByteConverion_ShouldConvertBack2()
{
    var intBytes = 2.ToLittleEndianBytes();

    //Store intBytes to Database or AWS S3
    // Retrive back the bytearray and convert to int32.

    int intValue = intBytes.FromLittleEndianBytes();

    Assert.Equal(2, intValue);
}

public static byte[] ToLittleEndianBytes(this int value)
{
    if (BitConverter.IsLittleEndian)
    return BitConverter.GetBytes(value);
    else
    {
        var bigEndianBytes = BitConverter.GetBytes(value);
        Array.Reverse(bigEndianBytes);// Converted to LittleEndian
        return bigEndianBytes;
    }
}

public static Int32 FromLittleEndianBytes(this byte[] littleEndianBytes)
{
    if (BitConverter.IsLittleEndian)
    return BitConverter.ToInt32(littleEndianBytes, 0);
    else
    {
        Array.Reverse(littleEndianBytes);// Converted to big endian as machine CPU is big endian
        return BitConverter.ToInt32(littleEndianBytes, 0);
    }
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Pravin
  • 810
  • 1
  • 10
  • 17
  • Possible duplicate of [How to get little endian data from big endian in c# using bitConverter.ToInt32 method?](https://stackoverflow.com/questions/8241060/how-to-get-little-endian-data-from-big-endian-in-c-sharp-using-bitconverter-toin) – Eliahu Aaron Jun 30 '19 at 11:54
  • In the question I have stated I encode myint with LittleEndian and transmit or store it in db as bytes. If you refer article : https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter?view=netframework-4.8 It says common convention is to send bytes in Big Endian way over the network. As I am not following this convention, just want to confirm this will not result in any bug. – Pravin Jul 01 '19 at 03:38

1 Answers1

1

What you have done works. To test it you can replace:

if(BitConverter.IsLittleEndian)

with:

if (false/*BitConverter.IsLittleEndian*/)

and check with the debugger if the bytes are actually reversed. If so (which it is) then it works, because the difference between Little-Endian and Big-Endian is the byte order.

Just one comment on your code (this is probably more in the scope of Code Review):

Using:

Array.Reverse(littleEndianBytes);

in method:

Int32 FromLittleEndianBytes(this byte[] littleEndianBytes)

is not the best practice since you are reversing the byte array sent to the method. The user of the method may not expect it and be confused.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37