/// <summary> /// Tworzy zaszyfrowane klucze do wysłania /// </summary> /// <param name="clientSign">Klucze klienta</param> //RSAParameters clientSign public void ExchangeKeysMsg() { byte[] toEncrypt; byte[] encrypted; byte[] signature; //string original = String.Concat(protocol.GetReceivedNumber()); string original = "hello"; //ASCIIEncoding myAscii = new ASCIIEncoding(); //signature = sign.HashSign(Encoding.UTF8.GetBytes(original)); toEncrypt = Encoding.UTF8.GetBytes(original); clientSign = sign.GetClientPublicKeys(); encrypted = sign.EncryptData(clientSign, toEncrypt); signature = sign.HashSign(encrypted); //sign.VerifyHash(clientSign, encrypted, signature).ToString(); signed = Tuple.Create(encrypted, signature); //return tuple; }
/// <summary> /// Wysyłanie wiadomości do socketa z pomocą opcji /// </summary> /// <param name="data">treść wiadomości</param> /// <param name="option">opcja wiadomości, możliwe opcje: /// 1 - wyślij wartość modulo /// 2 - wyślij wartość exponenty /// 3 - wyślij podpisaną wiadomość /// 4 - wyślij sygnaturę /// 5 - wyślij liczbę pierwszą /// 6 - wyślij pierwiastek pierwotny /// 7 - wyślij wyznaczoną wartość, wyliczoną za pomocą tajnej liczby własnej (g^t mod p) /// </param> public void SendMessage(string data, int option) { //ASCIIEncoding enc = new ASCIIEncoding(); string sending; string msgOption; byte[] endMsg = new byte[1024]; switch (option) { case 1: msgOption = "M"; sending = sign.ownPubKey.Item1; break; case 2: msgOption = "E"; sending = sign.ownPubKey.Item2; break; case 3: msgOption = "K"; //clientSign = sign.GetClientPublicKeys(); ExchangeKeysMsg(protocol.GetMsgToSign()); sending = Convert.ToBase64String(signed.Item1); break; case 4: msgOption = "S"; //ExchangeKeysMsg(sign.GetClientKeys()); sending = Convert.ToBase64String(signed.Item2); break; case 5: msgOption = "P"; sending = protocol.GetPrimeNumber().ToString(); break; case 6: msgOption = "R"; sending = protocol.GetPrimitiveRoot().ToString(); break; case 7: msgOption = "B"; //protocol.CreateNumberToSend(); sending = protocol.GetNumberToSend(); break; case 8: msgOption = "Q"; RSAParameters rsaPub = sign.GetClientPublicKeys(); byte[] encrypted = sign.EncryptData(rsaPub, Encoding.UTF8.GetBytes(sign.GetMsgToSign(1))); sending = Convert.ToBase64String(encrypted); break; case 9: msgOption = "T"; rsaPub = sign.GetClientPublicKeys(); int len = sign.GetMsgToSign(0).Length; halfMsg = sign.GetMsgToSign(0).Substring(0, len / 2); encrypted = sign.EncryptData(rsaPub, Encoding.UTF8.GetBytes(halfMsg)); sending = Convert.ToBase64String(encrypted); break; case 10: msgOption = "U"; rsaPub = sign.GetClientPublicKeys(); len = sign.GetMsgToSign(0).Length; halfMsg = sign.GetMsgToSign(0).Substring(len / 2, len / 2); encrypted = sign.EncryptData(rsaPub, Encoding.UTF8.GetBytes(halfMsg)); sending = Convert.ToBase64String(encrypted); break; case 11: msgOption = "V"; sending = aes.getAesIV(); break; case 12: msgOption = "W"; sending = aes.getAesKeys(); break; /*case 8: * msgOption = "Q"; //q * sending = sign.ownPrivKey.ElementAt(3); * break; * case 9: * msgOption = "T"; //p * sending = sign.ownPrivKey.ElementAt(2); * break; * case 10: * msgOption = "U"; //dp * sending = sign.ownPrivKey.ElementAt(4); * break; * case 11: * msgOption = "V"; //dq * sending = sign.ownPrivKey.ElementAt(5); * break; * case 12: * msgOption = "W"; //inverseq * sending = sign.ownPrivKey.ElementAt(6); * break; * case 13: * msgOption = "X"; //d * sending = sign.ownPrivKey.ElementAt(7); * break;*/ default: msgOption = "D"; sending = aes.addSalt(aes.DoAESEnc(data)); break; } endMsg = Encoding.UTF8.GetBytes(String.Concat(msgOption, sending)); clientSocket.Send(endMsg); }