예제 #1
0
        /// <exception cref="Exception"></exception>
        /// <param name="message"></param>
        /// <param name="privateKey">Must be base64 string</param>
        /// <returns></returns>
        public DecryptedMessageDto Decrypt(EncryptedMessage message, string privateKey, bool privateKeyIsReciever)
        {
            var recieverPrivateKey = Convert.FromBase64String(privateKey);

            using (var rsa = new RSACryptoServiceProvider(KeysConfiguration.RSAKeysBits))
            {
                rsa.ImportCspBlob(recieverPrivateKey);
                rsa.PersistKeyInCsp = false;

                var aesKey = rsa.Decrypt(Convert.FromBase64String(privateKeyIsReciever ? message.ToKey : message.FromKey), true);

                using (var aes = new AesCryptoServiceProvider())
                {
                    aes.Padding = PaddingMode.PKCS7;
                    aes.Mode    = CipherMode.CBC;

                    aes.Key = aesKey;
                    aes.IV  = Convert.FromBase64String(message.IV);
                    var aesDecryptor = aes.CreateDecryptor();

                    DecryptedMessageDto messageDto = new DecryptedMessageDto();
                    messageDto.Id        = message.Id;
                    messageDto.To        = message.To;
                    messageDto.From      = message.From;
                    messageDto.Timestamp = message.Timestamp;
                    messageDto.Title     = DecryptData(aesDecryptor, message.Title);
                    messageDto.Content   = DecryptData(aesDecryptor, message.Content);
                    return(messageDto);
                }
            }
        }
예제 #2
0
 public bool Validate(EncryptedMessage message)
 {
     foreach (var validator in _validators)
     {
         if (!validator.Validate(message))
         {
             return(false);
         }
     }
     return(true);
 }
예제 #3
0
        public async Task <bool> TryAddAsync(EncryptedMessage message)
        {
            var id = message.Id;

            if (!_messageValidator.Validate(message))
            {
                return(false);
            }
            if (GetMessageById(message.Id) != null)
            {
                return(false);
            }

            try
            {
                var messageDb = new EncryptedMessageStore(message);
                _applicationDatabase.Messages.Add(messageDb);
                return(await _applicationDatabase.SaveChangesAsync() > 0);
            }
            catch (DbException)
            {
                return(false);
            }
        }
 public EncryptedMessageBuilder(IHashCash hashCash)
 {
     _message  = new EncryptedMessage();
     _hashCash = hashCash;
 }
예제 #5
0
 public bool TryGet(string id, out EncryptedMessage message)
 {
     message = GetMessageById(id);
     return(message != null ? true : false);
 }