public void EncryptFile(IFormFile fileform, EncryptedMessage encryptedMessage, RSAEncryption rsaEncryption, DigitalSignature signature) { var sessionKey = _aes.GenerateRandomNumber(32); var encryptedPacket = new EncryptedFile { Iv = _aes.GenerateRandomNumber(16), SenderEmail = encryptedMessage.SenderEmail, ReceiverEmail = encryptedMessage.ReceiverEmail, EncryptedMessageId = encryptedMessage.EncryptedMessageId, FileName = fileform.FileName, }; byte[] fileInBytes; using (var stream = new MemoryStream()) { fileform.CopyToAsync(stream); fileInBytes = stream.ToArray(); } encryptedPacket.EncryptedData = _aes.Encrypt(fileInBytes, sessionKey, encryptedPacket.Iv); encryptedPacket.EncryptedSessionKey = rsaEncryption.EncryptData(sessionKey); using (var hmac = new HMACSHA256(sessionKey)) { encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData); } encryptedPacket.Signature = signature.SignData(encryptedPacket.Hmac); _dbContext.EncryptedFiles.Add(encryptedPacket); _dbContext.SaveChanges(); }
public EncryptedMessage EncryptData(Message email, RSAEncryption rsaEncryption, DigitalSignature signature) { byte[] message = Encoding.ASCII.GetBytes(email.Text); var sessionKey = _aes.GenerateRandomNumber(32); var encryptedPacket = new EncryptedMessage { ReceiveDate = DateTime.Now, Iv = _aes.GenerateRandomNumber(16), SenderEmail = email.EmailSender, ReceiverEmail = email.EmailReceiver }; encryptedPacket.EncryptedData = _aes.Encrypt(message, sessionKey, encryptedPacket.Iv); encryptedPacket.EncryptedSessionKey = rsaEncryption.EncryptData(sessionKey); using (var hmac = new HMACSHA256(sessionKey)) { encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData); } encryptedPacket.Signature = signature.SignData(encryptedPacket.Hmac); _dbContext.EncryptedMessages.Add(encryptedPacket); _dbContext.SaveChanges(); return(encryptedPacket); }