byte[] createRawData(string s) { MemoryStream ms = new MemoryStream(); byte[] pad = null; if (PadSettings.HasValue) { byte[] h = null; PadHeader ph = new PadHeader(); pad = CreatePad(ref h, ref ph); if (h != null) { ms.WriteAll(h); } if (ph.Location == PadLocation.Start) { ms.WriteAll(pad); pad = null; } } ms.WriteAll(s.EncodeToByteArray(Encoding.UTF8)); if (pad != null) { ms.WriteAll(pad); } return(ms.ToFinalArray()); }
public void FromPadHeader(PadHeader ph, byte[] seq) { size = ph.size; location = ph.Location; type = ph.Type; _t_fc_char = ph.fc_char; _t_fc_seq = (seq != null && seq.Length > 0)?seq:null; }
private string stripPadding(byte[] raw, out PadSettings?ps) { MemoryStream ms = new MemoryStream(raw); ms.Position = 0; if (ms.ReadValue <uint>() == PadHeader.PAD_ENABLED_MAGIC) { PadHeader ph = ms.ReadValue <PadHeader>(); byte[] seq = new byte[ph.fc_seq_size]; ms.ReadAll(seq); var _ps = new PadSettings(); _ps.FromPadHeader(ph, seq); ps = _ps; if (ph.Location == PadLocation.Start) { ms.Position += ph.size; using (var ms2 = new MemoryStream()) { ms.CopyTo(ms2); ms.Close(); return(ms2.ToArray().DecodeToString(Encoding.UTF8)); } } else { using (var ms2 = new MemoryStream()) { ms.CopyTo(ms2); ms.Close(); byte[] ar = ms2.ToArray(); return(ar.SubArray(0, ar.Length - ph.size).DecodeToString(Encoding.UTF8)); } } } else { ms.Close(); ps = null; return(raw.DecodeToString(Encoding.UTF8)); } }
public PadHeader CreatePadHeader(RNGCryptoServiceProvider rng, ref byte[] seq) { PadHeader ph = new PadHeader(); Random r = pad_rng(rng); ph.size = size ?? r.Next(global::et.PadSettings.RANDOM_SIZE_BOUND_LOW, global::et.PadSettings.RANDOM_SIZE_BOUND_HIGH); ph.Location = location ?? (PadLocation)r.Next(0, 1); ph.fc_char = type == null ? (char)r.Next(0, 255) : _t_fc_char; seq = type == null?pad_rng_seq(rng, r) : _t_fc_seq; if (seq == null) { ph.fc_seq_size = 0; } else { ph.fc_seq_size = seq.Length; } ph.Type = type ?? (PadType)(r.Next(0, 3)); return(ph); }
public byte[] CreatePad(ref byte[] padHeaderFull, ref PadHeader ph) { if (PadSettings == null) { return(null); } else { var ps = PadSettings.Value; byte[] seq = null; ph = ps.CreatePadHeader(rng, ref seq); if (seq != null) { padHeaderFull = global::et.PadHeader.PAD_ENABLED_MAGIC.ToByteArray().Concat(ph.ToByteArray().Concat(seq)).ToArray(); } else { padHeaderFull = global::et.PadHeader.PAD_ENABLED_MAGIC.ToByteArray().Concat(ph.ToByteArray()).ToArray(); } MemoryStream actualPad = new MemoryStream(); switch (ph.Type) { case PadType.Zero: actualPad.WriteAll(new byte[ph.size]); break; case PadType.Random: { byte[] v = new byte[ph.size]; rng.GetBytes(v); actualPad.WriteAll(v); } break; case PadType.FixedC: { for (int i = 0; i < ph.size; i++) { actualPad.WriteByte((byte)ph.fc_char); } } break; case PadType.FixedS: { for (int i = 0, j = 0; i < ph.size; i++) { if (j >= seq.Length) { j = 0; } actualPad.WriteByte(seq[j]); j += 1; } } break; } return(actualPad.ToFinalArray()); /* * var ps = PadSettings.Value; * MemoryStream ms = new MemoryStream(); * ms.WriteValue<uint>(PadHeader.PAD_ENABLED_MAGIC); * ph = new PadHeader(); * ph.size = ps.size ?? r.Next(global::et.PadSettings.RANDOM_SIZE_BOUND_LOW, global::et.PadSettings.RANDOM_SIZE_BOUND_HIGH); * ph.Location = ps.location?? (PadLocation) r.Next(0, 1); * header = ms.ToFinalArray(); * * var type = ps.type ?? (PadType)(r.Next(0, 3)); * MemoryStream actualPad = new MemoryStream(); * switch (type) * { * case PadType.Zero: ms.WriteAll(new byte[ph.size]); break; * case PadType.Random: * { * byte[] v = new byte[ph.size]; * rng.GetBytes(v); * actualPad.WriteAll(v); * } break; * case PadType.FixedC: * { * char c = ps.type == null ? (char)r.Next(0, 255) : ps._t_fc_char; * for (int i = 0; i < ph.size; i++) actualPad.WriteByte((byte)c); * } break; * case PadType.FixedS: * { * byte[] seq = ps.type == null ? pad_rng_seq(r) : ps._t_fc_seq; * for (int i = 0, j = 0; i < ph.size; i++) * { * if (j >= seq.Length) j = 0; * actualPad.WriteByte(seq[j]); * j += 1; * } * } break; * } * return actualPad.ToFinalArray();*/ } }