internal static unsafe byte[] EncryptCore(byte[] data, byte[] pass, RcOrder order) { byte[] mBox = GetKey(pass, KEY_LENGTH); byte[] output = new byte[data.Length]; //int i = 0, j = 0; if (order == RcOrder.ASC) { fixed(byte *_mBox = &mBox[0]) fixed(byte *_data = &data[0]) fixed(byte *_output = &output[0]) { var length = data.Length; int i = 0, j = 0; for (Int64 offset = 0; offset < length; offset++) { i = (++i) & 0xFF; j = (j + *(_mBox + i)) & 0xFF; byte a = *(_data + offset); byte c = (byte)(a ^ *(_mBox + ((*(_mBox + i) + *(_mBox + j)) & 0xFF))); *(_output + offset) = c; byte temp = *(_mBox + a); *(_mBox + a) = *(_mBox + c); *(_mBox + c) = temp; j = (j + a + c); } } } else { fixed(byte *_mBox = &mBox[0]) fixed(byte *_data = &data[0]) fixed(byte *_output = &output[0]) { // ReSharper disable once UnusedVariable var length = data.Length; int i = 0, j = 0; for (int offset = data.Length - 1; offset >= 0; offset--) { i = (++i) & 0xFF; j = (j + *(_mBox + i)) & 0xFF; byte a = *(_data + offset); byte c = (byte)(a ^ *(_mBox + ((*(_mBox + i) + *(_mBox + j)) & 0xFF))); *(_output + offset) = c; byte temp = *(_mBox + a); *(_mBox + a) = *(_mBox + c); *(_mBox + c) = temp; j = (j + a + c); } } } return(output); }
private static byte[] EncryptCore(byte[] dataBytes, byte[] keyBytes, RcOrder order = RcOrder.DESC) { var pointer = dataBytes; for (var counter = 0; counter < 3; ++counter) { pointer = RCYFunction.EncryptCore(pointer, keyBytes, order); } return(pointer); }
public ThreeRCYFunction(RcKey key, RcOrder order = RcOrder.DESC) { Key = key ?? throw new ArgumentNullException(nameof(key)); Order = order; }