public static string DeserializateMessage(byte[] message, TlsManager tlsManager, RsaManager rsaManager, out bool isAllSuccess) { isAllSuccess = false; byte[] IV = new byte[16], hashSum, digitalSignature = new byte[256], messageBytes; for (int i = 0; i < IV.Length; i++) { IV[i] = message[i]; } hashSum = new byte[message[16]]; for (int i = IV.Length + 1, j = 0; j < hashSum.Length; i++, j++) { hashSum[j] = message[i]; } for (int i = IV.Length + 1 + hashSum.Length, j = 0; j < digitalSignature.Length; i++, j++) { digitalSignature[j] = message[i]; } messageBytes = new byte[message.Length - IV.Length - 1 - hashSum.Length - digitalSignature.Length]; for (int i = IV.Length + 1 + hashSum.Length + digitalSignature.Length, j = 0; j < messageBytes.Length; i++, j++) { messageBytes[j] = message[i]; } bool isSuccess; string decryptMessage = tlsManager.DecryptMessage(messageBytes, IV, hashSum, out isSuccess); string rsaNotParsedKey = decryptMessage.Substring(decryptMessage.IndexOf("|rsaKeyStart|"), decryptMessage.LastIndexOf("|rsaKeyEnd|") + "|rsaKeyEnd|".Length); string rsaKey = rsaNotParsedKey.Replace("|rsaKeyStart|", ""); rsaKey = rsaKey.Replace("|rsaKeyEnd|", ""); bool isValidDigitalSignature = rsaManager.IsValidDigitalSignature(hashSum, digitalSignature, rsaKey); decryptMessage = decryptMessage.Replace(rsaNotParsedKey, ""); if ((isSuccess == true) & (isValidDigitalSignature == true)) { isAllSuccess = true; } return(decryptMessage); }
public static byte[] SerializateMessage(string message, TlsManager tlsManager, RsaManager rsaManager) { byte[] IV, hashSum, digitalSignature, hashSumCount, messageBytes; string publicRSAKey = rsaManager.PublicKey; message = $"|rsaKeyStart|{publicRSAKey}|rsaKeyEnd|{message}"; messageBytes = tlsManager.EncryptMessage(message, out IV, out hashSum); hashSumCount = new byte[] { (byte)hashSum.Length }; digitalSignature = rsaManager.GetDigitalSignature(hashSum); return(IV.Concat(hashSumCount.Concat(hashSum.Concat(digitalSignature.Concat(messageBytes)))).ToArray()); }