コード例 #1
0
        protected override Message ReceiveAuthenticatedMessage(AuthenticatedMessage message)
        {
            if (message is Bytes)
            {
                var bytesMessage       = message as Bytes;
                var bitsAsBytesPayload = bytesMessage.Payload as BitsAsBytes;

                if (bytesMessage.Demand == Demand.Estimation)
                {
                    var bobBits       = new BitArray(bitsAsBytesPayload.GetBytes().ToList().Skip(8).ToArray());
                    var aliceBits     = new BitArray(DestilationBuffer.GetBytes(bitsAsBytesPayload.Index, bitsAsBytesPayload.Length));
                    var estimatedQBER = Cascade.EstimateQBER(aliceBits, bobBits);
                    DestilationBuffer.SetEstimatedQBER(estimatedQBER);
                }
                if (bytesMessage.Demand == Demand.Confirmation)
                {
                }
            }
            if (message is AddingKeyAck)
            {
                var addingKeyAckMessage = message as AddingKeyAck;

                if (DoesAckCorrespondsToLastSentKeyIndex(addingKeyAckMessage.Payload as BlockIdAndKeyAllocation))
                {
                    _lastReceivedKeyIndexAck = _lastSendKeyIndex;

                    var key = LocalKeyStore.GetKey();
                    CommonKeyStore.AddNewKey(key, (addingKeyAckMessage.Payload as BlockIdAndKeyAllocation).KeyAllocation);

                    if (!LocalKeyStore.IsEmpty())
                    {
                        _lastSendKeyIndex += 1;
                        return(PrepareMessageForNewKeyChunk(_lastSendKeyIndex));
                    }
                    else
                    {
                        return(new NOP());
                    }
                }
                else
                {
                    throw new UnexpectedMessageException();
                }
            }
            return(new NOP());
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //INICJALIZACJA SYSTEMU
            var alreadyAgreedKeys = GetKeyStream(32768, 2);
            var initiator         = GetInitiator(alreadyAgreedKeys);
            var service           = GetService(CopyKeys(alreadyAgreedKeys));
            var bus = new ClientServerMessageBus()
            {
                Initiator = initiator, Service = service
            };
            var keyLength  = 32768;
            var aliceBits  = GetBits(keyLength);
            var bobBits    = Distortion(aliceBits, 0.05);
            var aliceBytes = new byte[aliceBits.Length / 4];
            var bobBytes   = new byte[aliceBits.Length / 4];

            new BitArray(aliceBits).CopyTo(aliceBytes, 0);
            new BitArray(bobBits).CopyTo(bobBytes, 0);

            bus.Send(initiator, new MoreUndestiledBitsArrived(aliceBytes));
            bus.Send(service, new MoreUndestiledBitsArrived(bobBytes));

            //INICJACJA PROCEDURY DESTYLACJI

            bus.Send(initiator, new InitiateDestilation());

            //FAZA ESTYMACJI BŁĘDÓW

            bus.Send(initiator, new EstimateQBER(0, 4048 / 2 / 2 / 2));

            //FAZA KOREKCJI BŁĘDÓW (3 iteracje)

            for (int i = 0; i < 3; i++)
            {
                bus.Send(initiator, new InitiateCascade()
                {
                    Service = service, Bus = bus
                });

                initiator.DestilationBuffer.Permute(initiator.CommonKeyStore.ServiceKeyBuffer.GetKey().GetBytes());
                service.DestilationBuffer.Permute(service.CommonKeyStore.ServiceKeyBuffer.GetKey().GetBytes());
                initiator.DestilationBuffer.SetEstimatedQBER(initiator.DestilationBuffer.GetEstimatedQBER() / 2);
            }

            //FAZA POTWIERDZENIA

            bus.Send(initiator, new EstimateQBER(0, 256));
            if (Cascade.EstimateQBER(initiator.DestilationBuffer._bits, service.DestilationBuffer._bits) != 0)
            {
                throw new ErrorCorrectionFailException();
            }

            //FAZA AMPLIFIKACJA PRYWATNOŚCI

            var privacyLevel = 0.1;

            bus.Send(initiator, new AmplifiePrivacy()
            {
                PrivacyLevel = privacyLevel
            });
            bus.Send(service, new AmplifiePrivacy()
            {
                PrivacyLevel = privacyLevel
            });

            //DODANIE KLUCZA DO SYSTEMU
            var klucz = initiator.DestilationBuffer.FlushBuffer();

            bus.Send(initiator, new MoreBitsArrived(new List <Key>()
            {
                new Key(klucz)
            }));
            bus.Send(service, new MoreBitsArrived(new List <Key>()
            {
                new Key(service.DestilationBuffer.FlushBuffer())
            }));
        }