Example #1
0
        public IActionResult ReceiveMessage(SendMessageModel messageModel)
        {
            string        filePublicKey = parameters.Value.FilesOutput + parameters.Value.PubKeyFile + messageModel.userNameDestination + messageModel.userNameOrigin;
            RSAEncryption rsaEncryption = new RSAEncryption();
            AESEncryption aesEncryption = new AESEncryption();
            RSASigning    rsaSigning    = new RSASigning();

            //Decrypt symmetric key
            ResponseRSADecryption rsaDecryptResponse = new ResponseRSADecryption();

            rsaDecryptResponse = rsaEncryption.DecryptAESKey(messageModel.encryptedKey, messageModel.userNameDestination + messageModel.userNameOrigin);
            if (!rsaDecryptResponse.result)
            {
                FileWriter.WriteOnEvents(EventLevel.Error, "Error descifrando llave AES con RSA.");
                return(BadRequest(new { result = false }));
            }
            //Decrypt Message
            ResponseAESDecryption responseAESDecryption = new ResponseAESDecryption();

            responseAESDecryption = aesEncryption.DecryptMessage(messageModel, rsaDecryptResponse.decryptedKey);
            if (!responseAESDecryption.result)
            {
                FileWriter.WriteOnEvents(EventLevel.Error, "Error descifrando mensaje con AES.");
                return(BadRequest(new { result = false }));
            }
            //Validate Sign
            if (!rsaSigning.validateSignAndHash(responseAESDecryption.decryptedMessage, messageModel.encryptSignature, filePublicKey))
            {
                FileWriter.WriteOnEvents(EventLevel.Atention, "La informaciĆ³n recibida es corrupta.");
                return(BadRequest(new { result = false }));
            }
            //Muestra mensaje
            return(Ok(new{ mensaje = responseAESDecryption.decryptedMessage }));
        }