diff --git a/lib/Request.cs b/lib/Request.cs index 97516c3..d22ed3f 100644 --- a/lib/Request.cs +++ b/lib/Request.cs @@ -1,6 +1,7 @@ namespace lib; -public enum RequestType { +public enum RequestType +{ Register = 1, ConfirmRegister = 2, Login = 3, @@ -9,20 +10,25 @@ public enum RequestType { SendMessage = 6, } -public static class Request { - public static byte[] CreateRequest(RequestType Type, ref byte counter, byte[] data) { - if(data.Length > 13) { +public static class Request +{ + public static byte[] CreateRequest(RequestType Type, ref byte counter, byte[] data) + { + if (data.Length > 13) + { throw new Exception("extra data is too long: " + data.Length.ToString()); } - byte[] msg = new byte[128]; + byte[] msg = new byte[16]; msg[0] = 0; // version msg[1] = (byte)Type; msg[2] = counter; - if(counter == byte.MaxValue) { + if (counter == byte.MaxValue) + { counter = 0; } - else { + else + { counter += 1; } @@ -30,4 +36,46 @@ public static class Request { Array.Copy(data, 0, msg, 3, data.Length); return msg; } + + public static string RequestToString(byte[] Request) + { + if (Request.Length != 16) + { + throw new Exception("Request size is not 128!"); + } + string res = $"V: {Request[0]}, "; + res += $"Request type: {(RequestType)Request[1]}, "; + res += $"Counter: {Request[2]}\n"; + res += $"Extra: {string.Join(' ', Request[3..].Select(b => b.ToString("{b8}")))}"; + // also display extra data based on the request itself + switch ((RequestType)Request[1]) + { + case RequestType.Register: + string phone = Utils.BytesToNumber(Request[3..11]); + int rsaLen = BitConverter.ToInt32(Request, 11); + res += $"Phone: {phone}, RSA Key length: {rsaLen}"; + break; + case RequestType.ConfirmRegister: + string code = string.Join(' ', Request[3..9]); + int sigLen = BitConverter.ToInt32(Request, 9); + res += $"Code: {code}, signature length: {sigLen}"; + break; + case RequestType.Login: + phone = Utils.BytesToNumber(Request[3..11]); + sigLen = BitConverter.ToInt16(Request, 11); + res += $"Phone: {phone}, signature length: {sigLen}"; + break; + case RequestType.GetMessages: + break; + case RequestType.GetUserKey: + break; + case RequestType.SendMessage: + break; + default: + res += "INVALID REQUEST TYPE"; + break; + } + + return res; + } } \ No newline at end of file diff --git a/lib/Utils.cs b/lib/Utils.cs index 8139470..c55b738 100644 --- a/lib/Utils.cs +++ b/lib/Utils.cs @@ -1,18 +1,22 @@ -using System.Text; using System.Linq; +using System.Text; namespace lib; -public static class Utils { - public static byte[] NumberToBytes(string Number) { - if(Number.Any(c => !char.IsDigit(c)) || Number.Length > 16) { +public static class Utils +{ + public static byte[] NumberToBytes(string Number) + { + if (Number.Any(c => !char.IsDigit(c)) || Number.Length > 16) + { throw new Exception("Invalid arguments!"); } byte[] res = Enumerable.Repeat((byte)0b1111_1111, 8).ToArray(); // Pad Number if needed to be of even length (because each 2 digits are turned into 1 byte) Number = Number.Length % 2 == 0 ? Number : Number + '-'; - for(int i = 0; i < Number.Length - 1; i += 2) { + for (int i = 0; i < Number.Length - 1; i += 2) + { char c1 = Number[i]; char c2 = Number[i + 1]; res[i / 2] = (byte)((DigitToByte(c1) << 4) | DigitToByte(c2)); @@ -20,33 +24,41 @@ public static class Utils { return res; } - public static string BytesToNumber(byte[] Bytes) { + public static string BytesToNumber(byte[] Bytes) + { string s = ""; - foreach(byte b in Bytes) { + foreach (byte b in Bytes) + { byte b1 = (byte)((b >> 4) & 0b1111); byte b2 = (byte)(b & 0b1111); s = s + ByteToDigit(b1) + ByteToDigit(b2); } - return new string(s.Where(c => char.IsDigit(c)).ToArray()); + return new string(s.Where(char.IsDigit).ToArray()); } - public static byte DigitToByte(char c) { - if (int.TryParse(c.ToString(), out int d)) { + public static byte DigitToByte(char c) + { + if (int.TryParse(c.ToString(), out int d)) + { return (byte)d; } - else { + else + { return 0b1111; // empty, turned into '-' later to be discarded } } - public static char ByteToDigit(byte b) { + public static char ByteToDigit(byte b) + { byte offset = Encoding.ASCII.GetBytes("0")[0]; - if(b == 0b1111) { + if (b == 0b1111) + { return '-'; } - else { + else + { return Encoding.ASCII.GetChars([(byte)(b + offset)])[0]; } } diff --git a/server/Program.cs b/server/Program.cs index 48ddc68..ca820d3 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -60,8 +60,8 @@ public class Program // Get AES session key int len = await stream.ReadAsync(buffer); Aes sk = Aes.Create(); - sk.Key = buffer[..256]; // just to make sure no one sends a too big to be true key - sk.IV = buffer[256..len]; + sk.Key = buffer[..32]; // just to make sure no one sends a too big to be true key + sk.IV = buffer[32..len]; Write(id, "key + iv: " + len.ToString()); // Get first message (should be either login or )