Example #1
0
        private void button_decrypt_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Decrypt button pressed.");

            if (this.Model.CryptoKeyInfoFilePath.Length == 0)
            {
                var result = MessageBox.Show("A crypto info file path is needed.");
                return;
            }

            if (this.Model.CryptoDataInput.Length == 0)
            {
                var result = MessageBox.Show("There is no input data.");
                return;
            }

            var fileInfo = new FileInfo(this.Model.CryptoKeyInfoFilePath);
            if (!File.Exists(fileInfo.FullName))
            {
                var result = MessageBox.Show("The specified key info file does not exist.");
                return;
            }
            var key = File.ReadAllText(fileInfo.FullName);


            this.Model.CryptoDataOutput = AsymCryptoHelper.DecryptToString(this.Model.CryptoDataInput, key);
        }
Example #2
0
        private void button_encrypt_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Encrypt button pressed.");
            
            if (this.Model.CryptoDataInput.Length == 0)
            {
                var result = MessageBox.Show("There is no input data.");
                return;
            }

            var fileInfo = new FileInfo(this.Model.CryptoKeyInfoFilePath);
            if (!Directory.Exists(fileInfo.Directory.FullName))
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
            }
            
            var key = "";
            if (!File.Exists(fileInfo.FullName))
            {
                var crytoServiceProvider = new RSACryptoServiceProvider(this.Model.CryptoKeySize);
                key = crytoServiceProvider.ToXmlString(true);
                File.WriteAllText(fileInfo.FullName, key);
            }
            else
            {
                key = File.ReadAllText(fileInfo.FullName);
            }

            this.Model.CryptoDataOutput = AsymCryptoHelper.EncryptToBase64String(this.Model.CryptoDataInput, key);
        }