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 })); }
internal ResponseAESDecryption DecryptMessage(SendMessageModel messageModel, byte[] decryptedKey) { ResponseAESDecryption response = new ResponseAESDecryption { result = false }; // Create an AesCryptoServiceProvider object with the specified key and IV. using (AesCryptoServiceProvider aesDecrypt = new AesCryptoServiceProvider()){ FileWriter.WriteOnEvents(EventLevel.Info, "Inicio proceso de descifrado de mensaje."); aesDecrypt.Key = decryptedKey; aesDecrypt.IV = messageModel.initVector; // Create a decryptor to perform the stream transform. ICryptoTransform decryptor = aesDecrypt.CreateDecryptor(aesDecrypt.Key, aesDecrypt.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(messageModel.encryptedMessage)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream and place them in a string. response.decryptedMessage = srDecrypt.ReadToEnd(); response.result = true; FileWriter.WriteOnEvents(EventLevel.Info, "Proceso de descifrado de mensaje finalizado correctamente"); } } } } return(response); }