DecryptKeyExchange() публичный Метод

Extracts secret information from the key exchange data.
public DecryptKeyExchange ( byte keyEx ) : byte[]
keyEx byte The key exchange data within which the shared key is hidden.
Результат byte[]
Пример #1
0
    public static void Main(string[] args)
    {
        ECDiffieHellmanCng alice = new ECDiffieHellmanCng();
        //alice.DeriveKeyMaterial(
        //CngKey.Import(

        // create a new DH instance
        DiffieHellman dh1 = new DiffieHellmanManaged();
        // export the public parameters of the first DH instance
        DHParameters dhp = dh1.ExportParameters(false);
        // create a second DH instance and initialize it with the public parameters of the first instance
        DiffieHellman dh2 = new DiffieHellmanManaged(dhp.P, dhp.G, 160);
        // generate the public key of the first DH instance
        byte[] ke1 = dh1.CreateKeyExchange();
        // generate the public key of the second DH instance
        byte[] ke2 = dh2.CreateKeyExchange();
        // let the first DH instance compute the shared secret using the second DH public key
        byte[] dh1k = dh1.DecryptKeyExchange(ke2);
        // let the second DH instance compute the shared secret using the first DH public key
        byte[] dh2k = dh2.DecryptKeyExchange(ke1);
        // print both shared secrets to verify they are the same
        Console.WriteLine("Computed secret of instance 1:");
        PrintBytes(dh1k);
        Console.WriteLine("\r\nComputed secret of instance 2:");
        PrintBytes(dh2k);

        Console.WriteLine("\r\nPress ENTER to continue...");
        Console.ReadLine();
    }
		private static string Test1() {
			DiffieHellman dh1 = new DiffieHellmanManaged();
			DiffieHellman dh2 = new DiffieHellmanManaged();

			string secret1 = Convert.ToBase64String(dh1.DecryptKeyExchange(dh2.CreateKeyExchange()));
			string secret2 = Convert.ToBase64String(dh2.DecryptKeyExchange(dh1.CreateKeyExchange()));

			Assert.AreEqual(secret1, secret2, "Secret keys do not match for some reason.");

			return secret1;
		}
Пример #3
0
        internal void Add(ITransport transport, TransportCallback connectCallback)
        {
            try {
                // XXX: This should be negotiated as part of the initial handshake.
                transport.Encryptor = new AESTransportEncryptor();

                transports.Add (transport);

                if (NewTransportAdded != null)
                    NewTransportAdded (transport);

                LoggingService.LogInfo(String.Format ("Transport {0} added", transport.ToString()));

                if (transport.Incoming == true) {
                    if (connectCallback != null)
                        throw new ArgumentException ("You can only specify a ConnectCallback for outoging connections!");

                    if (transport.Encryptor != null) {
                        DiffieHellmanManaged dh = new DiffieHellmanManaged ();

                        byte[] keyxBytes = new byte[transport.Encryptor.KeyExchangeLength];
                        transport.Receive (keyxBytes, 0, keyxBytes.Length);
                        keyxBytes = dh.DecryptKeyExchange (keyxBytes);

                        byte[] keyBytes = new byte[transport.Encryptor.KeySize];
                        byte[] ivBytes = new byte[transport.Encryptor.IvSize];
                        Array.Copy (keyxBytes, 0, keyBytes, 0, keyBytes.Length);
                        Array.Copy (keyxBytes, keyBytes.Length, ivBytes, 0, ivBytes.Length);

                        keyxBytes = dh.CreateKeyExchange ();
                        transport.Send (keyxBytes, 0, keyxBytes.Length);

                        transport.Encryptor.SetKey(keyBytes, ivBytes);
                    }

                    //Receive connection type, which is a ulong (8 bytes)
                    byte[] responseBuffer = new byte[8];
                        transport.Receive (responseBuffer, 0, 8);
                    ulong connectionType = EndianBitConverter.ToUInt64 (responseBuffer, 0);

                    // Recieve network ID (64 bytes)
                    responseBuffer = new byte[64];
                    transport.Receive (responseBuffer, 0, 64);
                    string networkId = EndianBitConverter.ToString (responseBuffer).Replace ("-", "");

                    // Match to one of our known networks!
                    foreach (Network network in Core.Networks) {
                        if (network.NetworkID == networkId) {
                            transport.Network = network;
                        }
                    }

                    if (transport.Network == null) {
                        throw new Exception (String.Format ("Unknown network: {0}.", networkId));
                    }

                    transport.ConnectionType = connectionType;

                    if (connectionType == ConnectionType.NodeConnection) {
                        LocalNodeConnection connection = new LocalNodeConnection(transport);
                        transport.Operation = connection;
                        transport.Network.AddConnection(connection);
                        connection.Start();
                    } else if (connectionType == ConnectionType.TransferConnection) {

                        Core.FileTransferManager.NewIncomingConnection(transport);

                    } else {
                        throw new Exception(String.Format("Unknown connection type: {0}.",
                                                          connectionType.ToString()));
                    }

                } else {
                    if (connectCallback == null) {
                        throw new ArgumentNullException("connectCallback");
                    }

                    connectCallbacks.Add (transport, connectCallback);

                    LoggingService.LogInfo("Transport {0} connecting...", transport);

                    TransportCallback callback = new TransportCallback (OnConnected);
                    transport.Connect (callback);
                }
            } catch (Exception ex) {
                transport.Disconnect (ex);
                RaiseTransportError(transport, ex);
            }
        }
Пример #4
0
        private void OnConnected(ITransport transport)
        {
            try {
                LoggingService.LogInfo("Transport {0} connected.", transport);

                if (transport.Encryptor != null) {
                    DiffieHellmanManaged dh = new DiffieHellmanManaged ();

                    byte[] keyxBytes = dh.CreateKeyExchange ();
                    transport.Send (dh.CreateKeyExchange (), 0, keyxBytes.Length);

                    keyxBytes = new byte [transport.Encryptor.KeyExchangeLength];
                    transport.Receive (keyxBytes, 0, transport.Encryptor.KeyExchangeLength);

                    keyxBytes = dh.DecryptKeyExchange (keyxBytes);

                    byte[] keyBytes = new byte[transport.Encryptor.KeySize];
                    byte[] ivBytes = new byte[transport.Encryptor.IvSize];
                    Array.Copy (keyxBytes, 0, keyBytes, 0, keyBytes.Length);
                    Array.Copy (keyxBytes, keyBytes.Length, ivBytes, 0, ivBytes.Length);

                    transport.Encryptor.SetKey(keyBytes, ivBytes);
                }

                byte[] connectionType = EndianBitConverter.GetBytes (transport.ConnectionType);
                transport.Send (connectionType, 0, connectionType.Length);

                byte[] networkId = Common.SHA512 (transport.Network.NetworkName);
                transport.Send (networkId, 0, networkId.Length);

                // Ready, Steady, GO!

                TransportCallback callback = (TransportCallback) connectCallbacks [transport];
                connectCallbacks.Remove (transport);
                callback (transport);

            } catch (Exception ex) {
                transport.Disconnect (ex);
                RaiseTransportError(transport, ex);
            }
        }
Пример #5
0
 public static byte[] ComputeSharedKey(DHPrivateKey privateKey, DHPublicKey publicKey)
 {
     DiffieHellman dh = new DiffieHellmanManaged(privateKey.P, privateKey.G, privateKey.X);
     return dh.DecryptKeyExchange(publicKey.KeyExchangeData);
 }