public static byte[] Encrypt(byte[] input, uint ms) { var ct = new CipherText(input, ms); var iv = Cipher8FromRand(ref ms); //encrypt foreach (var bytes in ct.Content) { for (var j = 0; j < 256; j++) { bytes[j] ^= iv[j]; } var temp2 = new uint[0x100 / 4]; Buffer.BlockCopy(bytes, 0, temp2, 0, 0x100); ShufflesLegacy.Shuffle2(temp2); Buffer.BlockCopy(temp2, 0, iv, 0, 0x100); Buffer.BlockCopy(temp2, 0, bytes, 0, 0x100); } return(ct.GetBytes(ref ms)); }
//this returns an empty buffer if error public static byte[] Decrypt(byte[] input, out int length) { int version, len = input.Length; if (len < 261) { length = 0; return(new byte[] { }); } var modSize = len % 256; switch (modSize) { case 32: version = 1; break; case 33: version = 2; break; case 5: version = 3; break; default: length = 0; return(new byte[] { }); } byte[] cipher8, output; int outputLen; switch (version) { case 1: outputLen = len - 32; output = new byte[outputLen]; Buffer.BlockCopy(input, 32, output, 0, outputLen); cipher8 = Cipher8FromIv(input); break; case 2: outputLen = len - 33; output = new byte[outputLen]; Buffer.BlockCopy(input, 32, output, 0, outputLen); cipher8 = Cipher8FromIv(input); break; default: outputLen = len - 5; output = new byte[outputLen]; Buffer.BlockCopy(input, 4, output, 0, outputLen); var tmp = new byte[4]; Buffer.BlockCopy(input, 0, tmp, 0, 4); Array.Reverse(tmp); var ms = BitConverter.ToUInt32(tmp, 0); cipher8 = Cipher8FromRand(ref ms); if (input[len - 1] != MakeIntegrityByte(GenerateRand(ref ms))) { length = 0; return(new byte[] { }); } break; } var outputcontent = new Collection <byte[]>(); //break into chunks of 256 var roundedsize = (outputLen + 255) / 256; //round up for (var i = 0; i < roundedsize; i++) { outputcontent.Add(new byte[256]); } for (var i = 0; i < outputLen; i++) { outputcontent[i / 256][i % 256] = output[i]; } foreach (var bytes in outputcontent) { var temp2 = new uint[0x100 / 4]; var temp3 = new uint[0x100 / 4]; Buffer.BlockCopy(bytes, 0, temp2, 0, 0x100); Buffer.BlockCopy(temp2, 0, temp3, 0, 0x100); if (version == 1) { ShufflesLegacy.Unshuffle(temp2); } else { ShufflesLegacy.Unshuffle2(temp2); } Buffer.BlockCopy(temp2, 0, bytes, 0, 0x100); for (var j = 0; j < 256; j++) { bytes[j] ^= cipher8[j]; } Buffer.BlockCopy(temp3, 0, cipher8, 0, 0x100); } var ret = new byte[outputLen]; for (var i = 0; i < outputcontent.Count; i++) { Buffer.BlockCopy(outputcontent[i], 0, ret, i * 256, 0x100); } length = outputLen - ret.Last(); return(ret); }