Ejemplo n.º 1
0
        private void Decrypt_Click(object sender, EventArgs e)
        {
            decryptMessageLabel.Show();
            this.decryptMessageLabel.ForeColor = Color.Orange;
            this.decryptMessageLabel.Text      = "Decryption in progress";

            if (outputFilenameTB.Text.Equals(string.Empty))
            {
                this.decryptMessageLabel.ForeColor = Color.Red;
                this.decryptMessageLabel.Text      = "Decryption failed";
                progressBar.Hide();
                MessageBox.Show("No output filename was chosen");
                return;
            }
            if (fileTB.Text.Equals(string.Empty))
            {
                this.decryptMessageLabel.ForeColor = Color.Red;
                this.decryptMessageLabel.Text      = "Decryption failed";
                progressBar.Hide();
                MessageBox.Show("No input file was chosen");
                return;
            }
            if (approvedUsersCB.Text.Equals(string.Empty))
            {
                this.decryptMessageLabel.ForeColor = Color.Red;
                this.decryptMessageLabel.Text      = "Decryption failed";
                progressBar.Hide();
                MessageBox.Show("No user was chosen");
                return;
            }
            if (userPassTB.Text.Equals(string.Empty))
            {
                this.decryptMessageLabel.ForeColor = Color.Red;
                this.decryptMessageLabel.Text      = "Decryption failed";
                progressBar.Hide();
                MessageBox.Show("Incorrect password");
                return;
            }

            var encryptedFileContent = File.ReadAllText(fileTB.Text);
            var XMLStringMetadata    = encryptedFileContent.Split(new[] { "</EncryptedFileHeader>" }, StringSplitOptions.None)[0] + "</EncryptedFileHeader>";
            var encyptedData         = encryptedFileContent.Split(new[] { "</EncryptedFileHeader>" }, StringSplitOptions.None)[1];
            var doc = new XmlDocument();

            doc.LoadXml(XMLStringMetadata);
            XmlNodeList approvedUserNodes = doc.SelectNodes("/EncryptedFileHeader/ApprovedUsers/User");
            var         decryptionMode    = doc.SelectSingleNode("/EncryptedFileHeader/CipherMode").InnerText;
            var         fileIV            = doc.SelectSingleNode("/EncryptedFileHeader/IV").InnerText;

            CipherMode mode       = 0;
            var        cipherMode = decryptionMode;

            if (cipherMode == "ECB")
            {
                mode = CipherMode.ECB;
            }
            else if (cipherMode == "CBC")
            {
                mode = CipherMode.CBC;
            }
            else if (cipherMode == "CFB")
            {
                mode = CipherMode.CFB;
            }

            this.decryptMessageLabel.ForeColor = Color.Red;
            this.decryptMessageLabel.Text      = "Decryption failed"; //this message is needed, when the user choose receiver that is not in the header of the file

            foreach (XmlNode node in approvedUserNodes)
            {
                if (approvedUsersCB.Text == node.SelectSingleNode("Email").InnerText)
                {
                    progressBar.Show();
                    if (backgroundWorker1.IsBusy != true)
                    {
                        // Start the asynchronous operation.
                        backgroundWorker1.RunWorkerAsync();
                    }

                    var    encryptedSessionKey = node.SelectSingleNode("SessionKey").InnerText;
                    string encryptedPrivKey, userIV;
                    using (StreamReader sr = new StreamReader(@"..\..\users\privateKeys\" + approvedUsersCB.Text + ".txt"))
                    {
                        encryptedPrivKey = sr.ReadToEnd();
                    }
                    using (StreamReader sr = new StreamReader(@"..\..\users\vectorsIV\" + approvedUsersCB.Text + ".txt"))
                    {
                        userIV = sr.ReadToEnd();
                    }

                    var serviceAESDecr = new ServiceAES(CipherMode.ECB, userPassTB.Text, true);
                    var privKey        = serviceAESDecr.Decrypt(encryptedPrivKey, Convert.FromBase64String(userIV));
                    var serviceRSADecr = new ServiceRSA(privKey, true);
                    var sessionKey     = serviceRSADecr.DecryptSessionKey(encryptedSessionKey);

                    var extensionOfFile = Path.GetExtension(doc.SelectSingleNode("/EncryptedFileHeader/Extension").InnerText);
                    var dirToSave       = fileTB.Text.Substring(0, fileTB.Text.LastIndexOf("\\") + 1);
                    var outputFileName  = dirToSave + outputFilenameTB.Text + extensionOfFile;

                    File.WriteAllText(Path.GetFullPath(outputFileName), // START DECRYPTING
                                      ServiceRijndaelAES.DecryptStringFromBytes(
                                          Convert.FromBase64String(encyptedData),
                                          Convert.FromBase64String(sessionKey),
                                          Convert.FromBase64String(fileIV), mode));

                    this.decryptMessageLabel.ForeColor = Color.Green;
                    this.decryptMessageLabel.Text      = "Decryption suceeded";
                }
            }
        }
Ejemplo n.º 2
0
        private void Encrypt_Click(object sender, EventArgs e)
        {
            encryptMessageLabel.Show();
            this.encryptMessageLabel.ForeColor = Color.Orange;
            this.encryptMessageLabel.Text      = "Encryption in progress";

            if (encryptionModeCB.SelectedItem == null)
            {
                this.encryptMessageLabel.ForeColor = Color.Red;
                this.encryptMessageLabel.Text      = "Encryption failed";
                encryptPB.Hide();
                MessageBox.Show("No encryption mode was chosen");
                return;
            }
            if (outputFilenameTB.Text.Equals(string.Empty))
            {
                this.encryptMessageLabel.ForeColor = Color.Red;
                this.encryptMessageLabel.Text      = "Encryption failed";
                encryptPB.Hide();
                MessageBox.Show("No output filename was chosen");
                return;
            }
            if (fileTB.Text.Equals(string.Empty))
            {
                this.encryptMessageLabel.ForeColor = Color.Red;
                this.encryptMessageLabel.Text      = "Encryption failed";
                encryptPB.Hide();
                MessageBox.Show("No input file was chosen");
                return;
            }
            if (approvedUsersLB.CheckedItems.Count <= 0)
            {
                this.encryptMessageLabel.ForeColor = Color.Red;
                this.encryptMessageLabel.Text      = "Encryption failed";
                encryptPB.Hide();
                MessageBox.Show("No receiver/receivers was/were chosen");
                return;
            }

            var users = new List <User>();

            byte[] fileIV;
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.GenerateIV();
                fileIV = rijAlg.IV;
            }

            var keySize    = 192;
            var blockSize  = 128;
            var sessionKey = GetSessionKey(keySize / 8); // length of key must be keySize/8

            foreach (var username in approvedUsersLB.CheckedItems)
            {
                string pubKey, privKey;
                using (StreamReader sr = new StreamReader(@"..\..\users\publicKeys\" + username + ".txt"))
                {
                    pubKey = sr.ReadToEnd();
                }
                using (StreamReader sr = new StreamReader(@"..\..\users\privateKeys\" + username + ".txt"))
                {
                    privKey = sr.ReadToEnd();
                }
                var serviceRSA          = new ServiceRSA(pubKey);
                var encryptedSessionKey = serviceRSA.EncryptSessionKey(sessionKey); // Encrypting session key of the user by public key of the same user
                users.Add(new User(username.ToString(), encryptedSessionKey));
            }

            CipherMode mode       = 0;
            var        cipherMode = encryptionModeCB.SelectedItem.ToString();

            if (cipherMode == "ECB")
            {
                mode = CipherMode.ECB;
            }
            else if (cipherMode == "CBC")
            {
                mode = CipherMode.CBC;
            }
            else if (cipherMode == "OFB")
            {
                this.encryptMessageLabel.ForeColor = Color.Red;
                this.encryptMessageLabel.Text      = "Encryption failed";
                MessageBox.Show("OFB isnt supported in .NET");
                encryptPB.Hide();
                return;
            }
            else if (cipherMode == "CFB")
            {
                mode = CipherMode.CFB;
            }

            var headers = new XElement("EncryptedFileHeader");

            headers.Add(new XElement("Algorithm", "AES"));
            headers.Add(new XElement("KeySize", keySize));
            headers.Add(new XElement("BlockSize", blockSize));
            headers.Add(new XElement("CipherMode", cipherMode));
            headers.Add(new XElement("IV", Convert.ToBase64String(fileIV)));
            var extensionOfFile = Path.GetExtension(fileTB.Text);

            headers.Add(new XElement("Extension", extensionOfFile));
            var approvedUsers = new XElement("ApprovedUsers");

            foreach (var user in users)
            {
                approvedUsers.Add(new XElement("User",
                                               new XElement("Email", user.name),
                                               new XElement("SessionKey", user.encryptedSessionKey)));
            }
            headers.Add(new XElement(approvedUsers));
            var xmlFile = new XDocument(headers);

            var dirToSave      = fileTB.Text.Substring(0, fileTB.Text.LastIndexOf("\\") + 1);
            var outputFileName = dirToSave + outputFilenameTB.Text;

            xmlFile.Save(outputFileName);

            encryptPB.Show();
            if (backgroundWorker1.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();
            }

            using (var swEncrypt = File.AppendText(outputFileName))
            {
                //Write all data to the stream.
                var sessionKeyInBytes = Encoding.ASCII.GetBytes(sessionKey);
                swEncrypt.Write(
                    Convert.ToBase64String(
                        ServiceRijndaelAES.EncryptStringToBytes(this,
                                                                File.ReadAllText(
                                                                    fileTB.Text), sessionKeyInBytes, mode, fileIV)));
            }

            this.encryptMessageLabel.ForeColor = Color.Green;
            this.encryptMessageLabel.Text      = "Encryption suceeded";
        }