Exemplo n.º 1
0
        protected override void EstablishConnection(NetworkMessage message)
        {
            //Generate symmetric key + IV, save it and encrypt it with the given public assymetric key and send it to the client.
            try
            {
                if (message.TryGetObject <string>(out string publicKey))
                {
                    AsymmetricCipher asymmetric = new AsymmetricCipher();
                    asymmetric.LoadPublicKey(publicKey);
                    cipher = new SymmetricCipher();

                    SymmetricKey key = new SymmetricKey {
                        Key = asymmetric.Encrypt(cipher.Key), IV = asymmetric.Encrypt(cipher.IV)
                    };

                    Send(MessageProtocols.Setup, key, false);
                    Send(MessageProtocols.SetUsername, Username);
                    server.Broadcast(MessageProtocols.Connect, this, Username); // Tell users that it connected
                    Send(MessageProtocols.Users, server.ConnectedUsers().ToArray());
                }
                else
                {
                    throw new Exception("Failed to load the public key");
                }
            }
            catch (Exception)
            {
                Terminate();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ends handshake by sending the symmetric iv and key to the remote peer
        /// </summary>
        private void EndHandshake(byte[] data)
        {
            AsymmetricCipher.PublicKey = data;
            Connection.SendData(AsymmetricCipher.Encrypt(SymmetricCipher.IV));
            Connection.SendData(AsymmetricCipher.Encrypt(SymmetricCipher.Key));
            handshakeDone = true;

            OnConnected?.Invoke();
        }
Exemplo n.º 3
0
        public static void TestAsymmetricEncrypt2()
        {
            var data = "非对称加密测试2";

            Console.WriteLine(data);

            var publicKey =
                "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjoHfRKSPSURhRYJu9UtgkvP2TFjOI4fHbXUmsBgIU1TiNugP/JepY5lMZ6ISF3zg0QO5DTJ09god76BZVFKnKAsWA4Dqcv3bNVUneSZygtsB/SCzjUQ9o8bZkiCd5vaAOER/Z6g75jtl8XGtjE7GtQ/ezd37JQR7xZ2axZrWtdQIDAQAB";
            var privateKey =
                "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKOgd9EpI9JRGFFgm71S2CS8/ZMWM4jh8dtdSawGAhTVOI26A/8l6ljmUxnohIXfODRA7kNMnT2Ch3voFlUUqcoCxYDgOpy/ds1VSd5JnKC2wH9ILONRD2jxtmSIJ3m9oA4RH9nqDvmO2Xxca2MTsa1D97N3fslBHvFnZrFmta11AgMBAAECgYBGaYVmApghpzgZvMMII6BTnuhX5VPj8acMSQas+iDnKiIeCxAxOfWwr9zO51ov6bDb+50MZOm9UHBRB7ykfDHbxkFXCLA3Tesr2/X3+0Iyz30pu1ctCOcjlJaHQpnE09okYI9kZpkTzzK3Hkm59wE/9SOor5Ag70hrXcAApwIceQJBANKXscBScD6nySdmbxBjfPc+UwSEFIwb+XYey3hJHNY9rKuA4opRTTWMB9JaFfgeXeRP8y5Fd+B8Y1R6n9z5KKcCQQDG6FsBtEmj6mVL/ZPr2JeUAo1Z7VH9uB6bG5o2sTeeERAz3GSqZd+K7s/LSjbJ9XbIuGHoFrE4wT+uIfwD9yCDAkAyUVKEXG47WkXC50PES7ExNjAJ1TE/pPN/GK6PKBD+06+tLtdyKyjikXnQ9ftn1IGkqsG1HZ4eAjqNldsapmHjAkApMEZgJPg21Dvjr3/pD7HbuWeR3p3i3zSfQ+j8OFhfCAOF6baCvpO6zlcDLrwHuCe/ysaja8eJDCNmqKzqGUuHAkBlTjExgc35J2TY1pn4FE1pqj8Yavll4vfiDhIrRwLdVJyKq8Sjm4OzVFAkk3rMkqPR/ZL3cxb45RMmtGkYwVQ9";

            var cipher = new AsymmetricCipher(AsymmetricAlgorithm.Rsa, publicKey, privateKey);

            var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes("非对称加密测试2"));

            Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}");

            // encrypted = Convert.FromBase64String("js3wD7eeMtEVyZvO0HrvlRU5esVrmXOyW21c+woVC5r9no1xm3KT1IBMcrGPWj8YL1NIjdBW+Qfq81bCRC1Oqsxou/5IZOIV4JFSnY0k1yxF+8TH9IWXsfU/nrFyQagQrJZ3gGG7NygIM6dxK8StsCLTVNmJX8vxvwPNqITql1A=");

            var decrypted = cipher.Decrypt(encrypted);

            Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}");
        }
Exemplo n.º 4
0
        public static void TestAsymmetricEncrypt1()
        {
            var data = "非对称加密测试1";

            Console.WriteLine(data);

            var cipher = new AsymmetricCipher(AsymmetricAlgorithm.Rsa);

            Console.WriteLine("=====================publicKey================");
            Console.WriteLine(cipher.Base64PublicKey);
            Console.WriteLine();

            Console.WriteLine("=====================privateKey================");
            Console.WriteLine(cipher.Base64PrivateKey);
            Console.WriteLine();

            var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes(data));

            Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}");

            var decrypted = cipher.Decrypt(encrypted);

            Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}");
        }