//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); }
//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!"); }