Ejemplo n.º 1
0
        //File decrypteren
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            RSAWithRSAParameterKey rsaParams = new RSAWithRSAParameterKey();
            HybridEncryption       hybrid    = new HybridEncryption();

            //Session key en IV in string-variabelen zetten + omzetten naar byte-arrays
            string encryptedSessionKeyFile = Application.StartupPath + @"\Users\" + username + @"\Cryptodata\Encrypted Session Key";
            string ivFile = Application.StartupPath + @"\Users\" + username + @"\CryptoData\IV";

            byte[] encryptedSessionKey = null;
            byte[] iv = null;
            try
            {
                encryptedSessionKey = File.ReadAllBytes(encryptedSessionKeyFile);
                iv = File.ReadAllBytes(ivFile);

                //Dialoogvenster openen om een te decrypteren file te openen
                openFileDialog.InitialDirectory = myInbox;
                openFileDialog.Filter           = "Text|*.txt|All|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filenameToDecrypt = openFileDialog.FileName;
                    byte[] dataToDecrypt = File.ReadAllBytes(filenameToDecrypt);     //file omzetten naar byte-array
                    byte[] decryptedData = hybrid.DecryptData(encryptedSessionKey, dataToDecrypt, iv, rsaParams);

                    //Filename van gedecrypteerde boodschap maken
                    string myFolder          = Application.StartupPath + @"\Users\" + username + @"\Inbox\";
                    int    startIndex        = filenameToDecrypt.LastIndexOf(@"\") + 6; //+6 Zodat de naam begint vanaf de "From"
                    int    indexUnderscore   = filenameToDecrypt.LastIndexOf("_") + 1;
                    string decryptedFilename = myFolder + "Decr_" + filenameToDecrypt.Substring(startIndex);

                    //Gedecrypteerde data in bestand schrijven
                    File.WriteAllBytes(decryptedFilename, decryptedData);

                    MessageBox.Show("You have successfully decrypted the received file!", "Successfully decrypted!");
                }
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("You have no files to decrypt yet. You were able to click this button because you appear to have a hidden file in your inbox.", "Warning");
            }
            catch (System.Security.Cryptography.CryptographicException)
            {
                MessageBox.Show("You can not decrypt this file.", "Not possible to decrypt!");
                return;
            }
        }