public static void AppendText(string path, string text) { FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (stream.ReadByte() == 0) { RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); for (int x = 0; x < stream.Length; x++) { enc.NextByte(); } stream.Seek(0, SeekOrigin.End); for (int x = 0; x < text.Length; x++) { stream.WriteByte((byte)(text[x] ^ enc.NextByte())); } } else { stream.Seek(0, SeekOrigin.End); for (int x = 0; x < text.Length; x++) { stream.WriteByte((byte)(text[x])); } } stream.Close(); }
public static void WriteAllLines(string path, string[] lines) { FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write); if (Config.I.EncryptFiles) { stream.WriteByte(0); RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); foreach (string line in lines) { for (int x = 0; x < line.Length; x++) { stream.WriteByte((byte)(line[x] ^ enc.NextByte())); } stream.WriteByte((byte)("\n".ToCharArray()[0] ^ enc.NextByte())); } } else { foreach (string line in lines) { for (int x = 0; x < line.Length; x++) { stream.WriteByte((byte)(line[x])); } stream.WriteByte((byte)("\n".ToCharArray()[0])); } } stream.Close(); }
public static string ReadAllText(string path) { string plain = ""; FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); stream.Seek(0, SeekOrigin.Begin); if (stream.ReadByte() == 0) { //Encrypted RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); int read = 0; while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); plain += (char)(read ^ enc.NextByte()); } } else { //Plain int read = 0; stream.Seek(0, SeekOrigin.Begin); while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); plain += (char)read; } } stream.Close(); return(plain); }
public static void EncryptFile(string path) { FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite); RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); while (stream.Position < stream.Length) { byte output = (byte)stream.ReadByte(); stream.Position--; stream.WriteByte((byte)(output ^ enc.NextByte())); } stream.Close(); }
public static string[] ReadAllLines(string path) { List<string> plain = new List<string>(); string plainBuf = ""; FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); if (stream.ReadByte() == 0) { //Encrypted RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); int read = 0; while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); char p = (char)(read ^ enc.NextByte()); if (p == "\n".ToCharArray()[0]) { if (plainBuf.EndsWith("\r")) plainBuf.Remove(plainBuf.LastIndexOf("\r")); if (!string.IsNullOrEmpty(plainBuf)) plain.Add(plainBuf); plainBuf = ""; } else plainBuf += p; } } else { //Plain stream.Seek(0, SeekOrigin.Begin); int read = 0; while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); char p = (char)(read); if (p == "\n".ToCharArray()[0]) { if (plainBuf.EndsWith("\r")) plainBuf.Remove(plainBuf.LastIndexOf("\r")); if (!string.IsNullOrEmpty(plainBuf)) plain.Add(plainBuf); plainBuf = ""; } else plainBuf += p; } } stream.Close(); if (!string.IsNullOrEmpty(plainBuf)) plain.Add(plainBuf); return plain.ToArray(); }
public static void WriteAllText(string path, string text) { if (Config.I.EncryptFiles) { FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write); stream.WriteByte(0); RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); for (int x = 0; x < text.Length; x++) { stream.WriteByte((byte)(text[x] ^ enc.NextByte())); } stream.Close(); } else { FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write); for (int x = 0; x < text.Length; x++) { stream.WriteByte((byte)(text[x])); } stream.Close(); } }
public static void AppendText(string path, string text) { FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (stream.ReadByte() == 0) { RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); for (int x = 0; x < stream.Length; x++) enc.NextByte(); stream.Seek(0, SeekOrigin.End); for (int x = 0; x < text.Length; x++) { stream.WriteByte((byte)(text[x] ^ enc.NextByte())); } } else { stream.Seek(0, SeekOrigin.End); for (int x = 0; x < text.Length; x++) { stream.WriteByte((byte)(text[x])); } } stream.Close(); }
public void Run() { RC4Encryption rc4Server = new RC4Encryption(); RC4Encryption rc4Client = new RC4Encryption(); rc4Server.makeSBox("zhangyawei"); rc4Client.makeSBox("zhangyawei"); IPAddress ip = IPAddress.Parse("127.0.0.1"); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { clientSocket.Connect(new IPEndPoint(ip, 26001)); Debug.logger.Log("connect Success."); } catch { Debug.logger.Log("connect Failed"); return; } if (true) { ClientAuthReq req = new ClientAuthReq("test", "123"); var binData = req.__encode().ToArray(); //binData = rc4Client.encryption(binData, binData.Length()); var sendData = new System.Collections.Generic.List <byte>(); NetHeader head = new NetHeader(); head.packLen = (UInt32)(4 + 2 + 2 + binData.Length); head.protoID = Proto4z.ClientAuthReq.getProtoID(); sendData.AddRange(head.__encode()); sendData.AddRange(binData); clientSocket.Send(sendData.ToArray()); var recvBytes = new byte[2000]; uint curLen = 0; uint needLen = 4 + 2 + 2; //暂时分两段读 后面要改buff接收提高效率 uint recvLen = 0; NetHeader recvHead = new NetHeader(); do { recvLen = (uint)clientSocket.Receive(recvBytes, (int)curLen, (int)needLen, System.Net.Sockets.SocketFlags.None);//第一段 if (recvLen == 0) { // remote close socket. return; } curLen += recvLen; needLen -= recvLen; if (needLen == 0 && curLen == 4 + 2 + 2) ////第一段 完成 { int pos = 0; recvHead.__decode(recvBytes, ref pos); needLen = recvHead.packLen - 4 - 2 - 2; //设置第二段 } else if (needLen == 0) //第二段完成 { if (recvHead.protoID == Proto4z.ClientAuthResp.getProtoID()) { ClientAuthResp result = new ClientAuthResp(); int pos = 4 + 2 + 2; result.__decode(recvBytes, ref pos); Debug.logger.Log("ClientAuthResp: account=" + result.account + ", token=" + result.token + ",retCode=" + result.retCode); int t = 0; t++; } else if (true) //other proto { } break; //一个协议接收处理完毕后break } recvLen = 0; } while (true); } }
static void Main(string[] args) { RC4Encryption rc4Server = new RC4Encryption(); RC4Encryption rc4Client = new RC4Encryption(); rc4Server.makeSBox("zhangyawei"); rc4Client.makeSBox("zhangyawei"); TestIntegerData idata = new TestIntegerData('a', 1, 5, 20, 30, 40, 50, 60, 70); TestFloatData fdata = new TestFloatData(243.123123f, 32432.123); TestStringData sdata = new TestStringData("love"); EchoPack pack = new EchoPack(); pack._iarray = new Proto4z.TestIntegerDataArray(); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._farray = new Proto4z.TestFloatDataArray(); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._sarray = new Proto4z.TestStringDataArray(); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._imap = new Proto4z.TestIntegerDataMap(); pack._imap.Add("123", idata); pack._imap.Add("223", idata); pack._fmap = new Proto4z.TestFloatDataMap(); pack._fmap.Add("523", fdata); pack._fmap.Add("623", fdata); pack._smap = new Proto4z.TestStringDataMap(); pack._smap.Add("723", sdata); pack._smap.Add("823", sdata); var now = DateTime.UtcNow; for (int i = 0; i < 1; i++) { var byteData = pack.__encode(); var binData = byteData.ToArray(); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } rc4Server.encryption(binData, binData.Length); rc4Client.encryption(binData, binData.Length); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } var v = new EchoPack(); int pos = 0; v.__decode(binData, ref pos); Client client = new Client(); client.Run(binData); } System.Console.WriteLine(DateTime.UtcNow - now); }
public static string[] ReadAllLines(string path) { List <string> plain = new List <string>(); string plainBuf = ""; FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); if (stream.ReadByte() == 0) { //Encrypted RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); int read = 0; while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); char p = (char)(read ^ enc.NextByte()); if (p == "\n".ToCharArray()[0]) { if (plainBuf.EndsWith("\r")) { plainBuf.Remove(plainBuf.LastIndexOf("\r")); } if (!string.IsNullOrEmpty(plainBuf)) { plain.Add(plainBuf); } plainBuf = ""; } else { plainBuf += p; } } } else { //Plain stream.Seek(0, SeekOrigin.Begin); int read = 0; while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); char p = (char)(read); if (p == "\n".ToCharArray()[0]) { if (plainBuf.EndsWith("\r")) { plainBuf.Remove(plainBuf.LastIndexOf("\r")); } if (!string.IsNullOrEmpty(plainBuf)) { plain.Add(plainBuf); } plainBuf = ""; } else { plainBuf += p; } } } stream.Close(); if (!string.IsNullOrEmpty(plainBuf)) { plain.Add(plainBuf); } return(plain.ToArray()); }
static void Main(string[] args) { RC4Encryption rc4Server = new RC4Encryption(); RC4Encryption rc4Client = new RC4Encryption(); rc4Server.makeSBox("zhangyawei"); rc4Client.makeSBox("zhangyawei"); IntegerData idata = new IntegerData('a', 1, 5, 20, 30, 40, 50, 60); FloatData fdata = new FloatData(243.123123f, 32432.123); StringData sdata = new StringData("love"); EchoPack pack = new EchoPack(); pack._iarray = new IntegerDataArray(); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._iarray.Add(idata); pack._farray = new FloatDataArray(); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._farray.Add(fdata); pack._sarray = new StringDataArray(); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._sarray.Add(sdata); pack._imap = new IntegerDataMap(); pack._imap.Add("123", idata); pack._imap.Add("223", idata); pack._fmap = new FloatDataMap(); pack._fmap.Add("523", fdata); pack._fmap.Add("623", fdata); pack._smap = new StringDataMap(); pack._smap.Add("723", sdata); pack._smap.Add("823", sdata); var now = DateTime.UtcNow; for (int i = 0; i < 1; i++) { var byteData = pack.__encode(); var binData = byteData.ToArray(); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } rc4Server.encryption(binData, binData.Length); rc4Client.encryption(binData, binData.Length); // for (int i = 0; i < binData.Length; i++) // { // System.Console.WriteLine((int)binData[i]); // } var v = new EchoPack(); int pos = 0; v.__decode(binData, ref pos); try { SimplePack pk = new SimplePack(10, "name", 100, null); pk.moneyTree = new MoneyTree(10001, 5, 5, 0, 0); var binMemory = pk.__encode().ToArray(); //序列化 SimplePack recvPK = new SimplePack(); int curPos = 0; recvPK.__decode(binMemory, ref curPos); System.Console.Write("success"); } catch (Exception e) { System.Console.Write("error"); } Client client = new Client(); client.Run(binData); } System.Console.WriteLine(DateTime.UtcNow - now); }
public static string ReadAllText(string path) { string plain = ""; FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); stream.Seek(0, SeekOrigin.Begin); if (stream.ReadByte() == 0) { //Encrypted RC4Encryption enc = new RC4Encryption(Config.I.FilePassword); int read = 0; while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); plain += (char)(read ^ enc.NextByte()); } } else { //Plain int read = 0; stream.Seek(0, SeekOrigin.Begin); while (read != -1 && stream.Position < stream.Length) { read = stream.ReadByte(); plain += (char)read; } } stream.Close(); return plain; }