//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;
            }
        }
        private void EncryptHash(string hashedMessageToEncrypt)
        {
            byte[] hashedMessageBytes = Encoding.UTF8.GetBytes(hashedMessageToEncrypt); //hashed message omzetten naar byte-array

            //Objecten aanmaken die gebruikt moeten worden
            RSAWithRSAParameterKey rsa    = new RSAWithRSAParameterKey();
            HybridEncryption       hybrid = new HybridEncryption();

            byte[] encryptedHash = rsa.EncryptHashedData(hashedMessageBytes); //Hashed message encrypteren

            //Filename voor encrypted hash maken
            int    startIndex        = loadedFilename.LastIndexOf(@"\") + 1;
            string folderReceiver    = Application.StartupPath + @"\Users\" + receiver + @"\HashedInbox\";
            string encryptedFilename = folderReceiver + "EncrHash_From" + username + "_" + loadedFilename.Substring(startIndex);

            //Geëncrypteerde hashed message in HashedInbox-map plaatsen
            File.WriteAllBytes(encryptedFilename, encryptedHash);
        }
Beispiel #3
0
        //ENCRYPTEER DATA
        //original = te encrypteren data
        public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams)
        {
            //Genereert session key
            byte[] sessionKey = aes.GenerateRandomNumber(32);

            //Maakt encryptedPacket en genereert iv
            EncryptedPacket encryptedPacket = new EncryptedPacket {
                iv = aes.GenerateRandomNumber(16)
            };

            //Encrypteert data met AES-sleutel
            encryptedPacket.encryptedData = aes.Encrypt(original, sessionKey, encryptedPacket.iv); //Session key en IV gebruiken voor encryptie van data

            //Encrypteert de session key met RSA
            encryptedPacket.encryptedSessionKey = rsaParams.EncryptData(sessionKey);

            return(encryptedPacket);
        }
        //GELADE FILE ENCRYPTEREN, HASHEN EN VERSTUREN
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            //Gelade file omzetten naar byte-array
            byte[] dataToEncrypt = File.ReadAllBytes(loadedFilename);

            //Objecten aanmaken die gebruikt moeten worden
            RSAWithRSAParameterKey rsaParams = new RSAWithRSAParameterKey();
            HybridEncryption       hybrid    = new HybridEncryption();

            //Filename voor encrypted data maken
            int    startIndex        = loadedFilename.LastIndexOf(@"\") + 1;
            string folderReceiver    = Application.StartupPath + @"\Users\" + receiver + @"\Inbox\";
            string encryptedFilename = folderReceiver + "Encr_From" + username + "_" + loadedFilename.Substring(startIndex);

            //ZOWEL DE DATA-, SESSION KEY- EN IV-FILE WORDEN HIERONDER GEHAALD UIT HET ENCRYPTEDPACK
            //Data wordt geencrypteerd + in Inbox van receiver geplaatst
            EncryptedPacket encryptedBlock = hybrid.EncryptData(dataToEncrypt, rsaParams);

            File.WriteAllBytes(encryptedFilename, encryptedBlock.encryptedData);

            //Filename voor session key maken
            string keyFilename = Application.StartupPath + @"\Users\" + receiver + @"\Cryptodata\" + "Encrypted Session Key";

            File.WriteAllBytes(keyFilename, encryptedBlock.encryptedSessionKey);

            //Filename voor iv maken
            string ivFilename = Application.StartupPath + @"\Users\" + receiver + @"\Cryptodata\" + "IV";

            File.WriteAllBytes(ivFilename, encryptedBlock.iv);

            //Hash maken van de oorspronkelijke file
            hashedMessage = Hash.ToMD5Hash(File.ReadAllText(loadedFilename));

            //Hash encrypteren
            EncryptHash(hashedMessage);

            MessageBox.Show($"You have successfully signed, encrypted and sent the file to {receiver}!", "Succesfully hashed, encrypted and sent!");
        }
        //HASH CHECKEN
        private void btnCheckHash_Click(object sender, EventArgs e)
        {
            //Dialoogvenster aanklikken om een geëncrypteerde hash te openen
            dialogWindow.InitialDirectory = hashedInbox;
            dialogWindow.Filter           = "Text|*.txt|All|*.*";
            try
            {
                if (dialogWindow.ShowDialog() == DialogResult.OK)
                {
                    string hashFileToDecrypt = dialogWindow.FileName;
                    string receivedFrom      = DefineSender(hashFileToDecrypt); //Bepalen van wie de geëncrypteerde hash komt

                    RSAWithRSAParameterKey rsa = new RSAWithRSAParameterKey();
                    byte[] dataToDecrypt       = File.ReadAllBytes(hashFileToDecrypt); //file omzetten naar byte-array
                    byte[] decryptedHashArray  = rsa.DecryptHashedData(dataToDecrypt, receivedFrom);
                    string decryptedHash       = Encoding.UTF8.GetString(decryptedHashArray);

                    MessageBox.Show("Choose a decrypted file to compare the hash with.", "Choose file.");
                    hashIsEqual = ChooseFileToCompare(decryptedHash);

                    DetermineLabel(hashIsEqual);
                }
            }
            catch (IOException)
            {
                MessageBox.Show($"Close {dialogWindow.FileName} before continueing.", $"Close {dialogWindow.FileName}.");
            }
            catch (Exception)
            {
                MessageBox.Show("This is not a valid enrcypted hash.", "Warning");
            }
            finally
            {
                reader.Close();
            }
        }
Beispiel #6
0
        //NIEUWE USER MAKEN
        private void CreateUser()
        {
            bool containsSpace = false;

            try
            {
                writer = File.AppendText(path + "/user.login");

                //Username controleren en in user.login zetten
                for (int i = 0; i < username.Length; i++)
                {
                    if (username[i] == ' ')
                    {
                        lblNotification.Text = "Username can not contain a space.";
                        containsSpace        = true;
                        return;
                    }
                }

                //Password controleren
                for (int i = 0; i < password.Length; i++)
                {
                    if (password[i] == ' ')
                    {
                        lblNotification.Text = "Password can not contain a space.";
                        containsSpace        = true;
                        return;
                    }
                }

                if (!containsSpace)
                {
                    writer.Write(username + ",");
                }

                //Salt maken en in user.login zetten
                string salt = PasswordStorage.GenerateSalt();
                writer.Write(salt + ",");

                byte[] saltBytes     = Encoding.UTF8.GetBytes(salt);
                byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

                //Password + salt hashen en in user.login zetten
                string hashedPassword = PasswordStorage.HashPassword(passwordBytes, saltBytes, 50000);
                writer.WriteLine(hashedPassword);

                loginForm.userCreated = true;

                //Public en private key van deze gebruiker genereren
                RSAWithRSAParameterKey rsaParams = new RSAWithRSAParameterKey(username);
                rsaParams.GeneratePrivatePublicKeys();

                CreateFolder(); //Folders voor de gebruiker maken
            }
            catch (IOException)
            {
                MessageBox.Show("Close the user.login-file before registering.", "Close user.login-file.");
                return;
            }
            finally
            {
                writer.Close();

                if (!containsSpace)
                {
                    this.Close();
                }
            }
        }
Beispiel #7
0
        //NEEMT GEENCRYPTEERD PAKKET (met geencrypteerde data, session key en iv)
        public byte[] DecryptData(byte[] encryptedSessionKey, byte[] encryptedData, byte[] iv, RSAWithRSAParameterKey rsaParams)
        {
            byte[] decryptedData = null;

            //Decrypteer AES-key met RSA + Decrypteren van session key met RSA
            byte[] decryptedSessionKey = rsaParams.DecryptData(encryptedSessionKey);

            //Decrypteer de data met de AES-key
            decryptedData = aes.Decrypt(encryptedData, decryptedSessionKey, iv);

            return(decryptedData);
        }