Dark Side
Dark Side

Reputation: 715

Ror byte array with C#

is there a way to Ror an entire byte[] by a specific amount? I've already done some research and found a solution to Rol a byte[] :

public static byte[] ROL_ByteArray(byte[] arr, int nShift)
{
    //Performs bitwise circular shift of 'arr' by 'nShift' bits to the left
    //RETURN:
    //      = Result
    byte[] resArr = new byte[arr.Length];

    if(arr.Length > 0)
    {
        int nByteShift = nShift / (sizeof(byte) * 8);   //Adjusted after @dasblinkenlight's correction
        int nBitShift = nShift % (sizeof(byte) * 8);

        if (nByteShift >= arr.Length)
            nByteShift %= arr.Length;

        int s = arr.Length - 1;
        int d = s - nByteShift;

        for (int nCnt = 0; nCnt < arr.Length; nCnt++, d--, s--)
        {
            while (d < 0)
                d += arr.Length;
            while (s < 0)
                s += arr.Length;

            byte byteS = arr[s];

            resArr[d] |= (byte)(byteS << nBitShift);
            resArr[d > 0 ? d - 1 : resArr.Length - 1] |= (byte)(byteS >> (sizeof(byte) * 8 - nBitShift));


        }
    }

    return resArr;
}

The author of this code can be found here: Is there a function to do circular bitshift for a byte array in C#?

Any idea how I can do the same thing but perform a Ror operation instead of a Rol operation on a byte[] ?

Upvotes: 0

Views: 484

Answers (1)

Serj-Tm
Serj-Tm

Reputation: 16981

static byte[] ROR_ByteArray(byte[] arr, int nShift)
{
  return ROL_ByteArray(arr, arr.Length*8-nShift);
}

Upvotes: 1

Related Questions