static string DecodeString(byte[] data) { var ms = new MemoryStream(data); var xs = new XorStream(ms); byte[] buff = new byte[data.Length]; xs.Read(buff, 0, buff.Length); return(Encoding.UTF8.GetString(buff)); }
private Task <bool> DoCipher(Stream input, Stream output, IProgress <ProgressReport> progress, bool decrypt) { var requestId = Guid.NewGuid().ToString("N"); var key = OnRequestKey("XOR Key", -1, requestId, out var error); if (key == null) { return(Task.Factory.StartNew(() => { progress.Report(new ProgressReport { Percentage = 0, Message = error }); return false; })); } return(Task.Factory.StartNew(() => { progress.Report(new ProgressReport { Percentage = 0, Message = decrypt ? "Decryption..." : "Encryption..." }); using (var xor = new XorStream(decrypt ? input : output, key)) { var buffer = new byte[0x10000]; var totalLength = decrypt ? xor.Length : input.Length; while (xor.Position < totalLength) { var length = (int)Math.Min(0x10000, totalLength - xor.Position); if (decrypt) { xor.Read(buffer, 0, length); output.Write(buffer, 0, length); } else { input.Read(buffer, 0, length); xor.Write(buffer, 0, length); } progress.Report(new ProgressReport { Percentage = (double)xor.Position / totalLength * 100, Message = decrypt ? "Decryption..." : "Encryption...", }); } } progress.Report(new ProgressReport { Percentage = 100, Message = decrypt ? "Decryption finished." : "Encryption finished." }); return true; })); }
public void ReadBytes() { var data = Enumerable.Range(0, 256).Select(d => (byte)d).ToArray(); byte encByte = 0x73; var dataExpected = data.Select(d => d ^ encByte).Select(d => (byte)d).ToArray(); using (var ms = new MemoryStream(data)) { var stream = new XorStream(ms, encByte); var output = new byte[256]; stream.Read(output, 0, 256); Check.That(output).ContainsExactly(dataExpected); } }