private static byte[] EncryptCore(byte[] dataBytes, byte[] keyBytes, RCXOrder order = RCXOrder.DESC) { var first = RCXEncryptionProvider.Encrypt(dataBytes, keyBytes, order); var second = RCXEncryptionProvider.Encrypt(first, keyBytes, order); return(RCXEncryptionProvider.Encrypt(second, keyBytes, order)); }
private static unsafe byte[] EncryptCore(byte[] data, byte[] pass, RCXOrder order) { byte[] mBox = GetKey(pass, KEY_LENGTH); byte[] output = new byte[data.Length]; //int i = 0, j = 0; if (order == RCXOrder.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); } } } // byte[] mBox = GetKey(pass, KEY_LENGTH); // byte[] output = new byte[data.Length]; // int i = 0, j = 0; // // if (order == RCXOrder.ASC) { // for (int offset = 0; offset < data.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 temp2 = mBox[c]; // mBox[c] = mBox[a]; // mBox[a] = temp2; // j = (j + a + c); // } // } else { // 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 temp2 = mBox[c]; // mBox[c] = mBox[a]; // mBox[a] = temp2; // j = (j + a + c); // } // } return(output); }
/// <summary> /// Decrypt /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="order"></param> /// <returns></returns> public static byte[] Decrypt(byte[] data, byte[] key, RCXOrder order = RCXOrder.ASC) { return(EncryptCore(data, key, order)); }
/// <summary> /// Decrypt /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="encoding"></param> /// <param name="order"></param> /// <returns></returns> public static string Decrypt(string data, string key, Encoding encoding = null, RCXOrder order = RCXOrder.ASC) { encoding = encoding.SafeValue(); return(encoding.GetString(EncryptCore(Convert.FromBase64String(data), encoding.GetBytes(key), order))); }
/// <summary> /// Encrypt /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="encoding"></param> /// <param name="order"></param> /// <returns></returns> public static string Encrypt(byte[] data, string key, Encoding encoding = null, RCXOrder order = RCXOrder.ASC) { encoding = encoding.SafeValue(); return(Convert.ToBase64String(EncryptCore(data, encoding.GetBytes(key), order))); }
/// <summary> /// Encrypt /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="encoding"></param> /// <param name="order"></param> /// <returns></returns> public static string Encrypt(string data, string key, Encoding encoding = null, RCXOrder order = RCXOrder.DESC) { encoding = encoding.SafeValue(); var dataBytes = encoding.GetBytes(data); var keyBytes = encoding.GetBytes(key); return(Convert.ToBase64String(EncryptCore(dataBytes, keyBytes, order))); }