Beispiel #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 }));
        }
Beispiel #2
0
        internal ResponseRSADecryption DecryptAESKey(byte[] encryptedKey, string containerName)
        {
            ResponseRSADecryption response = new ResponseRSADecryption {
                result = false
            };

            byte[] decryptedKey;

            CspParameters cspParameters = new CspParameters {
                Flags            = CspProviderFlags.UseExistingKey,
                KeyContainerName = "OwnkeyEncrypts" + containerName
            };

            try
            {
                RSACryptoServiceProvider rsa        = new RSACryptoServiceProvider(FileWriter.parameters.Value.KeyRSASize, cspParameters);
                RSAParameters            publicKey  = rsa.ExportParameters(false);
                RSAParameters            privateKey = rsa.ExportParameters(true);
                FileWriter.WriteOnEvents(EventLevel.Info, "Inicio proceso de descifrado de llave AES.");
                decryptedKey          = rsa.Decrypt(encryptedKey, true);
                response.decryptedKey = decryptedKey;
                FileWriter.WriteOnEvents(EventLevel.Info, "Proceso de descifrado de llave AES finalizada correctamente");
                response.result = true;
            }
            catch (System.Exception ex)
            {
                FileWriter.WriteOnEvents(EventLevel.Exception, "Error descifrando llave aes" + ex.Message);
            }
            return(response);
        }