Exemplo n.º 1
0
        private string CreateForwardMessage(Guid remoteId, string message)
        {
            byte[] iv = diffieHellman.GenerateIV();
            byte[] encryptedChatMessage = diffieHellman.Encrypt(commonSecret[remoteId], message, iv);
            string signature            = HMACFactory.CreateSignature(Convert.ToBase64String(encryptedChatMessage), commonSecret[remoteId]);

            return(MessageFactory.CreateForwardMessage(id, remoteId, iv, signature, encryptedChatMessage));
        }
Exemplo n.º 2
0
        public override IAlgorithmElement CreateAlgorithmElement()
        {
            IMac hmac = HMACFactory.CreateDigets(Name);

            switch (hmac.AlgorithmName)
            {
            default:
                return(new HMACControl(hmac));
            }
        }
Exemplo n.º 3
0
        private string HandleDataAsync(string data)
        {
            //TODO
            string[] dataArray = data.Split(';');

            if (dataArray[0] == "PUBLICKEYACCEPT")
            {
                byte[] serverPublicKey = Convert.FromBase64String(dataArray[PackageLocation.PUBLICKEYACCEPT.PUBLIC_KEY]);
                GetCommonSecretServer(serverPublicKey);
                return("---Established secure connection with server.");
            }

            string decryptedPackage = diffieHellman.Decrypt(commonSecretServer,
                                                            Convert.FromBase64String(dataArray[PackageLocation.SECURE_PACKAGE.ENCRYPTED_DATA]),
                                                            Convert.FromBase64String(dataArray[PackageLocation.SECURE_PACKAGE.IV]));

            string[] decryptedPackageArray = decryptedPackage.Split(';');
            Guid     senderId;
            Guid     remoteId;
            string   remoteUsername;

            byte[] receiverPublicKey;

            switch (decryptedPackageArray[0])
            {
            case "LOGINACCEPT":
                id = new Guid(decryptedPackageArray[PackageLocation.LOGINACCEPT.ASSIGNED_ID]);
                return("---Assigned ID: " + decryptedPackageArray[1]);

            case "NEWUSER":
                remoteId       = new Guid(decryptedPackageArray[PackageLocation.NEWUSER.REMOTE_ID]);
                remoteUsername = decryptedPackageArray[PackageLocation.NEWUSER.USERNAME];
                AddUser(remoteId, remoteUsername);
                return("---User " + remoteUsername + " has connected.");

            case "LOADUSER":
                remoteId       = new Guid(decryptedPackageArray[PackageLocation.NEWUSER.REMOTE_ID]);
                remoteUsername = decryptedPackageArray[PackageLocation.NEWUSER.USERNAME];
                AddUser(remoteId, remoteUsername);
                sendPublicKey(remoteId);
                return("---Loaded user " + remoteUsername);

            case "USERLEFT":
                remoteId       = new Guid(decryptedPackageArray[PackageLocation.NEWUSER.REMOTE_ID]);
                remoteUsername = onlineUsernames[remoteId];
                RemoveUser(remoteId);
                return("---User " + remoteUsername + " left.");

            case "FORWARDPUBLICKEYREQUEST":
                senderId          = new Guid(decryptedPackageArray[PackageLocation.FORWARDPUBLICKEYREQUEST.SENDER_ID]);
                receiverPublicKey = Convert.FromBase64String(decryptedPackageArray[PackageLocation.FORWARDPUBLICKEYREQUEST.PUBLIC_KEY]);
                GetCommonSecret(senderId, receiverPublicKey);
                respondToPublicKey(senderId);
                return("---Received key from " + onlineUsernames[senderId]);

            case "FORWARDPUBLICKEYACCEPT":
                senderId          = new Guid(decryptedPackageArray[PackageLocation.FORWARDPUBLICKEYACCEPT.SENDER_ID]);
                receiverPublicKey = Convert.FromBase64String(decryptedPackageArray[PackageLocation.FORWARDPUBLICKEYACCEPT.PUBLIC_KEY]);
                GetCommonSecret(senderId, receiverPublicKey);
                return("---Accepted key from " + onlineUsernames[senderId]);

            case "FORWARD":
                senderId = new Guid(decryptedPackageArray[PackageLocation.FORWARD.SENDER_ID]);
                string signature     = decryptedPackageArray[PackageLocation.FORWARD.SIGNATURE];
                string encryptedData = decryptedPackageArray[PackageLocation.FORWARD.ENCRYPTED_DATA];

                if (!HMACFactory.CheckSignature(encryptedData, commonSecret[senderId], signature))
                {
                    return("---NOTE: received message with invalid signature.");
                }

                string decryptedData = diffieHellman.Decrypt(commonSecret[senderId],
                                                             Convert.FromBase64String(encryptedData),
                                                             Convert.FromBase64String(decryptedPackageArray[PackageLocation.FORWARD.IV]));

                string[] decryptedDataArray = decryptedData.Split(';');
                switch (decryptedDataArray[0])
                {
                case "CHATMESSAGE":
                    return(FormatChatMessage(decryptedDataArray));

                case "FILE":
                    string fileName = decryptedDataArray[PackageLocation.FILE.FILE_NAME];
                    string userName = decryptedDataArray[PackageLocation.FILE.USERNAME];
                    byte[] rawFile  = Convert.FromBase64String(decryptedDataArray[PackageLocation.FILE.RAW_FILE]);
                    SaveFile(fileName, rawFile);
                    return("---Received file from " + userName + " and saved as " + fileName);
                }
                return("...");
            }
            return("...");
        }