예제 #1
0
        private void AddKey(object sender, RoutedEventArgs e)
        {
            AddKeyWindow requestWindow = sender as AddKeyWindow;

            //encrypt input key value before insert in db
            requestWindow.Key.KeyValue = KeyEncryptor.Encrypt(requestWindow.Key.KeyValue);

            fileService.AddKey(requestWindow.Key);
            requestWindow.Close();
        }
예제 #2
0
        private async void OnMessage(object sender, MessageEventArgs e)
        {
            Message message;

            string messageText = KeyEncryptor.Encrypt(Configuration["EncryptionKey"], e.Message.Text.ToString());

            try
            {
                string fileName = GoogleDrive.GetFileNameByKey(messageText);
                GoogleDrive.DownloadByKey(messageText);

                using (FileStream fileStream = System.IO.File.Open(Configuration["DownloadPath"] + "/" + fileName, FileMode.Open))
                {
                    message = await BotClient.SendDocumentAsync(
                        chatId : e.Message.Chat,
                        document : new InputOnlineFile(fileStream, fileName)
                        );
                }

                _logger.LogInformation(
                    $"{message.From.FirstName} sent file {fileName} " +
                    $"to user {e.Message.From.Username} at {message.Date}. "
                    );
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(
                    $"File was not sent to user {e.Message.From.Username} with text '{e.Message.Text}' " +
                    $"because of exception: {ex.Message}."
                    );

                await BotClient.SendTextMessageAsync(
                    chatId : e.Message.Chat,
                    text : ex.Message
                    );
            }
            catch (Exception ex)
            {
                _logger.LogError($"{DateTime.Now} : {ex.Message}");
            }
        }
예제 #3
0
        /// <summary>
        /// Encrypt the given password with strong encryption
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string[] EncryptPassword(string password)
        {
            string EncryptedPassword = "";
            string InitVector        = "";

            string[] ReturnArray = new string[2];

            try
            {
                // Instantiate the Encryptor
                KeyEncryptor Enc      = new KeyEncryptor(this.Algorithm);
                byte[]       Password = Encoding.ASCII.GetBytes(password);

                // Get the password key
                string StoredKey = this.GetPasswordKey();
                byte[] Key       = Convert.FromBase64String(StoredKey);

                // Set the Initialization vector property to null so a brand new one will get generated
                Enc.IV = null;

                // Encrypt the Password and Convert the resulting array of bytes to a Base64 string
                byte[] CipherText = Enc.Encrypt(Password, Key);

                // Retrieve the intialization vector needed for decryption.
                byte[] IV = Enc.IV;

                // Update table with new values
                EncryptedPassword = Convert.ToBase64String(CipherText);
                InitVector        = Convert.ToBase64String(IV);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Encrypting Password. " + ex.Message);
            }

            ReturnArray[0] = EncryptedPassword;
            ReturnArray[1] = InitVector;
            return(ReturnArray);
        }