//private void BtnDecrypt_Loaded(object sender, RoutedEventArgs e)
        //{
        //    if (String.IsNullOrEmpty(textBoxDecryptInputFile.Text) || String.IsNullOrEmpty(textBoxDecryptOutputFile.Text)
        //        || String.IsNullOrEmpty(PasswordDecrypt.Password) || ComboBoxRecipient.ItemsSource == null)
        //        DecryptBtn.IsEnabled = false;
        //    else
        //        DecryptBtn.IsEnabled = true;
        //}
        //private void BtnDecrypt_Loaded(object sender, SelectionChangedEventArgs e)
        //{
        //    var who = ComboBoxRecipient.SelectedItem as Recipient;

        //    if (who != null)
        //    {
        //        inputKeyFile = @".\prywatne\" + who.Name;
        //    }

        //}

        private byte[] RSADecrypt(byte[] toDecrypt)
        {
            AsymmetricKeyParameter keyPriv;
            var bytesToDecrypt = toDecrypt;

            var sec = new SecureRandom();

            PgpSecretKey pgpPriv;

            using (Stream sr = File.OpenRead(inputKeyFile))
            {
                pgpPriv = PGPKey.ImportSecretKey(sr);
            }

            var pass = PasswordDecrypt.Password.ToCharArray();

            try
            {
                keyPriv = pgpPriv.ExtractPrivateKey(pass).Key;
            }
            catch
            {
                keyPriv = null;
            }

            if (keyPriv == null)
            {
                byte[] seed = Encoding.UTF8.GetBytes(pass);
                var    sec2 = new SecureRandom(seed);

                var keyParam     = new KeyGenerationParameters(sec2, Int32.Parse(metadataDecrypt.KeySize));
                var keyGenerator = new CipherKeyGenerator();
                keyGenerator.Init(keyParam);
                return(keyGenerator.GenerateKey());
            }

            var decryptEngine = new Pkcs1Encoding(new RsaEngine());

            decryptEngine.Init(false, keyPriv);

            byte[] decrypted;

            try
            {
                decrypted = decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length);
            }
            catch
            {
                var keyParam     = new KeyGenerationParameters(sec, Int32.Parse(metadataDecrypt.KeySize));
                var keyGenerator = new CipherKeyGenerator();
                keyGenerator.Init(keyParam);
                return(keyGenerator.GenerateKey());
            }

            return(decrypted);
        }
예제 #2
0
        private AsymmetricKeyParameter readKey(string file)
        {
            AsymmetricKeyParameter key;
            PgpPublicKey           pgpPub;

            using (Stream sr = File.OpenRead(file))
            {
                pgpPub = PGPKey.ImportPublicKey(sr);
            }

            key = pgpPub.GetKey();

            return(key);
        }